[leetcode][math] 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 = 111 + 1 = 2
. Since 2
has
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
class Solution {
public:
int addDigits(int num) {
int res = 0;
bool doneFlag = false;
while(1){
res = 0;
while(num > 0){
res += num%10;
num /= 10;
}
if(res/10 == 0) break;
num = res;
}
return res;
}
};
class Solution {
public:
int addDigits(int num) {
if(num == 0) return 0;
if(num % 9 == 0) return 9;
return num%9;
}
};
[leetcode][math] Add Digits的更多相关文章
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- [LeetCode] 258. Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
随机推荐
- js本地对象——Date()
Date()是JavaScript的本地对象,用于获取当前的时间,包括年.月.日.时.分.秒,可以精确到毫秒级:该对象返回的是UTC 协调世界时(Coordinated Universal Time) ...
- 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- [using_microsoft_infopath_2010]Chapter5 为表单添加逻辑规则
本章概要: 1.在表单中使用逻辑和验证,不写代码 2.使用规则任务板 3.添加表单条件格式 4.通过函数和公式添加更加高级的规则 5.通过对驶入使用规则创建直观的用户界面
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...
- 用LayerDrawable实现两个图片的叠加效果
Drawable[] layers = new Drawable[2]; layers[0] = new ColorDrawable(primaryColor); layers[1] = new Co ...
- 使用malloc分别分配2KB的空间,然后用realloc调整为6KB的内存空间,打印指针地址
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...
- 一分钟了解Android横竖屏 mdpi hdpi xhdpi xxhdpi xxxhdpi
DPI:每英寸像素数 简单的屏幕分辨率计算方法: DisplayMetrics metrics = this.getResources().getDisplayMetrics(); float den ...
- 【甘道夫】Hadoop2.2.0环境使用Sqoop-1.4.4将Oracle11g数据导入HBase0.96,并自己主动生成组合行键
目的: 使用Sqoop将Oracle中的数据导入到HBase中,并自己主动生成组合行键! 环境: Hadoop2.2.0 Hbase0.96 sqoop-1.4.4.bin__hadoop-2.0.4 ...
- bzoj1218: [HNOI2003]激光炸弹(DP二维前缀和)
1218: [HNOI2003]激光炸弹 题目:传送门 题解: 一道经典题目啊... 为了更好的操作...把整个坐标系向右上角移动,从(1,1)开始 那么f[i][j]统计一下以(i,j)作为右上角, ...
- Web前端错题模糊题记录
title: Web前端错题模糊题记录 toc: true date: 2018-09-20 10:04:36 categories: Web tags: HTML CSS JavaScript HT ...