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 ...
随机推荐
- C# 线程安全集合
转载 对于并行任务,与其相关紧密的就是对一些共享资源,数据结构的并行访问.经常要做的就是对一些队列进行加锁-解锁,然后执行类似插入,删除等等互斥操作. .NetFramework 4.0 中提供了一些 ...
- PythonStudy——函数的导入 Import of functions
# 函数:完成 特定 功能的代码块,作为一个整体,对其进行特定的命名,该名字就代表函数# 难点:如何定义个函数 # 现实中很多问题要通过一些工具进行处理 => 可以将工具提前生产出来并命名# = ...
- 3.3-1933 problem A
#include <stdio.h> int main(void){ int h; while(scanf("%d", &h) != EOF){ * (h-); ...
- zabbix自动发现zabbix_agent后添加到所属组和链接到某些模块(九)
自动发现的两个操作:discovery(自动发现) and actions(发现后执行某个操作) 需求: 1:自动发现 Zabbix agent运行的主机 2:执行的动作 1)添加到所属组 ...
- MySQL 和 Oracle 在 MyBatis 使用中的区别
MySQL 和 Oracle 在 MyBatis 使用中的区别: 区别 MySQL Oracle 存储过程的参数模式 mode 为 IN 时,是否需要指定 jdbcType 不需要:MyBatis 为 ...
- postgresql jdbc 连接数据库测试
转载自:http://blog.csdn.net/southflow/article/details/5944107 1. 下载postgresql-8.4-702.jdbc4.jar 2. 点击 ...
- redis高可用(哨兵机制)
redis哨兵机制:redis的哨兵系统用于管理多个reids服务器,该系统主要有三个作用: 监控:哨兵 会不断地检查你的主服务(Master)和从服务器(Slave)是否运作正常. 提醒:当被监控的 ...
- vue打包
npm installnpm run devnpm run build @types/node npm install --save @types/node npm install @types/no ...
- 实验四:xl命令的常见子命令以及操作
实验名称: xl命令的常见子命令以及操作 实验环境: 这里我们需要正常安装一台虚拟机,如下图: 我们这里以一台busybox为例,来进行这些简单的常见的操作: 实验要求: 这里我们准备了5个常见操作: ...
- 基于form表单 写登陆注册
urls urlpatterns = [ url(r'^admin/', admin.site.urls), # 登录界面url(r'^login/$', views.login,name='logi ...