|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
题目不难,可以用来练习stl和lambda
- #include <numeric>
- using namespace std;
- class Solution {
- public:
- string readSum(string &s) {
- static const char* digit[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
-
- string t = to_string(accumulate(s.begin(), s.end(), int(0), [](int x,char c){ return x + c-'0'; }));
- return accumulate(t.begin(), t.end(), string(),
- [=](const string& s, char c){
- return s.empty() ? string(digit[c-'0']) : s + " " + digit[c-'0'];
- });
- }
- };
复制代码 用python的话可以写的更简洁且不失效率
- class Solution:
- def readSum(self, n):
- digits = ["zero","one","two","three","four","five","six","seven","eight","nine"]
- return " ".join([digits[int(d)] for d in str(sum([int(c) for c in str(n)]))])
复制代码
|
|