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 = ... 
随机推荐
- MySQL 分区间进行数据展示 实例
			如何进行分区间数据统计示例 业务场景:统计消费总金额大于1000元的,800到1000元的,500到800元的,以及500元以下的人数. SELECT COUNT(CASE WHEN IFNULL(t ... 
- window.open 子窗口关闭刷新父页面
			function JsMod(htmlurl,tmpWidth,tmpHeight){ htmlurl=getRandomUrl(htmlurl); var winObj = window.open( ... 
- 第7章 网络层协议(3)_ARP协议
			3. ARP协议 3.1 ARP(Address Resolution Protocol)协议的工作过程和安全隐患 (1)计算机A和C通信之前,先检查ARP缓存中是否有计算机C的IP地址对应的MAC地 ... 
- Keras 实现一个简单GAN
			Keras 实现一个简单GAN 代码中需提供: Loss Function 参见Keras 或者 Tensorflow 文档 model_param_matrix 反向调整的模型参数/参数矩阵 ... 
- SQLite3数据库
			#SQLite可视化管理工具(SQLite Expert Pro) SQLite特点: 1. 遵守ACID(原子性.一致性.隔离性和持久性)的关系型数据库管理系统:2. 不是一个C/S结构的数据库引擎 ... 
- python数字
			#=====>part1:数字类型#掌握:int,float#了解:Long(在python2中才有),complex# num=10# num=int(10)# print(type(num) ... 
- mapreduce深入剖析5大视频
			参考代码 TVPlayCount.java package com.dajiangtai.hadoop.tvplay; import java.io.IOException; import org.a ... 
- CS229  6.4 Neurons Networks Autoencoders and Sparsity
			BP算法是适合监督学习的,因为要计算损失函数,计算时y值又是必不可少的,现在假设有一系列的无标签train data: ,其中 ,autoencoders是一种无监督学习算法,它使用了本身作为标签以 ... 
- Android数据传递,使用广播BroadcastReceiver;
			Android数据传递有很多种,Intent意图传递或使用Bundle去传递,接口监听回调传递数据,也可以把数据保存起来,使用的时候去读取等等等...,"当你知道足够多的数据传递的方式之后, ... 
- jQuery设置div的自适应布局
			一.HTML代码: <div class="ui-wraper" id="Wraper"> </div> 二.CSS代码: html { ... 
