问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. 转:Oracle 数据泵详解

    一.EXPDP和IMPDP使用说明 Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复. 2 ...

  2. Python 中的面向对象编程

    面向对象编程(Object-oriented programming, OOP)是一种基于对象概念的编程范式,可包含属性(attribute)形式的数据以及方法(method)形式的代码.另一种对 O ...

  3. 从LocalDateTime序列化探讨全局一致性序列化

    日拱一卒无有尽,功不唐捐终入海. 楔子 前两周发了三篇SpringSecurity和一篇征文,这周打算写点简单有用易上手的文章,换换脑子,休息一下. 今天要写的是这篇:从LocalDateTime序列 ...

  4. WEB控件及HTML服务端控件能否调用客户端方法?如果能,请解释如何调用

    例如:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <INPU ...

  5. Unity 基于excel2json批处理读取Excel表并反序列化

    excel2json是一款将Excel表格文件快速生成json和C#数据类的高效插件,详情了解如下: https://neil3d.github.io/coding/excel2json.html 该 ...

  6. 使样式只在webkit内核生效

    @media screen and (-webkit-min-device-pixel-ratio:0){ .do someting{ } }     使用媒体查询,制定样式

  7. 使用java实现希表的基础功能

    用java代码完成哈希表数据结构的简单实现, 以公司雇员的添加修改作为模拟实例 具体代码如下: package com.seizedays.hashtable; import java.util.Sc ...

  8. 下载spring的路径的文章,已经试用没问题

    文章:https://blog.csdn.net/ethan__xu/article/details/80273249 spring jar下载路径 http://repo.spring.io/rel ...

  9. 一个edit的学习笔记

    https://blog.csdn.net/woshizoe/article/details/51555396

  10. Django序列化组件Serializers详解

    本文主要系统性的讲解django rest framwork 序列化组件的使用,基本看完可以解决工作中序列化90%的问题,写作参考官方文档https://www.django-rest-framewo ...