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 8.0用户和角色管理
MySQL 8.0用户和角色管理 MySQL8.0新加了很多功能,其中在用户管理中增加了角色的管理,默认的密码加密方式也做了调整,由之前的sha1改为了sha2,同时加上5.7的禁用用户和用户过期的设 ...
- 如何跳过前端JavaScript的验证
绕开前端的JS验证通常有以下的方法: 方法一: 将页面保存到自己机器上,然后把脚本检查的地方去掉,最后在自己机器上运行那个页面就可以了 方法二: 该方式与方法一类似一样,只是将引入js的语句删掉,或则 ...
- MySql存储过程与函数详解
存储过程和函数是在数据库中定义一些SQL语句的集合,然后直接调用这些存储过程和函数来执行已经定义好的SQL语句.存储过程和函数可以避免开发人员重复的编写相同的SQL语句.而且,存储过程和函数是在MyS ...
- python数字
#=====>part1:数字类型#掌握:int,float#了解:Long(在python2中才有),complex# num=10# num=int(10)# print(type(num) ...
- 深入理解Apache Flink
Apache Flink(下简称Flink)项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多人的关注.本文将深入分析Flink的一些关键技术与特性,希望能够帮 ...
- 安装部署elasticsearch
ELK下载:https://www.elastic.co/downloads/ Beats:负责收集系统数据,可以直接发送到es中,也可以通过logstash中转 logstash:收集日志,为bea ...
- es6(12)--类,对象
//类,对象 { //基本定义和生成实例 class Parent{ //定义构造函数 constructor(name='QQQ'){ this.name=name; } } let v_paren ...
- PowerDesigner 物理数据模型(PDM) 说明
ref: https://blog.csdn.net/tianlesoftware/article/details/6874067 一. PDM 介绍 物理数据模型(Physical Data ...
- 安全测试6_Web安全工具第二节(代理抓包分析工具)
上节课讲了浏览器及扩展,这节课继续来学习下抓包分析. 首先看下下图,了解下代理工具的原理:代理就相当于收费站一样,任何要通过的车辆必须经过它. 浏览器的代理我们可以通过设置进行手动设置代理,或者通过P ...
- spring 之 depends-check
我们知道 spring xml 配置中 bean 的子元素 property 是按照 setXxx(SomeType someParam) 方式来设值的,换句话说, 是根据属性的setter 方法, ...