Add Strings
2016-12-10 22:30
183 查看
题目地址:https://leetcode.com/problems/add-strings/
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
题目要将两个数字字符串转换为数字,然后相加,得出结果,再转换为字符串。题目第四条说的很明确,不能用内置的转换方法,要是可以,那不就太简单了么。
题目本身没啥难度,关键是进位处理,这一点都考虑到了,也就没啥问题了。
时间复杂度为:O(max{m, n}),其中m和n分别是两个字符串的长度。
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
题目要将两个数字字符串转换为数字,然后相加,得出结果,再转换为字符串。题目第四条说的很明确,不能用内置的转换方法,要是可以,那不就太简单了么。
题目本身没啥难度,关键是进位处理,这一点都考虑到了,也就没啥问题了。
public class AddStrings { public String addStrings(String num1, String num2) { StringBuilder result = new StringBuilder(""); //进位标记 boolean carry = false; for (int i = num1.length() - 1, j = num2.length() - 1 ; i >= 0 && j >= 0; i--, j--) { int t = 0; if (carry) t = num1.charAt(i) - '0' + num2.charAt(j) - '0' + 1; else t = num1.charAt(i) - '0' + num2.charAt(j) - '0'; if (t >= 10) carry = true; else carry = false; if (carry) result.insert(0, String.valueOf((char)('0' + (t-10)))); else result.insert(0, String.valueOf((char)('0' + t))); } // 不等长的时候,余下的位的计算,注意这里也有可能产生进位。 if (num1.length() > num2.length()) { for (int i = num1.length() - num2.length() - 1; i >= 0; i--) { int t = 0; if (carry) t = num1.charAt(i) - '0' + 1; else t = num1.charAt(i) - '0'; if (t >= 10) carry = true; else carry = false; if (carry) result.insert(0, String.valueOf((char)('0' + (t-10)))); else result.insert(0, String.valueOf((char)('0' + t))); } } if (num2.length() > num1.length()) { for (int i = num2.length() - num1.length() - 1; i >= 0; i--) { int t = 0; if (carry) t = num2.charAt(i) - '0' + 1; else t = num2.charAt(i) - '0'; if (t >= 10) carry = true; else carry = false; if (carry) result.insert(0, String.valueOf((char)('0' + (t-10)))); else result.insert(0, String.valueOf((char)('0' + t))); } } if (carry) result.insert(0, '1'); return result.toString(); } public static void main(String[] args) { AddStrings addStrings = new AddStrings(); System.out.println(addStrings.addStrings("999929", "72")); } }
时间复杂度为:O(max{m, n}),其中m和n分别是两个字符串的长度。
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Redis02 使用Redis数据库(String类型)全面解析
- PostgreSQL ERROR: invalid escape string 解决办法
- C#中Byte[]和String之间转换的方法
- C++中sting类的简单实现方法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- 关于C++ string和c类型字符数组的对比
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享