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 = ...
随机推荐
- (转)Linux netstat命令详解
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
- 关于dict的formkeys方法注意
使用容器中的元素生成k, v为统一值, 指向同一个内存地址 默认值指向同一个内存, 修改就全部修改 strvar = 'abcd' listvar = [] dictvar = {} dictvar ...
- tips:Java中while的判断条件
tips:Java中while的判断条件! 在c++中,有时候会遇到这种情况: while(x = y){ dosomething; } 如果x与y相等,这个时候如果循环体中没有跳出的点,那么会无限循 ...
- [ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)
/* 题目中的神仙性质真的是令人愉悦 因为我一眼看成了每个点的度数不超过二十, 心想这他喵的和字符串什么关系 统计树上不同子串个数, 按道理直接dfs n次把所有的串插到后缀自动机里就行了 但是我们发 ...
- SHOI2016方
/* 上帝说 要方 是的 很方 计数问题的容斥思想 (首先要注意 正方形有斜着的QAQ) 考虑我们要求的合法正方形 ans 根据容斥 ans = 无限制方案书 - 一个点确定的方案数 + 两个点确定的 ...
- JVM总结-垃圾回收
Java 虚拟机的自动内存管理,将原本需要由开发人员手动回收的内存,交给垃圾回收器来自动回收.不过既然是自动机制,肯定没法做到像手动回收那般精准高效 [1] ,而且还会带来不少与垃圾回收实现相关的问题 ...
- 关于text-align和text-align-last
很多人都用过text-align,基本上也比较熟悉这个属性. text-align: left; // 左对齐 text-align: right; // 右对齐 text-align: center ...
- JVM Tools
Java VisualVm 提供可视化界面展示运行在JVM上应用的信息.这些信息可用于诊断剖析应用. Jconsole Jconsole是基于JMX监视工具.Jconsole使用内置的JMX在java ...
- 20.多线程.join()和setDaemon()的使用
1.join()方法 join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等待,直到子线程B完成操作后,才可以接着往下执行,那么在调用这 ...
- Cannot run Eclipse; JVM terminated. Exit code=13
在myeclipse 上运行好好的, 在 eclipse 上就运行不了了. 运行eclipse.exe 就出现: Cannot run Eclipse; JVM terminated. Exit co ...