算法面试题-leetcode学习之旅(二)
题目:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, 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?
分析:
Hint:
A naive implementation of the above process is trivial. Could you come up with other methods?
What are all the possible results?
How do they occur, periodically or randomly?
You may find this Wikipedia article useful.
这道题让我们求数根,所谓树根,就是将大于10的数的各个位上的数字相加,若结果还大于0的话,则继续相加,直到数字小于10为止。那么根据这个性质,我们可以写出一个解法如下:
答案:
class Solution {
public:
int addDigits(int num) {
while (num / 10 > 0) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num;
}
};
分析提升:
但是这个解法在出题人看来又trivial又naive,需要想点高逼格的解法,一行搞定碉堡了,那么我们先来观察1到20的所有的树根:
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 1
11 2
12 3
13 4
14 5
15 6
16 7
17 8
18 9
19 1
20 2
根据上面的列举,我们可以得出规律,每9个一循环,所有大于9的数的树根都是对9取余,那么小于等于9的数对9取余就是0了,为了得到其本身,而且同样也要对大于9的数适用,我们就用(n-1)%9+1这个表达式来包括所有的情况,所以解法如下:
改进:
class Solution {
public:
int addDigits(int num) {
return (num - 1) % 9 + 1;
}
};
算法面试题-leetcode学习之旅(二)的更多相关文章
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
标题 Maximum Depth of Binary Tree 描述 The maximum depth is the number of nodes along the longest path f ...
- 算法面试题-leetcode学习之旅(一)
问题描述 Given an array of size n, find the majority element. The majority element is the element that a ...
- LeetCode 算法面试题汇总
LeetCode 算法面试题汇总 算法面试题 https://leetcode-cn.com/problemset/algorithms/ https://leetcode-cn.com/proble ...
- 【BAT经典算法面试题系列】求和为n的连续正整数
马上就要到9月份了,意味着一年一度的秋招就要开始了,相信不论是正在实习的童鞋还是马上就要找工作的童鞋,BAT无疑是国内的"明星企业",是每个学计算机的小伙伴们心之向往的企业,但是呢 ...
- 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!
算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! 解题思路 这是群里某位小伙伴去面试碰到的面试题,从题目本身来看,面试官应该是要考察面试者对泛型 Lis ...
- Java算法面试题(史上最强、持续更新、吐血推荐)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- Twitter算法面试题详解(Java实现)
最近在网上看到一道Twitter的算法面试题,网上已经有人给出了答案,不过可能有些人没太看明白(我也未验证是否正确),现在给出一个比较好理解的答案.先看一下题目. 图1 先看看图图1.可以将方块看做砖 ...
- 算法实践——Twitter算法面试题(积水问题)的线性时间解法
问题描述:在下图里我们有不同高度的挡板.这个图片由一个整数数组所代表,数组中每个数是墙的高度.下图可以表示为数组(2.5.1.2.3.4.7.2).假如开始下雨了,那么挡板之间的水坑能够装多少水(水足 ...
- 华为Python 算法面试题
华为算法面试题 """ 算法题: 提供一个序列,完成对这个序列的分割.要求分割后的两个序列彼此差值最小 实现函数,返回两个序列 """ de ...
随机推荐
- Ruby方法参数默认值的一个小技巧在Rails中的应用
我们需要生成一个gravatar格式的html.image标示,于是写了如下方法: def gravatar_for(user) gravatar_id = Digest::MD5::hexdiges ...
- vue以及js的一些坑或常用技巧
判断空object Object.getOwnPropertyNames(obj).length === 0 模拟range Array.apply(null, Array(5)).map(funct ...
- Spring动态切换多数据源解决方案
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- 利用git pull的勾子实现敏捷部署
监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...
- Hibernate与JPA的区别是什么
翻译来源:https://www.quora.com/What-is-the-difference-between-Hibernate-and-JPA 本文作者:苏生米沿 本文地址:http://bl ...
- Hibernate设置时间戳的默认值和更新时间的自动更新
Generated and default property values 生成的和默认的属性值 The database sometimes generates a property value, ...
- Linux-2.6.25 TCPIP函数调用大致流程
插口层系统调用send sys_send sys_sendtosendto sys_sendto sock_sendmsgsendmsg sys_send ...
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...
- Spark核心类:SQLContext和DataFrame
http://blog.csdn.net/pipisorry/article/details/53320669 pyspark.sql.SQLContext Main entry point for ...
- shell编程--流程控制for,do-while,if-then,break,continue,case等
2.5 流程控制 2.5.1 if语法 1.语法格式 if condition then statements [elif condition then statements. ..] ...