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. DIY-组装

    DIY:-组装 组装,现在基本什么都可以组装,就像计算机,手机,自己进行定制,同样操作系统可以自己组装,软件开发也要组装,现在就是一个DIY的时代. 大家了解DIY,说白了就是自己定制组装一些东西,比 ...

  2. PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别

    这篇文章主要介绍了PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别,本文给出了这两个参数的5个区别,需要的朋友可以参考下 虽然nos ...

  3. 一、探索 Android Studio

    探索 Android Studio 本文内容 项目结构 界面 Gradle 构建系统 调试和分析工具 Android Studio 是基于 IntelliJ IDEA 的官方 Android 应用开发 ...

  4. awk(gawk)

    awk,逐行处理文本内容.Linux里的awk其实是“gawk”. 使用格式: awk [选项] '模式匹配 {命令 命令参数}' file1, file2, …… 支持的选项 说明 -f progr ...

  5. openx _金额

    1/work/openx/lib/max/Delivery/log.php MAX_Delivery_log_logAdImpression    MAX_Delivery_log_logAdRequ ...

  6. MySQL优化十大技巧

    转自:https://m.2cto.com/database/201701/557910.html MYSQL优化主要分为以下四大方面: 设计:存储引擎,字段类型,范式与逆范式 功能:索引,缓存,分区 ...

  7. [ilink32 Error] Error: Unresolved external '__fastcall Data::Win::Adodb::TCustomADODataSet

    [ilink32 Error] Error: Unresolved external '__fastcall Data::Win::Adodb::TCustomADODataSet::GetParam ...

  8. J2SE 8的注解

    1. 注解概念 (1) 注解格式 modifiers @interface AnnotationName { type elementName(); type elementName() defaul ...

  9. VBA 公式中使用相对位置

    .Cells(3, 4).FormulaR1C1 = "=sum(r[-" & a & "]c[0]:r[-3]c[" & b & ...

  10. 前端-HTML练习题

    本小节重点: 熟练使用div+span布局,知道div和span的语义化的意思 熟悉对div.ul.li.span.a.img.table.form.input标签有深刻的认知,初期也了解他们,知道他 ...