258. Add Digits 入学考试:数位相加
[抄题]:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input:38
Output: 2
Explanation: The process is like:3 + 8 = 11,1 + 1 = 2.
Since2has only one digit, return it.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
while循环写得太少,潜意识里还是for循环
//id: yuec2 name:Yue Cheng
package day1test; import java.util.Scanner; public class Numerologist { public static void main(String[] args) {
Numerologist n = new Numerologist();
System.out.println("Enter an integer");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println("Your lucky number is " + n.getLuckyNumber(number));
input.close();
} int getLuckyNumber(int num) {
//write your code here
int number = Math.abs(num);
String str = String.valueOf(number);
char digits[] = str.toCharArray();
while (digits.length != 1) {
int sum = 0;
for (int i = 0; i < digits.length; i++) {
sum += digits[i] - 'a';
}
str = String.valueOf(sum);
digits[] = str.toCharArray();
} if (digits.length == 1)
{
int result = digits[0] - 'a';
return result;
}
return 0;
}
}
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
最基本的while循环吧
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
corner case没注意,小于10的数字直接返回num本身
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
练习多写while循环吧
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int addDigits(int num) {
//corner case
if (num == 0) return 0;
//while loop
while (num >= 10) {
int sum = 0;
//add every digit
while (num > 0) {
sum += num % 10;
num /= 10;
}
//assign the sum to the new num
num = sum;
}
//return
return num;
}
}
258. Add Digits 入学考试:数位相加的更多相关文章
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 258. Add Digits 数位相加到只剩一位数
[抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- 258 Add Digits 各位相加
给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字.例如:设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它.进阶:你可 ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
随机推荐
- go-json处理的问题
1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...
- 一个nginx服务器配置多站点
有时候你想在一台服务器上为不同的域名运行不同的站点.比如www.siteA.com作为博客,www.siteB.com作为论坛.你可以把两个域名的IP都解析到你的服务器上,但是没法在Nginx的根目录 ...
- 戴尔R710服务器安装系统——配置raid
一,内存二,硬盘(分区,数据量大小)三,电源线,网络线四,raid(raid0,raid1,raid5) 从这里开始 1.进入系统时不用管,默认进入即可 2.在读完内存消息之后,开始读取磁盘消息,在出 ...
- windows添加永久静态路由
添加路由最好在命令行管理员模式下操作 添加临时路由命令,重启后失效 route add 172.16.1.0 mask 255.255.255.0 10.0.10.19 其中,172.16.1.0 是 ...
- 初探JavaScript的截屏实现
最近参与了网易炉石盒子的相关页面开发,在做卡组分享页(地址:炉石盒子卡组分享),有个需求:用户可以把这个卡组以图片的形式分享给好友.最初的的做法是使用服务器把该页面转换成图片,然后把图片地址返回给前端 ...
- 论文阅读笔记:【Transforming Auto-encoders】
[Transforming Auto-encoders]: G E Hinton, A Krizhevsky, S D Wang. Transforming auto-encoders[C]. //I ...
- Excel--按内容分页打印
当我们有这样一张表,需要按不同城市分页打印,每页带标题行,可按以下步骤:1.点击城市一列任一单元格,点击“开始”——>“排序和筛选”(升序): 2.点击“数据”-->“分类汇总”: 分类字 ...
- 后台自动运行,定期记录定位数据(Hbuilder监听 app由前台切换到后台、切换运行环境的 监听方法)
http://ask.dcloud.net.cn/question/28090 https://blog.csdn.net/qq_37508970/article/details/86649703 各 ...
- C 栈实现队列节点的管理
栈预先存储节点,队列的malloc/free均有栈模拟,从而保证不频繁的开辟/是否节点内存. #include "com_is_buf.h" #include "com_ ...
- flutter环境配置
java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...