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 = ...
随机推荐
- PhysX Clothing for UE4
转自:http://www.52vr.com/article-737-1.html Hello! 之前看到论坛里有人发了个关于UE4布料的小知识,于是跟帖说抽空写个布料的工作流程供大家参考,那么,今天 ...
- python程序化交易神器——tushare
一直想试着将自己的交易思路程序化,可惜困难重重 ,连第一步获取数据都要花很多精力,直到最近发现了Tushare,不仅使用非常便利,功能也无比强大,股票.期货.基金.财经新闻,甚至电影票房等都可以非常便 ...
- linux chown命令解除文件夹的root权限限制
sudo chown -R demouser file 这个命令可以解除linux文件的超级权限限制 摘录: chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名 ...
- Memory Translation and Segmentation.内存地址转换与分段
原文标题:Memory Translation and Segmentation 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精 ...
- Python NLTK——python与nltk配置
按照<Python自然语言处理>中的步骤安装Python后nltk总是部署失败,出现如下提示: >>> import nltk Traceback (most recen ...
- join,fromkeys(),深浅拷贝
1. 补充基础数据类型的相关知识点 1. str. join() 把列表变成字符串例 # s= "哈哈"# s1=s.join('-')# print(s1)# s="呵 ...
- 零基础学习python_模块(50-52课)
今天学了下模块,那什么是模块呢?其实我们写的以py结尾的一个文件就是一个模块,模块也就是程序 还记得我们之前学过容器.函数.类吧 容器 -> 数据的封装 函数 -> ...
- 了解一下,Java 虚拟机
1.1 - 概述 Java 总述:Java 不仅是一门编程语言,还是一个由一系列计算机软件和规范形成的技术体系,这个技术体系提供了完整的用于软件开发和跨平台部署的支持环境,并广泛应用于 嵌入式 ...
- [Unity算法]A星寻路(一):基础版本
参考链接: https://www.cnblogs.com/yangyxd/articles/5447889.html 一.原理 1.将场景简化,分割为一个个正方形格子,这些格子称之为节点(node) ...
- mysql 动态拼接表字段,值 mybatis 动态获取表字段
-- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...