sum-root-to-leaf-numbers
来源:互联网 发布:crm软件价格 编辑:程序博客网 时间:2024/06/06 08:32
c++中各类类型与string类型的相互转化http://www.cnblogs.com/nzbbody/p/3504199.html
这道题还是坑了我这么久,一是忘了保留多种可能性,二是忘记了出来多个0的情况。后来用string来处理的确是方便了许多。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */int f(int s) { int a=0; if(s==0) return 1; while(s) { a++; s/=10; } return a;}vector<string> f2(TreeNode* root) { vector<string> v; if(root==NULL) return v; vector<string> v1 = f2(root->left); vector<string> v2 = f2(root->right); if(root->left||root->right) { for (int i=0;i<v1.size();i++) { ostringstream ss; ss<<root->val<<v1[i]; v.push_back(ss.str()); } for (int i=0;i<v2.size();i++) { ostringstream ss; ss<<root->val<<v2[i]; v.push_back(ss.str()); } } else { ostringstream ss; ss<<root->val; v.push_back(ss.str()); } return v;}class Solution {public: int sumNumbers(TreeNode *root) { if(root==NULL) return 0; else { vector<string> v1 = f2(root->left); vector<string> v2 = f2(root->right); if(root->left||root->right) { int ans = 0; for (int i=0;i<v1.size();i++) { stringstream ss; ss<<root->val<<v1[i]; int t; ss>>t; ans+= t; } for (int i=0;i<v2.size();i++) { stringstream ss; ss<<root->val<<v2[i]; int t; ss>>t; ans+= t; } return ans; } else { return root->val; } } }};
0 0
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- 源码搭建lnmp环境
- Solr查询中的局部参数Local Parameters
- struts2学习总结(一)
- CocoaPods 的简单快速安装方法
- mvc加载控制器、模型类以及视图类编写
- sum-root-to-leaf-numbers
- MVC框架自己封装开启超神之路(上) 2-1入口文件
- oracle中的having子句
- Dijkstra算法
- PHP闭包 function() use()
- Linux下基于源码安装MySQL
- Android项目针对libs(armeabi,armeabi-v7a,x86)进行平台兼容
- Spring 缓存注解@Cacheable的用法
- 对MVC的理解