classSolution: defcountSymmetricIntegers(self, low: int, high: int) -> int: ans = 0 for i inrange(low, high + 1): s = str(i) n = len(s) ans += n % 2 == 0andsum(map(int, s[:n // 2])) == sum(map(int, s[n // 2:])) return ans
classSolution { publicintcountSymmetricIntegers(int low, int high) { intans=0; for (inti= low; i <= high; i++) { char[] s = Integer.toString(i).toCharArray(); intn= s.length; if (n % 2 > 0) { continue; } intsum=0; for (intj=0; j < n / 2; j++) { sum += s[j]; } for (intj= n / 2; j < n; j++) { sum -= s[j]; } if (sum == 0) { ans++; } } return ans; } }
[sol-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intcountSymmetricIntegers(int low, int high){ int ans = 0; for (int i = low; i <= high; i++) { auto s = to_string(i); int n = s.length(); if (n % 2 == 0 && accumulate(s.begin(), s.begin() + n / 2, 0) == accumulate(s.begin() + n / 2, s.end(), 0)) { ans++; } } return ans; } };
funccountSymmetricIntegers(low int, high int) (ans int) { for i := low; i <= high; i++ { s := strconv.Itoa(i) n := len(s) if n%2 > 0 { continue } sum := 0 for _, c := range s[:n/2] { sum += int(c) } for _, c := range s[n/2:] { sum -= int(c) } if sum == 0 { ans++ } } return }
var countSymmetricIntegers = function (low, high) { let ans = 0; for (let i = low; i <= high; i++) { const s = i.toString(); const n = s.length; if (n % 2) { continue; } const m = n >> 1; let sum = 0; for (let j = 0; j < m; j++) { sum += s.charCodeAt(j); } for (let j = m; j < n; j++) { sum -= s.charCodeAt(j); } if (sum === 0) { ans++; } } return ans; };