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的更多相关文章

  1. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  4. 447. Add Strings

    原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...

  5. LeetCode——Add Strings

    LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...

  6. LeetCode_415. Add Strings

    415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...

  7. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  8. LeetCode Add Strings

    原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2  ...

  9. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  10. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

随机推荐

  1. [前端神器]handlebars+requirejs基本使用方法

    最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...

  2. APS高级计划排程系统应该支持的企业应用场景

    APS高级计划排程系统应该支持的企业应用场景 面对工业4.0智能制造的挑战,很多企业希望能够引进APS高级计划排程系统,全自动的.快速的制定精细化的生产计划,准确的计算产线/设备上各种产品型号的加工顺 ...

  3. MVC开发人员必备的五大工具

    1. Chirpy Zippy 下载地址:http://chirpy.codeplex.com/ 过去,在将JavaScript脚本和CSS文件部署到Web服务器之 前,我习惯使用压缩工具进行压缩再部 ...

  4. windows环境下模仿Linux环境发起curl请求

    1.到官网下载curl工具包 2.在curl.exe目录中使用(使用方式1) 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的目录. 由于博主使用的是windows 64位 的系统,因 ...

  5. 洛谷P3639 [APIO2013] 道路费用 [生成树的特殊算法]

    题目传送门 道路费用 格式难调,题面就不放了. 分析: 这是一道要细(yan)心(jing)的生成树的好(gui)题. 首先我们看到$k$的范围非常小,那么我们就可以直接$2^k$枚举每一条加边是否选 ...

  6. C#多线程编程实战(一):线程基础

    1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...

  7. Redis学习篇(七)之事务

    Redis中的事务 开启事务 MULTI:开启事务,事务块中多条语句会按照顺序放入队列当中,最后由EXEC来执行 MULTI INCT counter1 INCR counter2 INCR coun ...

  8. 山东省第四届省赛 E-Mountain Subsequences

    Description Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees ...

  9. [BZOJ4542] [JZYZOJ2014][Hnoi2016] 大数(莫队+离散化)

    正经题解在最下面 http://blog.csdn.net/qq_32739495/article/details/51286548 写的时候看了大神的题解[就是上面那个网址],看到下面这段话 观察题 ...

  10. bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...