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 和 ...
随机推荐
- fastJson使用
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,由阿里巴巴的工程师开发. 主要特点: 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson ...
- #iOS问题记录# 关于UITableViewcel的分割线去掉问题
十分清楚的记得以前在使用的时候,通过[_mTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];这一句话来达到效果的. 这次怎么 ...
- nodejs学习之events的使用
实用events做个小例子: var mysql = require("mysql"); var Event = require("events").Event ...
- Java_正则表达式
1.简介 正则表达式是一串特定字符串,组成一个“规则字符串”,这个“规则字符串”是描述文本规则的工具.正则表达式就是记录文本规则的代码. 2.字符集合 [abc] a,b,c中任意一个字符 [^abc ...
- Struts2登录小例子
前面实现了一个数据显示的例子,下面我来实现以下使用Struts2登录 首先是配置不用过多解释 注意名字要和类名保持一致 因为实现的是action这个方法所以需要用action.log来跳转到类里面 解 ...
- Ring0打开其他设备对象三种方式整理
1.通过ZwCreateFile打开其他设备的Handle,此函数只能得到句柄.ZwCreateFile与NtCreateFile区别在于NtCreateFile更加底层,ZwCreateFile是基 ...
- C语言中的插入排序(2016-12-30)
直接插入排序: 算法思想:假设待排序的记录存放在数组R[1--n]中,初始时,i=1,R[1]自成一个有序区,无序区为R[2--n].然后从i=2起直到i=n,依次将R[i]插入当前的有序区R[1.. ...
- win7 64位下 mongodb安装及命令运行
有网友老催我把框架加上mongodb的支持,于是偶尔抽空看了看相关的文章. 今天有缘,就把mongodb安装了一下,中间遇到了小小的问题,So,把整个过程记录一下: 1:先上官网:http://www ...
- 最流行的编程语言 JavaScript 能做什么?
此文转载oschina文章 首先很遗憾的一点是,“PHP虽然是最好的语言”,但是它不是最流行的语言. 同时对不起的还有刚刚在4月TIOBE编程语言排行榜上上榜的各个语言: 你们都很棒,但是你们都担当不 ...
- 3dmax模型制作备忘录
md2导出: http://thegreystudios.com/blog/?p=278 http://wenku.baidu.com/view/4d5652e2524de518964b7d89.ht ...