Add Strings Leetcode
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.
这道题不能用long会越界,所以只能用string直接来append。精髓在于设置一个变量来储存进位。
自己先写了一个用boolean型flag来储存进位,结果代码逻辑麻烦极了。。。要考虑到两个数字相加等于9,再加上进位的1等于10的情况,还要考虑到两个数相加,和的长度长于任何一个数的长度的时候,最高位也要加上1。
这是我一开始写的代码,自己都看不下去了。。。
- public class Solution {
- public String addStrings(String num1, String num2) {
- if (num1 == null || num2 == null) {
- return null;
- }
- int sum = 0;
- StringBuilder str = new StringBuilder();
- for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--) {
- int x = i < 0 ? 0 : num1.charAt(i) - '0';
- int y = j < 0 ? 0 : num2.charAt(j) - '0';
- sum = x + y;
- sum = sum % 10;
- if (flag) {
- if (sum + 1 >= 10) {
- flag = true;
- str.append((sum + 1) % 10);
- } else {
- str.append(sum + 1);
- flag = false;
- }
- } else {
- str.append(sum);
- }
- if (x + y >= 10) {
- flag = true;
- }
- }
- if (flag) {
- str.append(1);
- }
- return str.reverse().toString();
- }
- }
但其实有更简洁的办法,用一个int型代表最高位,当它是1的时候继续循环append。
- public class Solution {
- public String addStrings(String num1, String num2) {
- if (num1 == null || num2 == null) {
- return null;
- }
- int carry = 0;
- StringBuilder str = new StringBuilder();
- for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--) {
- int x = i < 0 ? 0 : num1.charAt(i) - '0';
- int y = j < 0 ? 0 : num2.charAt(j) - '0';
- str.append((x + y + carry) % 10);
- carry = (x + y + carry) / 10;
- }
- return str.reverse().toString();
- }
- }
逻辑清晰多了。。。= =
Add Strings Leetcode的更多相关文章
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- LeetCode_415. Add Strings
415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...
- 447. Add Strings
原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers 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. ...
随机推荐
- 我的android学习脚步----------- 的第一个应用
刚刚开始学android开发,以前都是在别人调好的应用中修改JNI,现在需要自己一步步走 开发环境:Eclipse+ADT 配置不多讲了,引自:http://www.cnblogs.com/allen ...
- UIView你知道多少
转载自:http://www.cnblogs.com/likwo/archive/2011/06/18/2084192.html 曾经有人这么说过,在iphone里你看到的,摸到的,都是UIVie ...
- 某技术大牛的帖子(android项目总结)
1. 一般性控件需要设置onclick事件才会有点击效果(selector). 2. <item android:drawable=”@drawable/btn_ct_green” /> ...
- POJ 2296 Map Labeler
二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...
- Python3基础 list(enumerate()) 将一个列表的每一个元素转换成 带索引值的元组
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- javascript 函数 add(1)(2)(3)(4)实现无限极累加 —— 一步一步原理解析
问题:我们有一个需求,用js 实现一个无限极累加的函数, 形如 add(1) //=> 1; add(1)(2) //=> 2; add(1)(2)(3) //=> 6; add ...
- 改变cinder默认vg的方法
在存储节点:# pvcreate /dev/sdb# vgcreate vg100gb /dev/sdb # openstack-config --set /etc/cinder/cinder.con ...
- 伸展二叉树树(C#)
参考过好几篇关于将伸展树的代码,发现看不懂.看图能看懂原理.就尝试自己实现了下. 自顶向上的算法. using System; using System.Collections.Generic; us ...
- ZenCoding 个人理解和总结
我的理解:ZenCoding是一个html简写的语法,可以最快速的生成html. 不少IDE应该都支持,我用的intellij idea是支持的. ZenCoding表示和CSS/JS有相通之处,比如 ...
- linux自动启动程序
下面用自启动apache为例: 有两种方法可以让Apache在系统启动时自动启动 1. 在/etc/rc.d/rc.local中增加启动apache的命令,例如:/usr/local/httpd/ ...