[抄题]:

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. [笔记]_ELVE_正则表达式

    本文章转载至简书技匠,简书签约作者,技匠,以上内容欢迎大家分享到朋友圈/微博等. 正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑. ...

  2. java 从一个工程action 跳转到另外一个工程action

    实现功能:java 从一个工程action 跳转到另外一个工程action 在我们实际编程的过程中,大家一定遇到过这种情况,那就是在一个工程中,需要使用到另外一个工程的实体Bean和方法.那么遇到这种 ...

  3. bvlc_reference_caffenet网络权值可视化

    一.网络结构 models/bvlc_reference_caffenet/deploy.prototxt 二.显示conv1的网络权值 clear; clc; close all; addpath( ...

  4. 基础:位(bit)、字节(byte)、字符、编码之间的关系

    1.位: 数据存储的最小单位.每个二进制数字0或者1就是1个位: 2.字节: 8个位构成一个字节:即:1 byte (字节)= 8 bit(位): 1 KB = 1024 B(字节): 1 MB = ...

  5. 初识rt-thread

    bernard.xiong CEO 熊谱祥 env,提供编译构建环境.图形化系统配置及软件包管理功能 scons 是 RT-Thread 使用的编译构建工具,可以使用 scons 相关命令来编译 RT ...

  6. <Numerical Analysis>(by Timothy Sauer) Notes

    2ed,  by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...

  7. Python的xml模块

    先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...

  8. composer在phpstorm中安装代码库

    E:\php\PHPTutorial\WWW\kmmhtt>composer install composer 安装地址 :https://getcomposer.org/download/

  9. mongo官方企业版安装及数据库授权使用

    通过安装.deb包的方式,系统是Ubuntu 16.04 1. Import the public key used by the package management system.(导入包管理系统 ...

  10. python字符串前面的r/u/b的意义 (笔记)

    u/U:表示unicode字符串 : 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. r/R:非转义的原始字符串: 与普通字符相比,其他相对特殊的字符,其中可能包含 ...