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 ...
随机推荐
- Android 拍照或从相册取图片并裁剪
在Android中,Intent触发Camera程序,拍好照片后,将会返回数据,但是考虑到内存问题,Camera不会将全尺寸的图像返回给调用的Activity,一般情况下,有可能返回的是缩略图,比如1 ...
- [转载] ffmpeg摄像头视频采集-采集步骤概述并采集一帧视频
近期由于工作任务,需要开发一个跨平台视频聊天系统,其中就用到了ffmpeg进行采集与编码,网上找了一大堆的资料,虽然都有一些有用的东西,但实在太碎片化了,这几天一直在整理和实验这些资料,边整理,边做一 ...
- HDU - 6129 :Just do it (杨辉三角)
There is a nonnegative integer sequence a 1...n a1...n of length n n . HazelFan wants to do a type ...
- 【整理】石子合并问题(四边形不等式DP优化)
有很多种算法: 1,任意两堆可以合并:贪心+单调队列. 2,相邻两堆可合并:区间DP (O(n^3)) ). 3,相邻,四边形不等式优化DP (O(n^2) ). 4,相邻,GarsiaWach ...
- 剑指Offer面试题:11.调整数组顺序使奇数位于偶数前面
一 题目:调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 二 解题思路 如果不考虑时间复杂度 ...
- sublime text3 中设置默认浏览器,并且设置快捷键
1.打开packageControl 对应快捷键 command + shift + p 2.输入install package 3.安装插件 SideBarEnhancements 4.安装了 ...
- manyToManyField理解和用法
转:http://www.cnblogs.com/linxiyue/p/3667418.html
- ArcGIS_Lisence安装步骤
1.双击lisence.exe文件 2.下一步 3.关闭 4.下一步 5.下一步 6.下一步 7.安装 8.完成 9.OK
- wordpress改不了固定连接的解决办法
经常遇到更改了固定连接刷新没效果>>>>>>>废话不多说直接上步骤,有图有真相. 1,ftp删除根目录的这个文件夹 2,进入wp后台设置-固定连接—自定义结构 ...
- HTTP协议-简介
1.什么是http协议? 百度百科上的解释:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准. ...