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. 【LOJ】#2129. 「NOI2015」程序自动分析

    题解 开始是想两个并查集的 和A相等,和A不相等 如果AB相等就连 A 相等,B相等 B不相等 A不相等 如果AB不相等就连 A不相等,B相等 B相等,A不相等 但是显然不对,因为和A不相等的不一定和 ...

  2. 记录自己在 cmd 中执行 jar 文件遇到的一些错误

    记录自己在 cmd 中执行 jar 文件遇到的一些错误 场景: 请求接口,解析接口返回的 JSON 字符串并插入到我们的数据库里面. 情况: 项目在 eclipse 中正常运行,打成 jar 包后在 ...

  3. VS code配置docker的shell环境

    今天尝试了下使用docker来做虚拟机,几番折腾后终于搞定可以用了,但是想着每次都要在命令行敲半天也太恶心了,所以就找了一下可视化的管理工具 首先说下,我的docker主机环境是windows10,用 ...

  4. 红黑树(RB Tree)

    看到一篇很好的文章 文章来源:http://www.360doc.com/content/15/0730/00/14359545_488262776.shtml 红黑树是一种高效的索引树,多于用关联数 ...

  5. mysql协议简析

    前言 如果要在命令行中连接mysql,最常用的便是 mysql -u root -p 这样指定用户名和密码 当然还可以使用远程连接 mysql -h 127.0.0.1 -u root -p 还有一种 ...

  6. SQL必知必会 -------- 聚集函数、分组排序

    聚集函数 1.AVG()函数 输入:SELECT AVG(prod_price) AS avg_price FROM Products 输出: 警告:只用于单个列AVG()只能用来确定特定数值列的平均 ...

  7. GeneXus项目启动

    使用GeneXus产品开发项目时,在开始,有一些属性我会经常改一下.我现在使用的GeneXus版本是GeneXus U3,由于在做手机应用的开发,所以一般使用最新的版本,老外那边差不多两个月会有一个u ...

  8. Spring JDBC主从数据库访问配置

    通过昨天学习的自定义配置注释的知识,探索了解一下web主从数据库的配置: 背景:主从数据库:主要是数据上的读写分离: 数据库的读写分离的好处? 1. 将读操作和写操作分离到不同的数据库上,避免主服务器 ...

  9. FastReport.Net使用:[34]小册子报表(奇偶页)

    打印一份小册子类型的报表,能实现如下要求: ●单独的封面,目录,报表内容,背面 ●奇偶页不同的页边距 ●奇偶页不同的页面/页脚 下面的例子将用到以上3点. 1.奇偶页的实现主要通过报表控件对象的Pri ...

  10. HDU 1880 简单Hash

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1880] 中文题面,题意很简单: 题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到ma ...