题目描述:

解决思路:

  此题较简单,和前面【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的更多相关文章

  1. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

  2. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  3. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  4. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  5. 【hash】Power Strings

    [题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...

  6. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  7. 【leetcode】Isomorphic Strings(easy)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. 【转】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 ...

  9. 【Genymotion】add a new virtual device 失败

    Genymotion 新增虚拟设备(模拟器)时,由于网络原因,总是下载失败,如图: 下载失败提示“Unable to create virtual device: Connection timeout ...

随机推荐

  1. PHP通过api上传图片

    参考:接口实现图片上传 提交端: $url="localhost:805/rdyc/123.jpg"; $img=file_get_contents($url); $img_api ...

  2. 初识js-charts和E-charts

    在前端开发的过程中,经常会使用到图表相关的东西,很多时候,图表在展示数据方面有着无与伦比的优势.下面我们就来看看两个常用的图表相关的插件jscharts和ECharts.前者,功能相对单一,但是不依赖 ...

  3. git下配置github sshkey

    教程看这里 http://xiaxveliang.blog.163.com/blog/static/29708034201341244759225/

  4. 跳过ssh在首次连接出现检查keys 的提示

    1.将需要登陆主机得公钥添加到known_hosts ssh-keyscan 192.168.77.129 192.168.77.130 >> /root/.ssh/known_hosts ...

  5. leetCode题解之字符最短路径解法2

    1.题目描述 2.分析 之前使用的大循环再向两边寻找的算法是 O(n^2)复杂度的,可以利用 multimap降低其复杂度. 3.代码 vector<int> shortestToChar ...

  6. js实现字符串一个一个依次显示

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  7. November 21st 2016 Week 48th Monday

    A bird is known by its note, and a man by his talk. 闻其声而知鸟,听其言而知人. Listen to what a man talks, watch ...

  8. Echarts 曲线数少于图例数解决方法

    在上一篇文章 Echarts 多曲线“断点”问题解决方法 中说到了Angular 项目中要使用 Echarts 的方法. 说明了自己解决当“每一条曲线的横坐标不相同”时,在各条曲线上,它们的值采用数组 ...

  9. codeforces 933D A Creative Cutout

    题目链接 正解:组合数学. 充满套路与细节的一道题.. 首先我们显然要考虑每个点的贡献(我就不信你能把$f$给筛出来 那么对于一个点$(x,y)$,我们设$L=x^{2}+y^{2}$,那么它的贡献就 ...

  10. python第十二课——for in循环

    1.for...in循环: 有两个使用场景: 场景一:for in和range对象配合使用 range对象的引入讲解 格式:range([start,end,step]): 特点:索引满足含头不含尾的 ...