问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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刷题笔记258 各位相加

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

  2. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

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

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

  4. C#LeetCode刷题-数学

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

  5. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  6. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  7. LeetCode刷题总结-链表

    LeetCode刷题总结-链表 一.链表     链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的 ...

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

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

  9. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

随机推荐

  1. DEX文件解析--3、dex文件字符串解析

    一.前言    前两篇文章链接:     1.DEX文件头解析     2.DEX文件校验和解析    PS:前几天检查文件夹的时候发现DEX文件解析还只写了开头,正好找点事情来做,就去接着解析DEX ...

  2. OSCP Learning Notes - File Transfers(1)

    File transfer type: 1. HTTP Transfer files through the website. 2.wget wget http://10.0.0.109/exploi ...

  3. 雨云CDN - 好用的CDN服务

    注册雨云 点我 创建CDN     解析CDN 解析完后去试试快了吗?

  4. Python过滤掉numpy.array中非nan数据实例

    代码 需要先导入pandas arr的数据类型为一维的np.array import pandas as pd arr[~pd.isnull(arr)] 补充知识:python numpy.mean( ...

  5. Linux企业运维人员最常用命令汇总

    本文目录 线上查询及帮助命令 文件和目录操作命令 查看文件及内容处理命令 文件压缩及解压缩命令 信息显示命令 搜索文件命令 用户管理命令 基础网络操作命令 深入网络操作命令 有关磁盘与文件系统的命令 ...

  6. ELK5.6.4+Redis+Filebeat+Nginx(CentOS7.4)

    下载地址: Elasticsearhc: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.tar.gz ...

  7. Bug:No mapping for GET /onepill//swagger-ui.html

    SpringBoot使用Swagger2本来可以使用的,后来出现的异常No mapping for GET /swagger-ui.html,这个异常其实不用怎么解释,说白了就是找不到了. 遇到这种情 ...

  8. 修改git上传代码的作者姓名

    本文参考https://www.cnblogs.com/weiaiqi/p/11842515.html 随着IT行业的不断壮大,开源的东西越来越多,使用git来进行代码管理的人自然也越来越多,而且很多 ...

  9. Numpy random函数

    import numpy as np # 生成一个随机数组 np.random.randint(0,6,3) # array([1, 1, 3]) # 生成一个随机数组(二维数组) np.random ...

  10. PHP zip_close() 函数

    定义和用法 The zip_close() 函数关闭由 zip_open() 函数打开的 zip 档案.高佣联盟 www.cgewang.com 语法 zip_close(zip) 参数 描述 zip ...