415. Add Strings
没什么限定的话,先翻转,在一位一位加,记得进位就行了。。
public class Solution
{
public String addStrings(String num1, String num2)
{
StringBuilder sb = new StringBuilder(num1);
num1 = sb.reverse().toString();
sb = new StringBuilder(num2);
num2 = sb.reverse().toString();
if(num1.length() > num2.length())
{
String temp = num1;
num1 = num2;
num2 = temp;
}
String res = new String();
int carry = 0;
for(int i = 0; i < num1.length();i++)
{
int val = num1.charAt(i)+num2.charAt(i)- '0'-'0'+ carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+=Integer.toString(val);
}
for(int i = num1.length(); i < num2.length();i++)
{
int val = num2.charAt(i) - '0' + carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+= Integer.toString(val);
}
if(carry == 1) res += 1;
sb = new StringBuilder(res);
return sb.reverse().toString();
}
}
415. Add Strings的更多相关文章
- 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 && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- LeetCode - 415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode&Python] Problem 415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- [leetcode]415. Add Strings字符串相加
Given two non-negative integers 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 和 ...
随机推荐
- 对有状态bean和无状态bean的理解(转)
现实中,很多朋友对两种session bean存在误解,认为有状态是实例一直存在,保存每次调用后的状态,并对下一次调用起作用,而认为无状态是每次调用实例化一次,不保留用户信息.仔细分析并用实践检验后, ...
- 从1到n整数中1出现的次数
题目如题 如 5 中1出现的次数 为1 12中1出现的次数为5 public class NumberOf1Between1AndN { /* *输入一个整数n,求从1到n这N个十进制表示中1出现的次 ...
- SGU Volume 2
SGU200.Cracking RSA------------------------★高斯消元 SGU207.Robbers -------------------------------数论 SG ...
- [Linux]命令行模式切换
图形界面进入命令:Ctrl+Alt+T 进入终端命令:Ctrl+Alt+F1 or Ctrl+Alt+F2 切换至图形界面:按Alt+F7
- yarn源代码
Modules-------YARN consists of multiple modules. The modules are listed below as per the directory s ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 推荐用于格式化以及高亮显示SQL文的PHP类-SqlFormatter
有时候做开发用到SQL文的时候,由于sql文太长,很难真正看清楚这个sql文的结构. 所以需要一个工具能够自动对SQL文进行排版,在网上有幸找到这个用php写的类能处理这个任务. 原文地址是http: ...
- nutch 1.7 导入 eclipse
开发环境建议:ubuntu+eclipse (windows + cygwin + eclipse不推荐) 第一步:下载http://archive.apache.org/dist/nutch/从上述 ...
- Android模拟器常用命令收录
一.Linux命令 1.挂载/systme分区为读写状态 mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system 2.切换为Root用户 ...
- wordpress 如何从后台数据库修改theme(图文教程)
我们在wordpress主题theme配置的时候,会从网站上下载比较流行的theme,使自己的blog看着很酷,也有不顺利的时候,你下载的theme有bug或者下载包出问题了,安装过后你的web页面不 ...