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

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

题目

如题

思路

1. while loop will run as long as there is character left in nums1, nums2 or carry

2. from right to left, each character is converted to integer

3. If the shorter string is exhausted first, the value will be forced to `0` as default

代码

 class Solution {
public String addStrings(String num1, String num2) {
//how many digits will be added? three! 1st from num1Array, 2nd from num2Array, 3rd from carry
int carry = 0;
int num1Len = num1.length()-1;
int num2Len = num2.length()-1; // StringBuilder can append and remove, more efficient
StringBuilder sb = new StringBuilder(); //while loop will run as long as there is character left in nums1, nums2 or carry
while(num1Len >= 0 || num2Len >=0 || carry >0){
// from right to left, each character is converted to integer
// If the shorter string is exhausted first, the value will be forced to `0` as default
int update1 = num1Len>=0 ? num1.charAt(num1Len--)-'0' :0;
int update2 = num2Len>=0 ? num2.charAt(num2Len--)-'0' :0;
int sum = update1 + update2 + carry;
sb.insert(0,sum%10);
carry = sum/10;
}
return sb.toString();
}
}

[leetcode]415. Add Strings字符串相加的更多相关文章

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

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

  2. 415 Add Strings 字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意:    num1 和num2 的长度都小于 5100.    num1 和num2 都只包含数字 0-9.    num1 和 ...

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

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

  4. 36. leetcode 415. Add Strings

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

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

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

  6. LeetCode - 415. Add Strings

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

  7. leetcode 415 两个字符串相加

    string addstring(string s1,string s2) { string ans=""; ; ,j=s2.length()-;i>=||j>=;i- ...

  8. 【leetcode】415. Add Strings

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

  9. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

随机推荐

  1. [UE4]射击和直线追踪

    只有有朝向的组件(如character.Controller等)才能动态获取到朝向,在编辑视图中会有一个箭头表示正前方: 如果是没有朝向的组件(如摄像机),则获取到的朝向永远是固定的

  2. javascript创建对象之函数构造模式和原型模式结合使用(四)

    创建自定义类型的常见方式就是组合使用构造函数模式与原型模式一起使用. 构造函数模式用于定义实例对象的特有的部分(属性和方法),原型模式用于定义共享的部分. 这样最大限度的节省了内存的开销. funct ...

  3. javascript cookie操作.

    给cookie追加name: document.cookie="name="+escape("我叫钢蛋");//这里可以不写secape,这样写会更加的安全., ...

  4. hbase建表时 ERROR: java.io.IOException: Table Namespace Manager not ready yet, try again later

    其实解决不难,是因为时钟不同步,把每个节点切换到root用户下同步时钟就好了,在重启hbase!

  5. Linux Performance Analysis and Tools(Linux性能分析和工具)

    首先来看一张图: 上面这张神一样的图出自国外一个Lead Performance Engineer(Brendan Gregg)的一次分享,几乎涵盖了一个系统的方方面面,任何人,如果没有完善的计算系统 ...

  6. 用多个class选择元素

    注意下面两个的区别:  $(".role-developer.role-designer").css("color","red");  $( ...

  7. sqlserver查询---分配cpu等资源

    数据库资源按需分配 https://www.cnblogs.com/i6first/p/4138365.html https://blog.csdn.net/kk185800961/article/d ...

  8. JAVA 整合 SSM (Spring + SpringMVC + MyBatis)

    < 一 > POM 配置文件 ( 如果出现 JAR 包 引入错误, 请自行下载 ) <project xmlns="http://maven.apache.org/POM/ ...

  9. leetcode496

    public class Solution { public int[] NextGreaterElement(int[] findNums, int[] nums) { var list = new ...

  10. python之内置函数:map ,filter ,reduce总结

    map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...