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. Java内部类引用外部类中的局部变量为何必须是final问题解析

    今天编写一个多线程程序,发现在方法内定义内部类时,如果内部类调用了方法中的变量,那么该变量必须申明为final类型,百思不得其解,后来想到应该是生命周期的原因,因为方法内定义的变量是局部变量,离开该方 ...

  2. Oracle函数中将参数放在模糊查询中

    --diagnosis_name like '%'||diagnosis_names||'%' create or replace function asdf(MIN_DATE IN varchar2 ...

  3. asp控制项目超时

    If Session("username")="" or isnull(Session("username")) Then %> &l ...

  4. 第12章 网络基础(2)_数据封装和IP地址

    4. 数据封装和IP地址 (1)数据封装 (2)IP地址 ①在TCP/IP网络中,每个主机都有唯一的地址,它是通过IP协议族实现的. ②IP协议要求在每次与TCP/IP网络建立连接时,每台主机都必须为 ...

  5. Nature | 光学CNN层替换传统CNN层,超省电

    CNN 计算效率的研究一直备受关注,但由于功率和带宽的严格限制,CNN 仍难以应用在嵌入式系统如移动视觉.自动驾驶中.在斯坦福大学发表在 Nature 旗下 Scientific Reports 的这 ...

  6. Spring中的ThreadPoolTaskExecutor

      在观察线上系统的运行情况下,发现在错误日志中有这类错误信息,org.springframework.core.task.TaskRejectedException,于是便对ThreadPoolTa ...

  7. Convolutional Neural Networks

    卷积神经网络(Convolutional Neural Networks/ CNN/ConvNets) 卷积神经网络和普通神经网络十分相似: 组成它们的神经元都具有可学习的权重(weights)和偏置 ...

  8. selenium自动化测试遇到的问题积累--持续积累中

    引言: 在做UI自动化测试过程中,总是会遇到各种问题,而解决问题总是会花费一些时间和心思,但是解决后对于自己就是一种成长,持续积累,当可能遇到的问题都被你遇到过,并且都知道解决办法,这就是一种经验的价 ...

  9. openstack 基镜像与差异镜像的整合

  10. OpenACC 书上的范例代码(Jacobi 迭代),part 3

    ▶ 使用Jacobi 迭代求泊松方程的数值解 ● 使用 data 构件,强行要求 u0 仅拷入和拷出 GPU 各一次,u1 仅拷入GPU 一次 #include <stdio.h> #in ...