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. python脚本传入参数--精讲(getopt模块)

    1.最常用的sys.argv[],这个不多谈 2.形如 dahu@dahu-OptiPlex-:~/json_folder$ python sub1.py -abb -oaaa --output=ou ...

  2. Visual Studio2017如何设置自动生成的代码不换行

  3. merc_timer_handle_t函数的使用

    merc_timer_handle_t,是定义一个时间类型,这个时间类型可以用来接收2个函数之间的wasted time 但是在项目中出现这个情况: 因为在脚本中添加了该函数:

  4. 【Spark亚太研究院系列丛书】Spark实战高手之路-第2章动手实战Scala第3小节:动手实战Scala函数式编程(2)

    3,动手实战Scala中的泛型 泛型泛型类和泛型方法,也就是我们实例化类或者调用方法的时候可以指定其类型,由于Scala的泛型和Java的泛型是一致的,这里不再赘述. 4,动手实战Scala中的隐式转 ...

  5. 编译PHP并与Ngnix整合

    nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管 ...

  6. UVA11987 Almost Union-Find [带权并查集]

    洛谷传送门 Almost Union-Find 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 5 7 1 1 2 2 3 4 1 3 5 3 4 2 4 1 3 4 3 ...

  7. Mybatis源码分析之参数处理

    Mybatis对参数的处理是值得推敲的,不然在使用的过程中对发生的一系列错误直接懵逼了. 以前遇到参数绑定相关的错误我就是直接给加@param注解,也稀里糊涂地解决了,但是后来遇到了一些问题推翻了我的 ...

  8. Appium robotframework-appium (ios 客户端测试)环境搭建

    一. 简介 1.1摘要 本人测试新人,最近在搞ios客户端的自动化,准备采用robotframework-appium来实现自动化测试,一边学习一边总结,此安装说明文档是基于mac系统10.11版本, ...

  9. PHP视频教程 字符串处理函数(三)

    字符串替换函数: str_replace() 替换字符串或数组元素,区分大小,第四个参数可选用于统计替换次数. str_ireplace() 不区分大小写替换 字符串函数比较 strcmp()比较字符 ...

  10. 【BZOJ 4663】 (最小割)

    4663: Hack Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 69  Solved: 26 Description 由于 FZYZ 教学区禁止使 ...