415. Add Strings

BitInteger的最简单的部分

把两个string反过来,从后往前加,可以学习的地方是对于while的条件可以设置成,index1< len1 || index2 < len2.

然后在内部分别对index1 < len1和index2 <len2处理,这样就不用对其中一个提前结束的情况单独再写一个循环了

 1     public String addStrings(String num1, String num2) {
 2         if(num1 == null) {
 3             return num2;
 4         }
 5         if(num2 == null) {
 6             return num1;
 7         }
 8         StringBuilder sb1 = new StringBuilder(num1);
 9         StringBuilder sb2 = new StringBuilder(num2);
10         int len1 = num1.length();
11         int len2 = num2.length();
12         sb1.reverse();
13         sb2.reverse();
14         StringBuilder sb = new StringBuilder();
15         int index1 = 0;
16         int index2 = 0;
17         int carry = 0;
18         while(index1 < len1 || index2 < len2) {
19             int cur = carry;
20             if(index1 < len1) {
21                 cur += sb1.charAt(index1++) - ‘0‘;
22             }
23             if(index2 < len2) {
24                 cur += sb2.charAt(index2++) - ‘0‘;
25             }
26             if(cur > 9) {
27                 cur %= 10;
28                 carry = 1;
29             } else {
30                 carry = 0;
31             }
32             sb.append(cur);
33         }
34         if(carry == 1) {
35             sb.append(1);
36         }
37         return sb.reverse().toString();
38     }
时间: 2024-12-21 23:25:26

415. Add Strings的相关文章

36. leetcode 415. Add Strings

415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain

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

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

415.两个字符串相加 Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

[LeetCode] Add Strings 字符串相加

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

Add Strings

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

LeetCode算法题-Add Strings(Java实现)

这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num1和num2的总和. 注意: num1和num2的长度均<5100. num1和num2都只包含数字0-9. num1和num2都不包含任何前导零. 您不能使用任何内置BigInteger库或直接将输入转换为整数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 6

【LeetCode415】Add Strings

题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: 1 public class LeetCode415 { 2 public static void main(String[] args) { 3 String a="1",b="9"; 4 System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b)); 5

Add Strings Leetcode

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

Leetcode: Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.