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
num1andnum2is < 5100. - Both
num1andnum2contains only digits0-9. - Both
num1andnum2does 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. ...
随机推荐
- di
dependency inversion依赖反转和dependency inversion依赖注入 di反转指的是不依赖于具体的实现而是依赖于抽象的接口,那么运行应用的main方法里如果创造具体实现的 ...
- Mac 下面,添加android adb命令(一般环境变量的添加方法)
1. 在 用户目录下 ~/.bash_profile vim ~/.bash_profile 2.加入我们需要添加的环境变量,这里是 添加 android platform-tools 和 ...
- 转:C#生成唯一值的方法汇总
这篇文章主要介绍了C#生成唯一值的方法汇总,有需要的朋友可以参考一下 生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: 一.在 .NET ...
- Web工程师必备的43款可视化工具
国外站点DATAVISUALIZATION.CH为大家总结出了当前热用的43款可视化工具,包括Arbor.Chroma.js.D3.js.Google Chart Tools等,绝对让你一饱眼福. 1 ...
- CodeForces 610C Harmony Analysis
构造 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...
- CCS设计手段——相对定位
1.认识相对定位 相对定位就是让元素相对自己原来的位置进行位置调整. 2.相对定位的本质特性 不脱标,老家留坑,形影分离. 3.用途 ①微调位置 ②做绝对定位的参考,子绝父相 4.相对定位的定位置 相 ...
- Tesseract-OCR使用记录
Tesseract是一个开源的OCR(Optical Character Recognition,光学字符识别)引擎,可以识别多种格式的图像文件并将其转换成文本,目前已支持60多种语言(包括中文). ...
- Apache 隐藏入口文件 index.php
新建 .htaccess文件至站点目录下,并写入如下代码: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQ ...
- UIView Methods
UIView翻译 (参考) 2011年04月12日 星期二 10:09 转载于:http://blog.csdn.net/tracylife/archive/2010/08/27/5842723.as ...
- iOS给自定义个model排序
今天有朋友问我怎么给Model排序,我顺便写了一个,伸手党直接复制吧. 例如,我建了一个Person类,要按Person的年龄属性排序: Person *per = [[Person alloc] i ...