leetcode笔记--6 Add Digits
question:
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.
answer:(which don't think by myself)
思路:
举例:
38,11,2 38-2=36(38-11=27,11-2=9,都是9的倍数)
57,12,3 57-3=54(57-12=45,12-3=9,都是9的倍数)
所以直接用57/9 得余数为3 ??
其实不对.......忘记了特殊情况(就是整除的时候)
18,9
余数为0,不符合上述方法
所以需要先减去一个数,最后余数再加上一个数,可以避免这种情况,但是可以随意先加后减一个数吗??
不可以,举例10-2=8 8对9取余数为8,8+2=10,不符合条件
所以只能是1
leetcode笔记--6 Add 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 ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode OJ:Add Digits(数字相加)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode算法题-Add Digits(Java实现-3种解法)
这是悦乐书的第199次更新,第207篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第63题(顺位题号是258).给定非负整数num,重复添加其所有数字,直到结果只有一位数 ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- LeetCode 题解之Add Digits
1.问题描述 2.问题分析 循环拆分数字,然求和判断. 3.代码 int addDigits(int num) { ) return num; int result = num; do{ vector ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
随机推荐
- 【mysql案例】mysql5.6.14配置my.cnf多实例,mysql_install_db初始化不读取my.cnf配置文件
1.1.1. mysql5.6.14多实例my.cnf时,初始化不读取my.cnf配置文件 [环境描写叙述] 在多实例配置的/etc/my.cnf环境中,运行mysql_install_db后.启动M ...
- 【[ZJOI2010]网络扩容】
题目 第一问直接板子敲上去 第二问并不明白直接在残量网络上加边的神仙做法 非常显然我们需要让流量加\(k\),那么我们就使得网络里的总流量为\(maxf+k\),\(maxf\)是第一问求出来的最大流 ...
- 【转】一个Android项目搞定所有主流架构-1.项目介绍和基本MVC架构示例
http://www.jianshu.com/p/798536fb91c5 项目启发来自谷歌的同类框架项目https://github.com/googlesamples/android-archit ...
- Trident中 FixedBatchSpout分析
FixedBatchSpout 继承自 IBatchSpout IBatchSpout 方法 public interface IBatchSpout extends Serializable { v ...
- python-time、datetimme模块
time模块 1.time.time():返回当前时间的时间戳. 打印时间戳: >>> import time >>> time.time() 1530329387 ...
- EntityFramework Code-First-------领域类配置之DataAnnotations
EF Code-First提供了一个可以用在领域类或其属性上的DataAnnotation特性集合,DataAnnotation特性会覆盖默认的EF约定. DataAnnotation存在于两个命名空 ...
- 石头剪刀布的JAVA小程序 供初学者参考
package youxi; public class Player { private String name; private double score; public Player(String ...
- SP1716 GSS3 - Can you answer these queries III(单点修改,区间最大子段和)
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are give ...
- 关于 'list' object has no attribute 'select'
我是在写爬虫是遇到了这个问题: c = chapter.select('href')AttributeError: 'list' object has no attribute 'select' 这是 ...
- BZOJ1607: [Usaco2008 Dec]Patting Heads 轻拍牛头(模拟 调和级数)
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 3031 Solved: 1596[Submit][Status][Discuss] Descriptio ...