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 ...
随机推荐
- manyToManyField理解和用法
转:http://www.cnblogs.com/linxiyue/p/3667418.html
- 归并排序的JavaScript实现
思想 这是一种分治算法.将原始数组切分成较小的数组,直到每个小数组只有一项,然后在将小数组归并为排好序的较大数组,直到最后得到一个排好序的最大数组. 代码 function mergeSort(arr ...
- 本地连接远程环境mysql报错:Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
问题现象:本机连接远程环境的mysql数据库报错,提示本机ip无法连接到mysql服务器. 问题原因:本机对远程mysql数据库没有访问权限. 解决方法:在远程mysql数据库添加本机ip的访问权限: ...
- gen_server模块
转自http://www.xuebuyuan.com/2132233.html
- javaMail邮件接收解析内容及附件 及删除邮件
参考自: http://blog.csdn.net/xyang81/article/details/7675160 package com.szy.project.utils; import jav ...
- Diffie-Hellman 密钥交换
假定有两个全局公开的参数,分别为一个素数p和一个整数g,g是p的一个原根,为了协商共享的会话密钥: 首先,用户A随机选取a,计算出A = g^a mod p,并将A发送给B:然后,用户B随机选取b,计 ...
- split的用法回顾,快忘记了@ →@
split:用for循环时不要忘记是数组名.length package com.aaa; //split的用法把指定的字符串按指定的分割符进行分割,然后返回字符串 数组 public class f ...
- Sql server 2008 R2 正在关闭[0x80041033]
1. 事件起因, 昨天还访问的好好的, 然后系统一更新, 今天访问的时候, 就报什么 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是 ...
- C#字符串Split方法的误区
string s = "aaa1bbb2ccc1ddd"; string[] ss = s.Split("12".ToCharArray()); ...
- 分布式队列 Celery
详情参见: 分布式队列神器 Celery 用户指南(User Guide) 1) Celery-4.1 用户指南: Application(应用) 2) Celery-4.1 用户指南: Task(任 ...