【LeetCode415】Add Strings
题目描述:

解决思路:
此题较简单,和前面【LeetCode67】方法一样。
Java代码:
public class LeetCode415 {
public static void main(String[] args) {
String a="1",b="9";
System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b));
}
}
class Solution {
public String addStrings(String num1, String num2) {
int len1=num1.length(),len2=num2.length();
int i=len1-1,j=len2-1;
int carry=0;
StringBuilder sb=new StringBuilder();
while(i>=0||j>=0){
int sum=carry;
if(i>=0) sum+=num1.charAt(i--)-'0';
if(j>=0) sum+=num2.charAt(j--)-'0';
sb.append(sum%10);
carry=sum/10;
}
if(carry==1) sb.append(carry);
return (sb.length()==0?"0":sb.reverse().toString());
}
}
程序结果:

【LeetCode415】Add Strings的更多相关文章
- 【LeetCode67】 Add Binary
题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- 【POJ2406】【KMP】Power Strings
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- 【hash】Power Strings
[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【转】Split strings the right way – or the next best way
I know many people are bored of the string splitting problem, but it still seems to come up almost ...
- 【Genymotion】add a new virtual device 失败
Genymotion 新增虚拟设备(模拟器)时,由于网络原因,总是下载失败,如图: 下载失败提示“Unable to create virtual device: Connection timeout ...
随机推荐
- git之回退
1:本地已commit,未push到远程仓库 1)git log: 查看commit日志,获取commit的id 2) git reset --hard commit_id: ...
- Centos7配置
1.静态ip配置 1.1 cd /etc/sysconfig/network-scripts/ 1.2 vim ifcfg-ens33 (可通过ls查看 一般为第一个) (网关DNS1可以通过V ...
- opencv3.2.0形态学滤波之形态学梯度、顶帽、黑帽
/*一.形态学梯度 (1)含义:是膨胀图和腐蚀图之差 (2)数学表达式:dst=morph-grad(src,element) =dilate(src,element) - erode(src,ele ...
- mybatis 一对多 id标签作用
一对多 MyBatis的resultMap只用于配置结果如何映射,id的唯一作用就是在嵌套的映射配置时判断数据是否相同,当配置id标签时,MyBatis只需要逐条比较所有数据中id标签字段值是否相同即 ...
- Django 模板继承extend 标签include block
# block 站网页位置# includ 导入网页标签# extends 导入网页模板 # common_js.html <script src="/static/plugins/j ...
- clean-css
What is clean-css? Clean-css is a fast and efficient Node.js library for minifying CSS files. Accord ...
- SQL Server FOR XML PATH 和 STUFF函数的用法
FOR XML PATH ,其实它就是将查询结果集以XML形式展现,将多行的结果,展示在同一行. 下面我们来写一个例子: 假设我们有个工作流程表: CREATE TABLE [dbo].[Workfl ...
- 转: 根据屏幕分辨率,浏览器调用不同css
<link type="text/csss" href="" rel="stylesheet"/> <link type= ...
- 使用CAReplicatorLayer [1]
使用CAReplicatorLayer [1] 说明 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Ref ...
- notepad快捷键大全
Notepad++ 快捷键 大全Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话框 ...