[leetcode-415-Add Strings]

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.

思路:

模拟手工加法过程,用carry表示进位。

第一个是自己写的,比较啰嗦。

第二个是参考的网上大神的,简洁。

string addStrings(string num1, string num2)
     {
         int n1 = num1.length()-1, n2 = num2.length()-1;
         string ret="";
         int digit = 0;
         int carry = 0;
         int sum = 0;
         while (n1 >= 0 && n2 >= 0)
         {
             sum = carry + num1[n1--] + num2[n2--] - ‘0‘ - ‘0‘;
             digit = sum %10;
             carry = (sum >= 10) ? 1 : 0;
             ret += (digit+‘0‘);
         }

         while (n1 >= 0)
         {
             sum = carry + num1[n1--] - ‘0‘;
             digit = sum % 10;
             carry = (sum >= 10) ? 1 : 0;
             ret += (digit + ‘0‘);
         }        

         while (n2 >= 0)
         {
             sum = carry + num2[n2--] - ‘0‘;
             digit = sum % 10;
             carry = (sum >= 10) ? 1 : 0;
             ret += (digit + ‘0‘);
         }        

        if (carry) ret += ‘1‘;
         reverse(ret.begin(), ret.end());
         return ret;
     }
string addStrings(string num1, string num2) {
    int i = num1.size() - 1;
    int j = num2.size() - 1;
    int carry = 0;
    string res = "";
    while(i>=0 || j>=0 || carry){
        long sum = 0;
        if(i >= 0){sum += (num1[i] - ‘0‘);i--;}
        if(j >= 0){sum += (num2[j] - ‘0‘);j--;}
        sum += carry;
        carry = sum / 10;
        sum = sum % 10;
        res =  res + to_string(sum);
    }
    reverse(res.begin(), res.end());
    return res;
}

参考:

https://discuss.leetcode.com/topic/62305/c-_accepted_13ms

时间: 2024-08-07 08:17:20

[leetcode-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

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 nu

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurre

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

Leetcode 数 Add Binary

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Binary Total Accepted: 9509 Total Submissions: 37699 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

Leetcode: Group Shifted Strings

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -> &quo

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a