成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Big Integer Addition

i_garfileo / 2897人閱讀

Problem

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

Notice

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.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

Example

Given num1 = "123", num2 = "45"
return "168"

Solution
public class Solution {
    /*
     * @param num1: a non-negative integers
     * @param num2: a non-negative integers
     * @return: return sum of num1 and num2
     */
    public String addStrings(String num1, String num2) {
        //start from adding the last digits of num1, num2:
        //if the current sum > 10, save 1 in `carry`,
        //add to the front of StringBuilder sb
        //... doing this till both indice less than 0
        
        int i = num1.length()-1, j = num2.length()-1, carry = 0, curSum = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0 || carry == 1) {
            //Integer.valueOf(String.valueOf(char)) is to remind me that the value of char is mapped to the decimal value in ascii 
            int curNum1 = i >= 0 ? Integer.valueOf(String.valueOf(num1.charAt(i))) : 0;
            int curNum2 = j >= 0 ? Integer.valueOf(String.valueOf(num2.charAt(j))) : 0;
            int sum = carry + curNum1 + curNum2;
            curSum = sum % 10; carry = sum/10;
            sb.insert(0, curSum);
            i--; j--;
        }
        return sb.toString();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/70857.html

相關(guān)文章

  • [LintCode/LeetCode] Range Addition

    Problem Assume you have an array of length n initialized with all 0s and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each el...

    endless_road 評論0 收藏0
  • [LintCode] Hash Function

    摘要:又用到了取余公式,推導(dǎo)出。用循環(huán)對數(shù)組各位進行轉(zhuǎn)換和相乘和累加。因為第二個取余公式證明乘積取余與乘數(shù)相加后再取余等價于乘積取余,所以在每個循環(huán)內(nèi)都進行一次取余,以免乘積太大溢出。 Problem In data structure Hash, hash function is used to convert a string(or any other type) into an int...

    Dogee 評論0 收藏0
  • [LintCode] Buy Fruits

    Problem Xiao Ming is going to help companies buy fruit. Give a codeList, which is loaded with the fruit he bought. Give a shoppingCart, which is loaded with target fruit. We need to check if the order...

    邱勇 評論0 收藏0
  • [LintCode] Integer to Roman & Roman to Integer

    摘要:建立映射整數(shù)數(shù)組字符串數(shù)組,這兩個數(shù)組都要從大到小,為了方便之后對整數(shù)進行從大到小的分解,以便用從前向后建立數(shù)字。建立,存入的數(shù)值對應(yīng)關(guān)系。 Problem Integer to RomanGiven an integer, convert it to a roman numeral.The number is guaranteed to be within the range fro...

    pkwenda 評論0 收藏0
  • [LintCode] Reverse Integer

    摘要:這道題有一些細節(jié)需要留意。新數(shù)會不會溢出符號位如何處理用慣用的做法,除以取余,得到最低位,放進。每次循環(huán)乘以累加當前最低位,同時除以不斷減小。要點在于考慮乘累加運算的情況,用分支語句判斷發(fā)生溢出的條件。最后的結(jié)果要加上之前排除的符號位。 Problem Reverse digits of an integer. Returns 0 when the reversed integer o...

    The question 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<