题目描述:

解决思路:

  此题较简单,和前面【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. JavaScript 常见面试题

    此文内容多为 JavaScript 对数组理解及运用. 1.如何消除一个数组里面重复的元素? var arr = [1, 2, 3, 3, 4, 4, 5, 5, 6, 1]; var newArr ...

  2. java获取本月第一天和最后一天

    public class CalendarTest { public static void main(String[] args) { // 获取当前年份.月份.日期 Calendar cale = ...

  3. 多个raq导出一个excel2007中

     需求描述:         客户以前通过润乾API把多个raq模板数据来导出到一个excel文件中,由于现在数据量过大一个raq数据就超过了65535,原来的2003接口已经满足不了现在的需求, ...

  4. 线程间的通信方式3--Handler

    Android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因 ...

  5. 三、vue如何配置路由 、获取路由的参数、部分刷新页面、缓存页面

        1.路由配置:所有的启动文件都在最初始的main.js文件里面,这个文件中首先需要引入: 2.路由文件配置说明: 3.如何获取页面url的参数? this.$route.query 4.页面之 ...

  6. PyQt4(简单布局)

    import sys from PyQt4 import QtCore, QtGui app = QtGui.QApplication(sys.argv) widget = QtGui.QWidget ...

  7. 团队项目——软件需求分析(NABCD)

    一.团队项目简介 团队名称:SmartCoder 项目名称:<一起> 二.针对 " 地图可视化查看发布的内容 " 这一特点进行 NABCD 分析 N(Need需求) 往 ...

  8. J2EE开发环境--RAP

    J2EE开发环境--RAP J2EE开发环境分四步: 1.JDK环境 2.tomcat 3.redis环境 4.mysql环境 5.RAP包 线上环境,推荐使用源码,自建应用用户,设置对应规则,禁止关 ...

  9. 使用 Azure CLI 创建 Linux 虚拟机

    Azure CLI 用于从命令行或脚本创建和管理 Azure 资源. 本指南详细介绍了如何使用 Azure CLI 部署运行 Ubuntu 服务器的虚拟机. 服务器部署以后,将创建 SSH 连接,并且 ...

  10. java多线程读取、操作List集合

    import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils;   pub ...