力扣(LeetCode)415. 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
注意:
num1 和num2 的长度都小于 5100.
num1 和num2 都只包含数字 0-9.
num1 和num2 都不包含任何前导零。
你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
java 基础版
class Solution {
public String addStrings(String num1, String num2) {
String res = "";
int s1c = num1.length();
int s2c = num2.length();
if(s1c>=s2c) {
int diff = s1c-s2c;
String temp = "";
for(int i=0;i<diff;i++) {
temp += "0";
}
num2 = temp+num2;
char[] cs1 = num1.toCharArray();
char[] cs2 = num2.toCharArray();
int flag=0;
for(int i = cs1.length-1;i>=0;i--) {
System.out.println((cs1[i]-48+cs2[i]-48+flag));
if(flag+cs1[i]-48+cs2[i]-48>=10) {
res = (flag+cs1[i]-48+cs2[i]-48-10) + res;
flag=1;
}else {
res = (flag+cs1[i]-48+cs2[i]-48) + res;
flag=0;
}
}
if(flag == 1 ) {
res = 1+res;
}
}else {
int diff = s2c-s1c;
String temp = "";
for(int i=0;i<diff;i++) {
temp += "0";
}
num1 = temp+num1;
char[] cs1 = num1.toCharArray();
char[] cs2 = num2.toCharArray();
int flag=0;
for(int i = cs1.length-1;i>=0;i--) {
System.out.println((cs1[i]-48+cs2[i]-48+flag));
if(flag+cs1[i]-48+cs2[i]-48>=10) {
res = (flag+cs1[i]-48+cs2[i]-48-10) + res;
flag=1;
}else {
res = (flag+cs1[i]-48+cs2[i]-48) + res;
flag=0;
}
}
if(flag == 1 ) {
res = 1+res;
}
}
return res;
}
}
java 升级版
字符串加法、链表加法、二进制加法 都可以如此做
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder build = new StringBuilder();
int s1c = num1.length()-1;
int s2c = num2.length()-1;
int carry = 0; //进位
while(s1c>=0 || s2c>=0 || carry !=0) {
if(s1c>=0) {
carry += num1.charAt(s1c--)-'0';
}
if(s2c>=0) {
carry += num2.charAt(s2c--)-'0';
}
build.append(carry%10); //各位数字
carry /=10; // 进位数字
}
return build.reverse().toString();
}
}
运行结果

力扣(LeetCode)415. 字符串相加的更多相关文章
- Java实现 LeetCode 415 字符串相加
415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num ...
- [LeetCode]415. 字符串相加、43. 字符串相乘
题目 415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 题解 维护一个temp表示当前两数相加+上一个进位的和. 每次更新结果的一位. 注意终止条件. 最后将 ...
- 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数
最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...
- LeetCode:字符串相加【415】
LeetCode:字符串相加[415] 题目描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100.num1 和num2 都只 ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- 刷题-力扣-541. 反转字符串 II
541. 反转字符串 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-string-ii 著作权归领扣网络所有. ...
- 【力扣leetcode】-787. K站中转内最便宜的航班
题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...
- 力扣Leetcode 面试题56 - I. 数组中数字出现的次数
面试题56 - I. 数组中数字出现的次数 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 ...
- 领扣(LeetCode)字符串相加 个人题解
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...
随机推荐
- 开启redis-server提示 # Creating Server TCP listening socket *:6379: bind: Address already in use--解决方法
在bin目录中开启Redis服务器,完整提示如下: 3496:C 25 Apr 00:56:48.717 # Warning: no config file specified, using the ...
- Pytorch的torch.cat实例
import torch 通过 help((torch.cat)) 可以查看 cat 的用法 cat(seq,dim,out=None) 其中 seq表示要连接的两个序列,以元组的形式给出,例如:se ...
- bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...
- tcp编程 示例
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...
- cscope for golang
从 https://gist.github.com/bopjiang/11146574 下载, 做了修改. cscope-go.sh #!/bin/bash # generate cscope ind ...
- Thinkphp5 分页带参数
原文链接:http://www.zhaisui.com/article/51.html
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- batchGetAnchorLevel(dubbo接口)
一.编写脚本前的准备工作 1.安装idea,安装本地maven库,并在idea里面配置maven 2.导入git源码(目的在于下载所依赖的基础包)-->File-new-Project from ...
- 纯注解方式配置spring+springMVC
1.新建类initConfig,继承AbstractAnnotationConfigDispatcherServletInitializer,并重写getRootConfigClasses().get ...
- 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!
mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...