415. Add Strings

Easy

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

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
package leetcode.easy;

public class AddStrings {
public String addStrings(String num1, String num2) {
final int m = num1.length();
final int n = num2.length();
int p1 = m - 1, p2 = n - 1;
int carry = 0;
StringBuilder sb = new StringBuilder();
while (p1 >= 0 || p2 >= 0 || carry > 0) {
int sum = carry + (p1 >= 0 ? num1.charAt(p1) - '0' : 0) + (p2 >= 0 ? num2.charAt(p2) - '0' : 0);
carry = sum / 10;
sb.append((char) (sum % 10 + '0'));
p1--;
p2--;
}
return sb.reverse().toString();
} @org.junit.Test
public void test() {
System.out.println(addStrings("0", "0"));
}
}

LeetCode_415. Add Strings的更多相关文章

  1. 【leetcode】415. Add Strings

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

  2. 36. leetcode 415. Add Strings

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

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

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

  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] Add Strings 字符串相加

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

  7. LeetCode Add Strings

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

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

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

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

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

随机推荐

  1. Linux虚拟机安装(rhel 7.4)

    Linux虚拟机安装(rhel 7.4) linux 1. 创建虚拟机 1.1. 新建虚拟机 1.2. 启动虚拟机 附录:部分配置 1. 创建虚拟机 1.1. 新建虚拟机 新建虚拟机 典型虚拟机 稍后 ...

  2. idea中自动deployment的步骤

  3. Python模块(导入,内置,自定义,开源)

    目录: 模块介绍 自定义模块 内置模块 开源模块 一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用p ...

  4. Android打包遇到的问题

    问题一 运行环境 引擎:Unity 4.3.4f1 安卓:Android 6 打包机的环境 出错堆栈 Unity version : 4.3.4f1 Caused by: java.lang.Unsa ...

  5. 使用Arduino开发板和ESP8266从互联网读取数据

    ESP8266-01是一款很强大的模块,可以满足我们任何IOT项目的需求.自发布以来,它已经形成了一个很强大的群体,并演变成一个易于使用.价格低廉且功能强大的Wi-Fi模块.另一个更受欢迎的开源平台是 ...

  6. 项目Beta冲刺(团队)——凡事预则立

    项目Beta冲刺(团队)--凡事预则立 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:Beta冲刺前对冲刺阶段的总体规划 ...

  7. 视觉跟踪:MDnet

    应用需注明原创! 深度学习在2015年中左右基本已经占据了计算机视觉领域中大部分分支,如图像分类.物体检测等等,但迟迟没有视觉跟踪工作公布,2015年底便出现了一篇叫MDNet的论文,致力于用神经网络 ...

  8. 使用docker搭建etcd

    下载etcd代码然后拷贝到服务器 来自为知笔记(Wiz)

  9. 【批处理】if命令,注释方式

    If 命令 if 表示将判断是否符合规定的条件,从而决定执行不同的命令. 有三种格式:1.if "参数" == "字符串" 待执行的命令参数如果等于指定的字符串 ...

  10. vault 使用 中间ca 进行证书管理

    使用vault 进行pki 管理是很方便的,以前测试的都是由根证书进行证书签发,这次使用中间ca 进行签发 所以会有一个证书连 测试使用docker-compose 运行 环境准备 docker-co ...