Leetcode 258
来源:互联网 发布:js json对象元素个数 编辑:程序博客网 时间:2024/05/21 15:44
Total Accepted: 70241 Total Submissions: 146130 Difficulty: Easy
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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
刚开始也并不知知道如何O(1)的时间解决 , 后来查找才知道原来有个数根公式
digits = 1 + ( n - 1 )%9 ;
所以问题就简单了 ,一行 代码就搞定了 。
int addDigits(int num) { return (num-1)%9+1 ;}
0 0
- Leetcode 258
- leetcode 258
- Leetcode 258 258
- leetcode 258: Add Digits
- 【LeetCode】258_Add Digits
- 258Add 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
- leetcode 258 :Add Digits
- leetcode 258--Add Digits
- 数组(二维数组)
- Android Wear 进阶 2.1 Creating and Running a Wearable App 创建和运行手表应用
- xcode怎么把自定义代码添加到代码库CodeSnippets
- C++构造函数调用规则
- os_tmr.c
- Leetcode 258
- c++中new与delete
- sort c++ double
- Android JNI开发笔记
- 全局函数和成员函数
- 自定义view onMeasure方法的重写
- 淘宝应对"双11"的技术架构分析
- 友元函数与友元类
- 职坐标 选课系统整体框架 参考