add-strings
https://leetcode.com/problems/add-strings/ package com.company; import java.util.LinkedList;
import java.util.List;
import java.util.Random; public class Main {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
if (num1.length() > num2.length()) {
String tmp = num1;
num1 = num2;
num2 = tmp;
}
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0;
int ival = 0; for (; i>=0 && j >= 0; i--,j--) {
ival = carry + num1.charAt(i) - '0' + num2.charAt(j) - '0';
if (ival > 9) {
carry = ival / 10;
ival %= 10;
}
else {
carry = 0;
}
sb.append((char)('0' + ival));
} // num2 is always longer than num1
while (j >= 0) {
ival = carry + num2.charAt(j) - '0';
if (ival > 9) {
carry = ival / 10;
ival %= 10;
}
else {
carry = 0;
}
sb.append((char)('0' + ival));
j--;
} // At first, it's missed
if (carry > 0) {
sb.append((char)('0' + carry));
} return sb.reverse().toString();
} public static void main(String[] args) {
// write your code here
System.out.println("Hello"); String num1 = "1";
String num2 = "9";
Main obj = new Main();
String ret = obj.addStrings(num1, num2);
System.out.printf("ret: %s\n", ret); }
}
add-strings的更多相关文章
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- 447. Add Strings
原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- LeetCode_415. Add Strings
415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2 ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
随机推荐
- 【C#】实现INotifyPropertyChanged的3种方法
class StudentItemViewModel:INotifyPropertyChanged { public event PropertyChangedEventHandler Propert ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- Angular部署到windows上
1. 确保已经打开了IIS服务. 如果没有打开可参考 http://jingyan.baidu.com/article/eb9f7b6d9e73d1869364e8d8.html 2. 编译angul ...
- Eclipse中Tomcat 修改后台代码即时生效
修改类后不用重启Tomcat就能自动生效,对于提高开发效率的帮助很大. server.xml 中节点定义时一般会有如下配置: <Context docBase="test1" ...
- shell脚本基本用法
下面是一些简单常用的脚本,工作中可能会用到,记录一下. #!/usr/bin/env bash #变量[=两边不要有空格], 在使用的时候需要用${变量名} 或者是$变量名 name="sa ...
- Django Restframework 实践(二)
按照自己的方法来写接口 ''' @api_view([ 'POST','GET',]) 允许请求的是get或post方法,这里去掉get那么就不能用get方法请求 @permission_classe ...
- BeautifulSoup解析库
解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差 lxml HTML解 ...
- java8新特性——简介
java8问世已经有好长时间了,但是之前项目中都没有使用到,所以一直都只是了解一些,近期刚刚换了家新公司,在开发中需要使用到java8来开发,所以也是马上赶来学习一下java8得新特性. 一.新特性 ...
- BZOJ1226 SDOI2009学校食堂
这题状压DP太神了. g[i][j][k]表示前i-1个人都已打到饭,自己和后七个人打饭的情况是j,当前最后一个打饭的与i的关系是k 如果j&1==1说明当前这个人也打了饭,那么可以转移到g[ ...
- BZOJ4554 HEOI2016游戏
网络流. 我一开始傻傻的将每条边与每一列连边,边权为联通块树,但是这样做是错的因为我们就在跑网络流中会忽略掉边和列的关系. 我们在做网络流题时一定要注意题目中给出的限制条件,一定要以限制条件建图!!! ...