问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3873 访问。

给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。

注意:

  1. num1 和num2 的长度都小于 5100.
  2. num1 和num2 都只包含数字 0-9.
  3. num1 和num2 都不包含任何前导零。
  4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

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.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3873 访问。

public class Program {

    public static void Main(string[] args) {
var num1 = "392";
var num2 = "19823"; var res = AddStrings(num1, num2);
Console.WriteLine(res); Console.ReadKey();
} private static string AddStrings(string num1, string num2) {
//定义一个大1位的数组
var max = Math.Max(num1.Length, num2.Length) + 1;
var total = new int[max];
//向左补0到相同数量,否则需要额外处理边界
num1 = num1.PadLeft(max, '0');
num2 = num2.PadLeft(max, '0');
//进位标识
var carry = false;
var sum = 0;
for(int i = max - 1; i >= 0; i--) {
sum = int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString());
if(carry) sum++;
total[i] = int.Parse(sum.ToString()[sum.ToString().Length - 1].ToString());
carry = sum > 9;
}
//定义结果
var res = string.Empty;
//遍历输出到res
total.ToList().ForEach(r => { res += r.ToString(); });
//取消最高位0
res = res.TrimStart('0');
//如果空了,返回0
if(res.Length == 0) res = "0";
//返回结果
return res;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3873 访问。

20215

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#415-字符串相加(Add Strings)的更多相关文章

  1. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  2. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  3. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  4. C#LeetCode刷题之#258-各位相加(Add Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...

  5. leetcode刷题笔记258 各位相加

    题目描述: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...

  6. [Swift]LeetCode415. 字符串相加 | Add Strings

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

  7. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  9. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

  10. Java实现 LeetCode 415 字符串相加

    415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num ...

随机推荐

  1. 题解 CF1354D 【Multiset】

    考试拿到题,一看,这不是权值线段树吗? 思路 使用线段树每个节点维护该区间内元素出现次数. 根据题目,对于加入.删除元素,我们可以单点修改(\(+1\).\(-1\)),对于输出,我们可 随便 遍历找 ...

  2. awesome : vue-awesome 按需引入

    其实挺简单的,被文档带到沟里去了. main.js: 首先讲一下全部引入,很简单. import 'vue-awesome/icons' import Icon from 'vue-awesome/c ...

  3. vue : 本地调试跨域问题的解决办法:proxyTable

    本来我是不想写的,但为了加深印象还是写一写吧. ./config/index.js module.exports = { dev: { // Paths assetsSubDirectory: 'st ...

  4. OneinStack - 自动编译环境安装脚本

    https://oneinstack.com/

  5. spring-cloud-alibaba-sentinel和feign配合使用,启动报Caused by: java.lang.AbstractMethodError: com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata(Ljava/lang/Class;)Ljava/util/List

    背景 我在学习spring-cloud-alibaba技术栈期间,在学习服务熔断与限流的时候,服务启动发生了以下异常 #这是控制台最上面的 sun.misc.Unsafe.park(Native Me ...

  6. VIM的常用快捷方式(尽量简洁,删去能组合实现的且不易记的)

    vi可以分为三种状态,分别是一般模式.编辑模式和命令行模式 1一般模式:以vi打开一个文件就直接进入一般模式了(这是默认的模式).在这个模式中, 你可以使用上下左右按键来移动光标,你可以使用删除字符或 ...

  7. Spring Data JPA根据属性名查询

    https://blog.csdn.net/chengqiuming/article/details/82528961

  8. Bug--Mybatis报错:There is no getter for property named 'id' in 'class java.lang.Integer'

    Mybatis 添加@Param("")注释就可以了

  9. Laravel 定时任务调度 的 Artisan 命令调度

    1.创建命令 php artisan make:command command_name --command=artisan_command_name # Explanation: # command ...

  10. zookeeper 源码编译

    环境:mac 1.github上下载 源码 项目地址:https://github.com/apache/zookeeper 2.安装 ant mac:brew update ->  brew ...