[抄题]:

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. 列表:remove/del删除方法中的逻辑“误区”

    结果: list_1=["A","B","C","D","E","F",&quo ...

  2. zombodb 索引管理

    zombodb 支持标准的index 管理(create .alter.drop) 创建索引 CREATE INDEX index_name ON table_name USING zombodb ( ...

  3. ILBC 规范 2

    接上篇 <ILBC 规范>  https://www.cnblogs.com/KSongKing/p/10354824.html  , ILBC    的 目标 是    跨平台  跨设备 ...

  4. Node.js express获取参数有三种方法

    express获取参数有三种方法:官网介绍如下 Checks route params (req.params), ex: /user/:id Checks query string params ( ...

  5. 一些有用的Java学习资料

    Better Java,一些好的Java实践 Google Java Style Guide 30个Java编程技巧 JDK8新增语法特性简介,对Java8中新增的函数接口.Lambda表达式.方法引 ...

  6. springboot对oracle的配置

    spring.jpa.database=oracle spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver sprin ...

  7. git与github区别与简介

    From: https://blog.csdn.net/skyxmstar/article/details/65631658 git和github是两个完全不同的概念. git 是一个版本管理工具,是 ...

  8. LeetCode——300. Longest Increasing Subsequence

    一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...

  9. C# 自定义类动态追加属性

    利用Dynamic,需要.net4.0以上的支持 var dg = rel.ResultDocuments.FirstOrDefault()["dg"].AsBsonArray.G ...

  10. x86 asm调用框架(vs2015)

    EXTERN_C void _stdcall Asm_1();//在cpp文件下 要使用EXTERN_C . .MODEL FLAT,C,stdcall .DATA .CODE Asm_1 PROC ...