leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2698.cpp (627B)
0 class Solution {
1 public:
2 bool canPartition(const string &s, const int target) {
3 if (s == "" && target == 0) return true;
4 if (target < 0) return false;
6 int left = 0;
7 for (int i = 0; i < s.size(); i++) {
8 left = left * 10 + s[i] - '0';
9 if (canPartition(s.substr(i + 1), target - left)) return true;
10 }
11 return false;
12 }
14 int punishmentNumber(int n) {
15 int res = 0;
16 for (int i = 1; i <= n; i++) {
17 const int sqr = i * i;
18 if (canPartition(to_string(sqr), i)) res += sqr;
19 }
20 return res;
21 }
22 };