LeetCode - 415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both
num1andnum2is < 5100. - Both
num1andnum2contains only digits0-9. - Both
num1andnum2does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
import java.util.*;
public class Solution {
public String addStrings(String num1, String num2) {
if (num1.length() == 1 && num2.length() == 1) {
return (int)(((int)num1.charAt(0) + (int)num2.charAt(0)) - 2*(int)'0')+"";
}
int maxLen = Math.max(num1.length(), num2.length());
int[] numArr1 = new int[maxLen];
int[] numArr2 = new int[maxLen];
for (int i=0; i<num1.length(); i++) {
numArr1[i] = num1.charAt(num1.length()-i-1) - '0';
}
for (int i=0; i<num2.length(); i++) {
numArr2[i] = num2.charAt(num2.length()-i-1) - '0';
}
char[] sum = new char[maxLen+1];
int carry = 0;
for (int i=0; i<maxLen; i++) {
sum[i] = (char)((numArr1[i] + numArr2[i] + carry)%10 + (int)'0');
carry = (numArr1[i] + numArr2[i] + carry) / 10;
}
sum[maxLen] = (char)(carry+'0');
int noz = maxLen;
while (sum[noz--] == '0');
StringBuilder ret = new StringBuilder();
for (int i=noz+1; i>=0; i--) {
ret.append(sum[i]);
}
return ret.toString();
}
}
LeetCode - 415. Add Strings的更多相关文章
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [leetcode]415. Add Strings字符串相加
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- [LeetCode&Python] Problem 415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 415 Add Strings 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和 ...
随机推荐
- jquery mobile 问问多多
jquery mobile 问题多多,兼容性太差.android4.1下完全崩溃.以后再也不用jquery mobile了
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- Wine——在Linux上运行Windows软件
官网:https://www.winehq.org/ 参考: wikipedia 教你使用Wine在Linux上运行Windows软件 如何安装和使用Wine,以便在Linux上运行Windows应用 ...
- Sort简单排序
List<T> 类型排序: public List<ProductionMaterialModel> OrderBybom(List<ProductionMaterial ...
- jQuery.Callbacks之demo
jQuery.Callbacks是jquery在1.7版本之后加入的,是从1.6版中的_Deferred对象中抽离的,主要用来进行函数队列的add.remove.fire.lock等操作,并提供onc ...
- iOS开发系列--C语言之基础知识
概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(iphone/ipad) Swift ...
- 初试ASP.NET Web API/MVC API(附Demo)
写在前面 HTTP RESTful 创建Web API 调用Web API 运行截图及Demo下载 ASP.NET Web API是一个框架,可以很容易构建达成了广泛的HTTP服务客户端,包括浏览 ...
- [ZigBee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁分析(下)
说在前面:上一篇介绍了无线LED闪烁实现的OSAL部分,本篇介绍如何实现无线数据收发及数据处理: 上一篇是用SI跟着流程查看源码,我个人认为以架构的思维去了解代码能让人更清晰 ::ZMain.c程序入 ...
- EF:根据实体类生成表结构SQL
根据实体类生成表结构SQL: PM> Enable-Migrations -ProjectName Domain -StartUpProjectName Handler -Force PM> ...
- [大数据之Spark]——Transformations转换入门经典实例
Spark相比于Mapreduce的一大优势就是提供了很多的方法,可以直接使用:另一个优势就是执行速度快,这要得益于DAG的调度,想要理解这个调度规则,还要理解函数之间的依赖关系. 本篇就着重描述下S ...