447. Add Strings
原文题目:
解题:
字符串的当做整数来做加法,其实就是大数加法的简化版本
思路:
1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再处理num1的高两位
2)考虑进位,相加大于10时就要进位,进位值 = sum/10;
3)考虑最高位的进位,如“5099”+“4987”最后得到了五位的“10086”
AC代码:
class Solution {
public:
string addStrings(string num1, string num2)
{
string re = "";
int len1 = num1.length();
int len2 = num2.length();
int temp1,temp2,tempsum = 0;
string tempstr;
int carry = 0;
int i = 0;
//计算相同位的低位,同时保存进位到carry
while(len1&&len2)
{
temp1 = num1[len1-1]-'0';
temp2 = num2[len2-1]-'0';
tempsum = temp1 + temp2 + carry;
carry = tempsum / 10;
tempstr = tempsum%10 + '0';
re = tempstr + re; //注意re在后,若为re += tempstr,那么结果就需要翻转了
len1--;
len2--;
}
//计算num1或者num2剩余的位
if(len1)
{
for(i = len1-1;i>=0;i--)
{
tempsum = num1[i]-'0' +carry;
carry = tempsum / 10;
tempstr = tempsum%10 + '0';
re = tempstr + re;
}
}
if(len2)
{
for(i = len2-1;i>=0;i--)
{
tempsum = num2[i]-'0' +carry;
carry = tempsum / 10;
tempstr = tempsum%10 + '0';
re = tempstr + re;
}
}
//剩余的位中如果还有进位,那么还需要加上
if(carry)
{
tempstr = carry+'0';
re = tempstr +re;
}
return re;
}
};
447. Add Strings的更多相关文章
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- LeetCode_415. Add Strings
415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2 ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
随机推荐
- OA-DB-LINUX安装说明
HOST配置: HOST1:OADB-NODE1.LSTECH.COM HOST2:OADB-NODE2.LSTECH.COM 每台主机配置两个不同VLAN的IP地址 OSUSER:oracle 1. ...
- GAC 注册查看与删除
1.复制以下命令粘贴到以管理员权限运行的命令行程序里,回车运行(前提条件得有gacutil.exe注册工具): cd "C:\NETFX 4.0 Tools" 以windows7 ...
- Android---页面跳转
1.首先在一个布局文件(.XML)中绘画了一个跳转按钮(id为btn1): <Button android:id="@+id/btn1" an ...
- Noip数学整理
目录 Noip数学整理 序 1 取模相关 2 质数相关 3.基本操作 4.方程相关 5.数列相关 6.函数相关 Noip数学整理 序 因为某些原因, Noip对于数学方面的考纲仅停留在比较小的一部分, ...
- phalcon断点调试(phpStorm+xdebug)
1.下载并添加chrome插件xdebug helper,下载地址:http://www.downcc.com/soft/261091.html 2.php添加xdebug扩展 mkdir -p /u ...
- java 日期排序。。。。
Collections.sort(list, new Comparator<Map<Object, Object>>() { public int compare(Map< ...
- 干净win7要做几步才能运行第一个Spring MVC 写的动态web程序
干净win7要做几步才能运行第一个Spring MVC 写的动态web程序: 1. 下载安装jdk 2. 配置Java环境变量 3. 测试一下第1,2两步是否完全成功:http://jingyan.b ...
- tornado运行提示OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
找到占用端口的进程,kill掉 netstat -ano | find $(port) kill: tskill $(PID)
- linux上常见的压缩解压缩的命令
压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成jpg.ta ...
- 《算法》第六章部分程序 part 3
▶ 书中第六章部分程序,包括在加上自己补充的代码,后缀树的两种实现 ● 后缀树实现一 package package01; import java.util.Arrays; import edu.pr ...