C#LeetCode刷题之#415-字符串相加(Add Strings)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3873 访问。
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
注意:
- num1 和num2 的长度都小于 5100.
- num1 和num2 都只包含数字 0-9.
- num1 和num2 都不包含任何前导零。
- 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both num1 and num2 is < 5100.
- Both num1 and num2 contains only digits 0-9.
- Both num1 and num2 does not contain any leading zero.
- 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)的更多相关文章
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- leetcode刷题2:两数相加add_two_numbers
题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...
- C#LeetCode刷题之#258-各位相加(Add Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...
- leetcode刷题笔记258 各位相加
题目描述: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...
- [Swift]LeetCode415. 字符串相加 | Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- Java实现 LeetCode 415 字符串相加
415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num ...
随机推荐
- 利用cublasHgemm来实现cublasHgemv
前几天做half量化时发现cublas竟然没有提供half版本的矩阵-向量乘,也就是half版本的cublasHgemv.自己写一个又太麻烦,重点是精度和耗时不一定比cublas提供的要好,不过cub ...
- javascript算法 最短路径问题
var obj = { 1: [2, 3], 2: [1, 4, 5], 3: [1, 7, 8], 4: [2, 7], 7: [4, 8], } var 起点 = 1 var 终点 = 8 var ...
- Windows File Recovery - 微软官方文件恢复工具
假如你不小心误删除了文件或因各种意外情况丢失数据后,你可以通过 微软这款工具 这个工具来尝试恢复它们.WinFR 工具支持读取本机硬盘.移动硬盘.U 盘,或者连接相机.手机.使用读卡器来恢复 SD.T ...
- pyinstall打包资源文件
相关代码 main.py import sys import os #生成资源文件目录访问路径 #说明: pyinstaller工具打包的可执行文件,运行时sys.frozen会被设置成True # ...
- 从LocalDateTime序列化探讨全局一致性序列化
日拱一卒无有尽,功不唐捐终入海. 楔子 前两周发了三篇SpringSecurity和一篇征文,这周打算写点简单有用易上手的文章,换换脑子,休息一下. 今天要写的是这篇:从LocalDateTime序列 ...
- 【java面试】- 集合篇
Java 集合概览 从下图可以看出,在Java中除了以Map结尾的类之外, 其他类都实现了Collection接口.并且,以Map结尾的类都实现了Map接口 List.Set.Map三者的区别 Lis ...
- 为什么在SpringBoot+maven的项目中,所引入的依赖包可以不指定依赖的版本号?
当在Springboot项目中引入了spring-boot-starter-parent,则可以不用引入依赖包版本号,比如: <parent> <groupId>org.spr ...
- Redis知识总结
1.什么是Redis Redis是一个nosql(not only sql 不仅仅只有sql)数据库,翻译成中文叫做非关系型数据库,低由C语言开发,数据模型为key-value 关系型数据库:以二维表 ...
- java 将整型数组转化为字符串
java arrays 和arrayList 的区别 package com.vc; import java.util.Arrays; public class Demo05 { public sta ...
- Horse Pro(带负坐标的bfs搜索)
Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...