【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 ...
随机推荐
- angularjs ui-view多视口多层嵌套路由配置
最近研究了一下ui-view多层嵌套,整理了一下 1.最简单的ui-view用法 html部分: <ul class="nav navbar-nav"> <li ...
- opencv3.2.0图像处理之中值滤波medianBlur API函数
/*中值滤波:medianBlur函数是非线性滤波 函数原型:void medianBlur(inputArray src,OutputArray dst,int ksize) 参数详解: input ...
- 【node】用koa搭建一个增删改服务(一)
前文,vue分类里有一个日志demo的练习,这篇文章就是介绍针对日志demo的服务是怎么写的 一.koa搭建项目 1. npm init 2. npm install koa 二.建数据库 下面是项目 ...
- Android上实现视频录制
首先,我们肯定要用到摄像头,因此需要在Manifest文件中声明使用权限: <uses-permission android:name="android.permission.CAME ...
- Android 原生 MediaPlayer 和 MediaCodec 的区别和联系(二)
目录: (3)Android 官方网站 对 MediaPlayer的介绍 正文: Android 官方网站 对 MediaPlayer的介绍 MediaPlayer pub ...
- hornor8改user模式为debug模式
在学习Android软件安全的过程中,经常要用到Android的动态调试.但是呢,一般的Android应用在发布的时候都是发布版的不能直接被调试,为了能使Android应用能够支持调试就需要对Andr ...
- CSS 小结笔记之盒子模型
网页标签可以看成是一个个盒子,页面设计就像垒积木一样,在网页中将盒子摆好显示出来.在浏览器中可以很清楚的去看到一个标签的盒子,具体方法如下: 打开浏览器的开发人员工具,在Elements中选中一个标签 ...
- Leetcode题解之Container With Most Water
1.题目描述 2.题目分析 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一 ...
- windows下查看端口占用以及进程名称
http://www.cnblogs.com/rollenholt/archive/2012/08/17/2644657.html
- Ajax 请求下载 Execl 文件
通过Ajax请求下载Execl 的问题,掉进一个坑里半个多小时,特此来记录一下 . 起初 我误以为是后台的问题,然而调试了一下并不是这样的,也不会报错,且进入了success 函数. 以下的事件及请 ...