415. Add Strings
来源:互联网 发布:三维动画制作软件手机 编辑:程序博客网 时间:2024/04/30 11:26
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;class Solution {public: string addStrings(string num1, string num2) { int x=num1.size(), y=num2.size(), i,carry,temp; vector<int>ret; if(x==0&&y==0) return ""; reverse(num1.begin(),num1.end()); reverse(num2.begin(),num2.end()); carry=0; for(i=0;i<x&&i<y;i++) { temp=(num1[i]-'0')+(num2[i]-'0')+carry; carry=temp/10; ret.push_back(temp%10); } while(i<x) { temp=num1[i]-'0'+carry; carry=temp/10; ret.push_back(temp%10); i++; } while(i<y) { temp=num2[i]-'0'+carry; carry=temp/10; ret.push_back(temp%10); i++; } if(carry>0) ret.push_back(carry); string s; for(i=ret.size()-1;i>=0;i--)//将整数数组转化为字符串 { char c=ret[i]+'0'; s=s+c; } return s; }};int main(){ string num1,num2; cin>>num1>>num2; string result; Solution solve; cout<<solve.addStrings(num1,num2)<<endl; return 0;}
0 0
- 415. Add Strings
- Leetcode-415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- [LeetCode]--415. Add Strings
- LeetCode 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings(unsolved)
- leetcode 415. Add Strings
- Android OkHttp完全解析
- AsyncTask 流程解析
- iOS 客户端学习-UITableViewCell高度自适应
- String s="hello";s=s+"world",这两行代码执行后,原始的String对象中的内容到底变了没有?
- 疯狂了!当游戏爱上MongoDB会怎么样???
- 415. Add Strings
- Android Intent传值和Activity之间对象列表传递
- CSS3快速上手之5:线性渐变
- POJ3735【矩阵快速幂】
- java中如何忽略字符串中的转义字符--转载
- Android各种访问权限Permission详解
- Kafka+Storm+HDFS整合
- Linux下的目录及其操作命令(链接文件的命令
- 拦截器实现未登录跳转