leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1945.cpp (416B)
0 class Solution { 1 static int sum(int n) { 2 int res = 0; 3 4 while (n > 0) { 5 res += n % 10; 6 n /= 10; 7 } 8 9 return res; 10 } 11 12 public: 13 int getLucky(const string &s, int k) const { 14 int res = 0; 15 16 for (const char c : s) 17 res += sum(c - '`'); 18 for (int i = 1; i < k; i++) 19 res = sum(res); 20 21 return res; 22 } 23 };