C#LeetCode刷题之#258-各位相加(Add Digits)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3860 访问。
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
输入: 38
输出: 2
解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。
进阶:
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3860 访问。
public class Program {public static void Main(string[] args) {var n = 38;var res = AddDigits(n);Console.WriteLine(res);n = 513;res = AddDigits2(n);Console.WriteLine(res);n = 66;res = AddDigits3(n);Console.WriteLine(res);Console.ReadKey();}private static int AddDigits(int num) {var length = num.ToString().Length;if(length == 1) return num;var res = 0;while(length > 1) {res = 0;for(var i = 0; i < length; i++) {res += int.Parse(num.ToString()[i].ToString());}num = res;length = num.ToString().Length;}return res;}private static int AddDigits2(int num) {if(num < 10) return num;var temp = num;var sum = 0;while(temp != 0) {sum += temp % 10;temp /= 10;}return AddDigits2(sum);}private static int AddDigits3(int num) {//if(num == 0) return 0;//var result = num % 9;//if(result == 0) return 9;//return result;return num == 0 ? 0 : (num % 9 == 0 ? 9 : num % 9);}}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3860 访问。
2
9
3
分析:
显而易见,AddDigits 和AddDigits2 的时间复杂度为: ,AddDigits3 的时间复杂度为: 。
C#LeetCode刷题之#258-各位相加(Add Digits)相关推荐
- LeetCode第二题:两数相加(Add Two Numbers)
LeetCode第二题:两数相加(python,java) You are given two non-empty linked lists representing two non-negative ...
- LeetCode刷题实战(2):Add Two Numbers
题2描述: 2 Add Two Numbers 29.10% Medium You are given two non-empty linked lists representing two non- ...
- leetCode刷题 2. 两数相加
原题链接: leetcode-cn.com/problems/ad- 题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你 ...
- leetcode刷题 2.两数相加
解法 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() ...
- 学渣的刷题之旅 leetcode刷题 2. 两数相加
给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...
- LeetCode刷题笔记汇总
LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...
- LeetCode刷题记录4——67. Add Binary(easy)
LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
最新文章
- [UIImage imageNamed:@]使用误区
- 容器管理大战:Kubernetes vs.Docker Swarm与Amazon ECS
- NAACL19笔记:自然语言处理应用的实用理解(多图解链接)
- oracle12基本操作
- 操作系统原理:进程与线程、进程生命周期、线程的类型
- “个人核心竞争力”与“危机感”
- 初级Web前端工程师是什么?初级Web前端工程师的技术体系有哪些?
- find命令使用案例
- 生命的密码 —— DNA
- 开发一个React + Electron应用
- Android 高仿微信支付密码输入控件
- 有限元分析基础操作(ANSYS)
- Yolo 一小时学会基本操作
- 厦大计算机科学复试线,2020年厦门大学信息学院考研复试分数线
- 2014 史丰收速算
- 光盘刻录 CD刻录软件 Ashampoo Burning Studio特别版 刻录CD就这么简单
- Eclipse - 取消英文拼写检查
- 开发中关于手机的听筒模式的设置
- 微信营销的技巧有哪些 微信营销取名的禁忌 微信内容写作的七个要点
- 华为路由器 基本ACL配置
热门文章
- 目标检测论文阅读:Cascade R-CNN: Delving into High Quality Object Detection(CVPR2018)
- 第一章 数据挖掘基础
- 【AI视野·今日Robot 机器人论文速览 第十五期】Fri, 25 Jun 2021
- 【python】正则表达式匹配多个模式
- Linux—scp或ssh出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
- Kubernetes-Pod/ReplicaSet/Deployment/Service关系(二十一)
- 02 掌握变量的定义和使用方法 1214
- 类与对象 格式小结 java 1202
- 虚拟环境的常用命令 virtualenv命令
- 索引-python编程技术-第二版