[leetcode]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
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - 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字符串相加的更多相关文章
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- 415 Add Strings 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和 ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode - 415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- leetcode 415 两个字符串相加
string addstring(string s1,string s2) { string ans=""; ; ,j=s2.length()-;i>=||j>=;i- ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
随机推荐
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图
Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有: 第一个按钮:Reimport All Maven Projects 表示根据 pom.xml 重新载入项 ...
- 关于input=file的用法
<input type="file"/>这个东西是用来上传图片用的. 1,但是存在一下问题但是在在各个浏览器下的显示是不一样的 IE下: IE之外的浏览器: 2.如果不 ...
- javascript继承之原型式继承(四)
javascript之父道格拉斯在2006年给出了这样一串代码,来实现继承. function object(o) { function F() { } F.prototype = o; return ...
- Openstack kvm win7镜像制作
本文地址http://www.cnblogs.com/tcicy/p/7790956.html 网上找了很多为openstack制作win7镜像的文章,总是不成功 自己写一下,以便大家查看. 我使用c ...
- MySQL数据库索引(上)
上一篇回顾: 1.数据页由七部分组成,包括File Header(描述页的信息).Page Header(描述数据的信息).Infimum + Supremum(页中的虚拟数据最大值和最小值).Use ...
- ajaxGet 获取封装
callback 表示下一个功能(回调函数) function ajaxGet(url,callback,data){ 如果路径上有参数 就在url后面拼接参数 否则只请求url ...
- Python之函数——进阶篇
嵌套函数 ---函数内部可以再次定义函数 ---函数若想执行,必须被调用 注意,下例中,执行结果为什么? age = 19 def func1(): print(age) def func2(): p ...
- servletConfig的使用案例
servletConfig参数的使用案例 首先,建立Dynamic Web Project ,同样命名FirstServlet,然后建立Servlet:Login.java,包名为cc.openhom ...
- TortoiseGit上传项目到GitHub
1. 简介 gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. 2. 准备 2.1 安装git:https://git-scm.c ...
- 使用MATPLOTLIB 制图(散点图,热力图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfil ...