题目描述:

解决思路:

  此题较简单,和前面【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. OpenGL学习--01--打开一个窗口

    // Include standard headers #include <stdio.h> #include <stdlib.h> // Include GLEW #incl ...

  2. Android属性动画:插值器与估值器

    声明:本篇文章部分内容来自<Android开发艺术探索>. 我们都知道对于属性动画可以对某个属性做动画,而 插值器(TimeInterpolator)和 估值器(TypeEvaluator ...

  3. Android 原生 MediaPlayer 和 MediaCodec 的区别和联系(二)

    目录: (3)Android 官方网站 对 MediaPlayer的介绍 正文:  Android 官方网站 对 MediaPlayer的介绍         MediaPlayer      pub ...

  4. Pig的使用场景

    数据转换加载(ETL)数据流:读取原始数据(比如用户日志),进行数据清洗,进行简单的预计算后导入到数据仓库,比如join连接数据库里的用户信息.

  5. 开源一款私藏Management Studio插件,ProjkyAddin,送给所有使用SQLServer的园友们

    ProjkyAddin 是一款Management Studio 插件,安装包才500多kb,兼容SSMS 2005.SSMS 2008.SSMS 2008 R2.SSMS 2012.SSMS 201 ...

  6. 使用innodb_ruby探查Innodb索引结构

    使用innodb_ruby探查Innodb索引结构 innodb_ruby 是使用 Ruby 编写的 InnoDB 文件格式解析器.innodb_ruby 的目的是暴露一些其他隐藏的 InnoDB 原 ...

  7. 转:JavaBean 、 Serverlet 总结

    Serverlet简介: Servlet(Server Applet),全称Java Servlet,未有中文译文.是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web ...

  8. Windows Pre-commit hook for comment length Subversion

    @echo off :: Stops commits that have empty log messages. @echo off setlocal rem Subversion sends thr ...

  9. AFNetworking 2.5.x 网络请求的封装

    AFNetworking 2.5.x 网络请求的封装 源码地址 https://github.com/YouXianMing/Networking 说明 1. 将block形式的请求转换成用代理来处理 ...

  10. 理解http请求

    HTTP请求的GET方法可以用来抓取网页. HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则,计算机专家设计出HTTP,使HTTP客户(如Web浏览 ...