[抄题]:

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.
  Since 2 has 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 入学考试:数位相加的更多相关文章

  1. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  2. 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 ...

  3. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  4. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  5. 258. Add Digits 数位相加到只剩一位数

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  6. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  7. 258 Add Digits 各位相加

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

  8. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  9. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

随机推荐

  1. 从npm 角度理解 mvn 的 pom.xml

    从npm 角度理解 mvn 的 pom.xml pom -- project object model. 用于描述项目的配置: 基础说明 依赖 如何构建运行 类似 node.js 的 package. ...

  2. redux进一步优化

    1. 将原来的  mapStateToDispatch  中的函数提取出来,放在组件中, 如原来的: function mapStateToProps(state, ownProps) { retur ...

  3. Centos7 下安装VMware tools

    1:先在虚拟机点击安装VMware Tools   2:然后挂载       mount /dev/cdrom /mnt 3:进入/mnt,可以看到有       4:拷贝VMwareTools到其他 ...

  4. Java_框架面试题

    Java_框架面试题 欢迎来我Git上分享您的优秀建议 1.Spring框架分为哪七大模块,各模块的主要功能作用是什么? 七大模块,如下: 1. Spring Core: Core封装包是框架的最基础 ...

  5. ionic3安卓版release发布

    1.进入到项目根目录 keytool -genkey -v -keystore your-full-keystore-name.keystore -alias your-lias-name -keya ...

  6. bcdboot(引导修复工具) 命令行工具使用方法

    BCDboot 是一种用于快速设置系统分区或修复系统分区上的启动环境的工具.系统分区是通过从已安装的 Windows(R) 映像复制一小部分启动环境文件来设置的.BCDboot 还会在系统分区上创建引 ...

  7. 解决配置Windows Update失败,还原更改问题

    问题描述 由于配置Windows Update失败,还原更改状态下无法正常关机.只能长按电源键关机后进入WinPE环境. 解决步骤 进入WinPE环境->选择Dism++->选择版本-&g ...

  8. [UE4]Spacer

    一.Spacer:留白占位控件 二.如下图所示,如果想要2个按钮都在容器右对齐: 三.可以放一个Spacer到最左边,设置成Fill,Spacer控件就是起到占位的作用.  

  9. 从Tomcat的处理web请求分析Java的内存模型

    Tomcat作为一个java应用,同样是有主线程和子线程的.主线使用while(true)的方式一直循环,等待客户端来连接.一个客户端来了之后,就从线程池中拿一个线程来处理请求,如果没有配置线程池,就 ...

  10. 高性能mysql 第六章查询性能优化 总结(上)查询的执行过程

    6  查询性能优化 6.1为什么查询会变慢 这里说明了的查询执行周期,从客户端到服务器端,服务器端解析,优化器生成执行计划,执行(可以细分,大体过程可以通过show profile查看),从服务器端返 ...