【Leetcode】Add Digits(easy)
来源:互联网 发布:java工程师招聘北京 编辑:程序博客网 时间:2024/04/29 10:51
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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Code
#include<iostream>using namespace std;class Solution {public: int addDigits(int num) { while (num>=10) { num = num/10 + num % 10; } return num; }};int main() { Solution so; int num = 38; cout<<so.addDigits(num)<<endl; system("pause"); return 0;}
0 0
- 【leetcode】Add Digits[easy]
- 【Leetcode】Add Digits(easy)
- <LeetCode><Easy>258Add Digits
- Leetcode----258.Add Digits(easy)
- LeetCode #258 - Add Digits -Easy
- leetcode 258[easy]---Add Digits
- 【leetcode】【Easy】【258. Add Digits】【Math】
- Leetcode 258. Add Digits (Easy) (cpp)
- 258. Add Digits #Easy
- 258. Add Digits (Easy)
- add digits(easy)
- leetcode 258 Add Digits(难易度:Easy)
- 258. Add Digits [easy] (Python)
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- 学习笔记,记录的开始~^_^
- u3d中如何读取txt文件中的信息
- 九宫格问题
- 目标跟踪基本算法思维导图
- iOS调用系统的通讯录界面获取用户信息
- 【Leetcode】Add Digits(easy)
- 隐藏id
- 浅谈WebView的使用
- 匿名内部类
- 解决不能连接数据库报错10038
- Linux下用crontab定时执行PHP程序有问题时候的解决方式
- Java之一 Java语 言 的 产 生 及 其 特 点
- Java try/catch/finally代码块中加入return后的执行顺序问题
- QFile和C语言对文件操作的性能比较.--读取double型二进制数据文件