258. Add Digits 数位相加到只剩一位数
[抄题]:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
%9
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
if + else if不是完整的,if + else才是完整的
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
if + else if不是完整的,if + else才是完整的
[复杂度]:Time complexity: O(1) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int addDigits(int num) {
if (num == 0)
return 0;
if (num % 9 == 0) {
return 9;
}
else{
return num % 9;
}
}
}
258. Add Digits 数位相加到只剩一位数的更多相关文章
- 258 Add Digits 各位相加
给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字.例如:设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它.进阶:你可 ...
- 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 ...
- 【一天一道LeetCode】#258. Add Digits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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仅仅有一位数.所以返回它. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
随机推荐
- SQL—数据分析
留存分析——左连接 矩阵分析——group by 结构分析:分析每种产品占据总销售量的比例 排序分析
- vs中删除nuget包
最近发现有些解决方案都是用来nuget包,这个偶尔能跑,但是有一个爱抽风的毛病,生成解决方案的时候报错:无法连接到远程服务器,真几把蛋疼.... 就是下图的情况 网上找了下不是很容易找到处理这个问题的 ...
- Net Core ElasticSearch入门
ElasticSearch入门 附.Net Core例子 https://www.cnblogs.com/CoderAyu/p/9564977.html 1.什么是ElasticSearch? Ela ...
- 转载关于reset vector 和 exception vector
在NIOS II学习过程中设置CPU参数的时候,遇到Reset Vector和Exception Vector的设置.参数设置画面如下图所示. Reset Vector——复位向量Exception ...
- nginx中在超全局变量$_SERVER中增加变量
业务中可能会用到一些自定义的超全局变量,需要在nginx中生成的,比如,每次nginx请求的id,可以在nginx中配置 如: location ~ \.php$ { root / ...
- assembly 需要 unload 和 update 的时候怎么办?
我正在开发公司的业务组件平台,组件池的灵活性要求很高,业务组件都是可以立即更新和及时装配的;目前完成这些功能,有待测试.用appDomain.unload 拆卸assembly 可以,只是用起来比较麻 ...
- FastAdmin 一键 CRUD 生成时方法不存在的问题分析
FastAdmin 一键 CRUD 生成时方法不存在的问题分析 有群友反馈 使用 一键 CRUD 生成时不成功. 我试了以下命令 php think crud -t test -u 1 是成功的. 再 ...
- bzoj 2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...
- dxjk中 支付宝二维码支付 git 存疑
线上的vendor/latrell/alipay 文件拉取不了至本地,失去了git监控 要想本地使用 1.注释掉config/app.php 'providers' 下的Latrell模块 2.下载线 ...
- java代码如何替换字符
package com.aa; public class Ss { public static void main(String[] args) { String a = "ABCD&quo ...