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. linux下php pcntl_fork模拟多线程

    开始用php写后台服务一段时间了.也是在这样的驱动下,不断的学习php语法,体验这一原来一直以为神秘且敬而远之的神奇语言的魅力.最初看php多线程的资料是为了提高程序的处理能力,充分发挥linux多任 ...

  2. Python之路【第六篇】:模块与包

    目录 一 模块 3.1 import 3.2 from ... import... 3.3 把模块当做脚本执行 3.4 模块搜索路径 3.5 编译python文件 3.6  标准模块 3.7  dir ...

  3. poj——1986 Distance Queries

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14392   Accepted: 5066 ...

  4. Linux 下安装gmpy2

    GMP(GNU Multiple Precision Arithmetic Library,即GNU高精度算术运算库),它是一个开源的高精度运算库,其中不但有普通的整数.实数.浮点数的高精度运算,还有 ...

  5. XamarinForms教程构建XamarinForms开发环境

    构建XamarinForms开发环境 所谓Xamarin.Forms的开发环境,就是指在基本硬件和数字软件的基础上,为支持系统软件和应用软件的工程化开发和维护而使用的一组软件,简称SDE.对于任何的程 ...

  6. Linux下burg引导

    用得比较久了,比grub顺手: 安装: sudo add-apt-repository ppa:n-muench/burg; sudo apt-get update; sudo apt-get ins ...

  7. .net中session的使用

    什么是Session? Session即会话,是指一个用户在一段时间内对某一个站点的一次访问. Session对象在.NET中对应HttpSessionState类,表示"会话状态" ...

  8. 【51Nod 1815】【算法马拉松 23】调查任务

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1815 tarjan缩点后在DAG上递推即可. 每个点维护所有根到它的路径 ...

  9. 矩阵乘法<简单总结>

    原理:矩阵相乘最重要的方法是一般矩阵乘积.它只有在第一个矩阵的 行数 和第二个矩阵的 列数 相同时才可进行.若A为m×n矩阵,B为n×p矩阵,则他们的乘积AB会是一个m×p矩阵. 若A=    a   ...

  10. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...