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 和 ...
随机推荐
- Android课程---简单的音乐播放器
第一个:用Activity实现 activity_music_play1.xml <?xml version="1.0" encoding="utf-8" ...
- js表达式与语句的区别
http://www.2ality.com/2012/09/expressions-vs-statements.html http://www.jb51.net/article/31298.htm 表 ...
- 登陆数据库,界面一直保持正在登陆的状态,oracle使用界面无法登陆
原因:关机时没有关闭oracle窗口. 导致在登陆数据库的时候,使用oracle的这个界面登陆时,界面一直保持''正在登陆''的状态,一旦点击就会卡住,使界面变得无法响应. 然后使用sqlplus仍然 ...
- Javascript基础回顾 之(三) 面向对象
本来是要继续由浅入深表达式系列最后一篇的,但是最近团队突然就忙起来了,从来没有过的忙!不过喜欢表达式的朋友请放心,已经在写了:) 在工作当中发现大家对Javascript的一些基本原理普遍存在这里或者 ...
- 你写的Try...Catch真的有必要么?
很多人喜欢用Try...Catch把每一个方法都包裹起来,可是真的有必要么? 为什么要这样做?我估计是大家被BUG吓怕了,生怕生产环境出现各种莫名其妙的错误,比如最经典的NullReferenceEx ...
- Worktile协同特色之一:无处不在的关注
团队沟通中常见问题 在回答这个问题之前,我们不妨先来思考一下,团队成员之间互相配合沟通的几个问题:1. 任务的执行者在完成这个任务时,如何通知到此任务相关联的其他成员,比如该任务的后续任务执行者?2. ...
- 当前不会命中断点。源代码与原始版本不同 (VS2012)
遇到“当前不会命中断点.源代码与原始版本不同”的问题. 在网上查的类似: 一般studio会提示将“工具”,“选项”,“调试”,“要求源文件与原始版本完成匹配”去掉勾.但是这个配置去掉治标不治本,错误 ...
- ajax的使用:例题、ajax的数据处理
需要注意的是,调用的封装的数据库,和jQuery的保存地址 一.注册 (1)写文本框来进行用户名的验证 <input type="text" id="uid&quo ...
- Java Web应用的开发环境配置
1:第一是下载好Eclipse开发工具,这里不做叙述,自行下载安装. 2:使用Eclipse开发WEB项目,启动Eclipse,选择File--->new --->other---> ...
- OGNL相关代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...