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

代码

  1. class Solution {
  2. public String addStrings(String num1, String num2) {
  3. //how many digits will be added? three! 1st from num1Array, 2nd from num2Array, 3rd from carry
  4. int carry = 0;
  5. int num1Len = num1.length()-1;
  6. int num2Len = num2.length()-1;
  7.  
  8. // StringBuilder can append and remove, more efficient
  9. StringBuilder sb = new StringBuilder();
  10.  
  11. //while loop will run as long as there is character left in nums1, nums2 or carry
  12. while(num1Len >= 0 || num2Len >=0 || carry >0){
  13. // from right to left, each character is converted to integer
  14. // If the shorter string is exhausted first, the value will be forced to `0` as default
  15. int update1 = num1Len>=0 ? num1.charAt(num1Len--)-'0' :0;
  16. int update2 = num2Len>=0 ? num2.charAt(num2Len--)-'0' :0;
  17. int sum = update1 + update2 + carry;
  18. sb.insert(0,sum%10);
  19. carry = sum/10;
  20. }
  21. return sb.toString();
  22. }
  23. }

[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. Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!

    Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器! http://www.iplaysoft.com/sublimetext.html 代码编辑器或者文本编辑器,对于程序员来说, ...

  2. 一份CTR的特征工程图

  3. Maven 添加jdk编译插件

    问题描述: 默认情况下,eclipse的maven项目使用jdk1.5编译,而我们的jdk为1.8每次更改jdk1.5之后,只要maven项目已更新,eclipse就会自动的回到jdk1.8结局方法: ...

  4. Java 集合 线程安全

    Java中常用的集合框架中的实现类HashSet.TreeSet.ArrayList.ArrayDeque.LinkedList.HashMap.TreeMap都是线程不安全的,如果多个线程同时访问它 ...

  5. php 编程笔记分享 - 非常实用

    php opendir()列出目录下所有文件的两个实例 php opendir()函数讲解及遍历目录实例 php move_uploaded_file()上传文件实例及遇到问题的解决方法 php使用m ...

  6. Spark交互式工具spark-shell

    REPL Spark REPL Spark shell 下面我们启动一下(我这里搭建的是3节点集群) sc.后面按TAB键可以把提示调出来 查看hdfs上文件内容 这个数据从这里下载的 https:/ ...

  7. 小朋友学C语言(7)

    数组 一.数组简介 C 语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比 ...

  8. VSCode编辑器编写Python代码

    如何用VSCode愉快的写Python https://code.visualstudio.com/   在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带 ...

  9. centos7配置yum源

    https://www.cnblogs.com/renpingsheng/p/7845096.html

  10. 5. Java中序列化的serialVersionUID作用

    Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象. 序列化是Java中实现持久化存储的一种方法:为数据传输提供了线路级对象表示法. Java的序列化机制是通过在运行时判断 ...