Leetcode 258. Add Digits
来源:互联网 发布:辣条 网络用语 编辑:程序博客网 时间:2024/05/21 14:59
Given a non-negative integer
把两位数依次列出,列十几个就能找到规律
num
, repeatedly add all its digits until the result has only one digit.<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);"></span>
For example:
Given num = 38
, the process is like: 3 + 8 = 11
, 1 + 1 = 2
. Since 2
has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
public class Solution { public int addDigits(int num) { if (num == 0){ return 0; } if (num % 9 == 0){ return 9; } else{ return (num % 9); } }}
把两位数依次列出,列十几个就能找到规律
discuss最短代码,思路一样
public int addDigits(int num) { return num == 0 ? 0 : (num % 9 == 0 ? 9 : num % 9);}
0 0
- 【LeetCode】258.Add Digits
- LeetCode 258. Add Digits
- LeetCode : 258. Add Digits
- leetCode 258. Add Digits
- LeetCode 258. Add Digits
- 258. Add Digits LeetCode
- LeetCode 258. Add Digits
- LeetCode 258. Add Digits
- leetcode 258. Add Digits
- [LeetCode]258. Add Digits
- LeetCode 258. Add Digits
- <LeetCode>258. Add Digits
- leetcode--258. Add Digits
- LeetCode 258. Add Digits
- LeetCode *** 258. Add Digits
- Leetcode 258. Add Digits
- 258.[LeetCode]Add Digits
- 【leetcode】258. Add Digits
- 使用/sys/class/gpio对A31S的GPIO通过ADB进行设置
- asp.net中有关文件HTTP上传服务器保存的问题(捷易拍sdk开发方面)
- Spark函数讲解:coalesce
- 求100内的所有素数(质数)
- asm/page.h 的文件内容 asm/page.h: No such file or directory
- Leetcode 258. Add Digits
- LinkedHashMap源码解析
- mysql中int长度的意义
- Android中 完美实现 计时 倒计时 时间间隔处理等功能实现 by CountDownTimer
- JS -- =、==和===
- Maven学习详解(13)——Maven常用命令大全与pom文件讲解
- JAXB 解析XML获取不到属性值
- Mybatis初识
- Swift请求数据封装