原文题目:

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的更多相关文章

  1. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  4. LeetCode——Add Strings

    LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...

  5. LeetCode_415. Add Strings

    415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...

  6. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  7. LeetCode Add Strings

    原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2  ...

  8. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  9. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

随机推荐

  1. 2-java内省机制(Introspector)

    来一个简单的示例吧 package com.my.test; import java.beans.BeanInfo; import java.beans.Introspector; import ja ...

  2. 学习docker后的个人理解

    一.什么是docker Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行 ...

  3. linux安装chrome浏览器

    按照下面的方式安装 wget -P /home/linfu/桌面 https://dl.google.com/linux/direct/google-chrome-stable_current_amd ...

  4. vmware centos7 动态ip->静态

    TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_ ...

  5. c# 后台AJAX

    public class BackA { #region 后台 AJAX public static string GetPage(string posturl) { Stream outstream ...

  6. android 相对布局例子代码

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. 网页静态处理技术FreeMarker概述

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不仅 ...

  8. 练手mysqlbinlog日志恢复数据(centos6.5 64,mysql5.1)

    练手mysql bin log日志相关 系统是centos 6.5 64 阿里云的服务器 mysql版本5.1 1 如何开启bin-log日志? vi /etc/my.cnf [mysqld] log ...

  9. 36纯 CSS 动画原理,在页面上表现日蚀现象

    原文地址:https://segmentfault.com/a/1190000015070543 感想: 动画,背景颜色 HTML code: <div class="sky" ...

  10. leetcode1005

    func largestSumAfterKNegations(A []int, K int) int { sort.Ints(A) var negatives int var zeros int va ...