问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)相关推荐

  1. LeetCode第二题:两数相加(Add Two Numbers)

    LeetCode第二题:两数相加(python,java) You are given two non-empty linked lists representing two non-negative ...

  2. LeetCode刷题实战(2):Add Two Numbers

    题2描述: 2 Add Two Numbers 29.10% Medium You are given two non-empty linked lists representing two non- ...

  3. leetCode刷题 2. 两数相加

    原题链接: leetcode-cn.com/problems/ad- 题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你 ...

  4. leetcode刷题 2.两数相加

    解法 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() ...

  5. 学渣的刷题之旅 leetcode刷题 2. 两数相加

    给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以 ...

  6. C#LeetCode刷题-数学

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

  7. LeetCode刷题笔记汇总

    LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...

  8. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  9. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

最新文章

  1. [UIImage imageNamed:@]使用误区
  2. 容器管理大战:Kubernetes vs.Docker Swarm与Amazon ECS
  3. NAACL19笔记:自然语言处理应用的实用理解(多图解链接)
  4. oracle12基本操作
  5. 操作系统原理:进程与线程、进程生命周期、线程的类型
  6. “个人核心竞争力”与“危机感”
  7. 初级Web前端工程师是什么?初级Web前端工程师的技术体系有哪些?
  8. find命令使用案例
  9. 生命的密码 —— DNA
  10. 开发一个React + Electron应用
  11. Android 高仿微信支付密码输入控件
  12. 有限元分析基础操作(ANSYS)
  13. Yolo 一小时学会基本操作
  14. 厦大计算机科学复试线,2020年厦门大学信息学院考研复试分数线
  15. 2014 史丰收速算
  16. 光盘刻录 CD刻录软件 Ashampoo Burning Studio特别版 刻录CD就这么简单
  17. Eclipse - 取消英文拼写检查
  18. 开发中关于手机的听筒模式的设置
  19. 微信营销的技巧有哪些 微信营销取名的禁忌 微信内容写作的七个要点
  20. 华为路由器 基本ACL配置

热门文章

  1. 目标检测论文阅读:Cascade R-CNN: Delving into High Quality Object Detection(CVPR2018)
  2. 第一章 数据挖掘基础
  3. 【AI视野·今日Robot 机器人论文速览 第十五期】Fri, 25 Jun 2021
  4. 【python】正则表达式匹配多个模式
  5. Linux—scp或ssh出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  6. Kubernetes-Pod/ReplicaSet/Deployment/Service关系(二十一)
  7. 02 掌握变量的定义和使用方法 1214
  8. 类与对象 格式小结 java 1202
  9. 虚拟环境的常用命令 virtualenv命令
  10. 索引-python编程技术-第二版