[LeetCode] Add Digits (a New question added)
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?
貌似是前天还是大前天新加的一道题来着。
这是一道很简单的题哈。直接用loop就可以解决。这是常规思维。接着下面分享一个非常取巧的算法。
常规思维的代码如下。~(不要忘了-‘0’哦)
public class Solution {
public int addDigits(int num) {
String test=String.valueOf(num);
while(test.length()>1){
num=0;
for(int i=0;i<test.length();i++){
num=num+test.charAt(i)-'0';
}
test=String.valueOf(num);
}
return num;
}
}
还有一个非常巧妙的算法。 只要一行即可解决。代码如下。~
这个算法的道理很简单,分别算算1-20的这20个数的add digits的结果,就可以找出规律了。
public class Solution {
public int addDigits(int num) {
return (num-1)%9+1;
}
}
[LeetCode] Add Digits (a New question added)的更多相关文章
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (leetcode)Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode——Add Digits
Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode Add Digits (规律题)
题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
随机推荐
- json的数据格式(仔细查看)
1.json对象就是jsonObject,jsonobject里可以放入很多键值对,并以逗号为分隔符. jsonObject里还可以嵌套JsonObject对象,或者数组信息作为value,数组作为k ...
- ios开发--高德地图SDK使用简介
高德LBS开放平台将高德最专业的定位.地图.搜索.导航等能力,以API.SDK等形式向广大开发者免费开放.本章节我们来简单学习一下如何使用它的定位及地图SDK. 一.相关框架及环境配置 地图SDK 对 ...
- 涨姿势:创业做一个App需要花多少钱(8个人,6个月,就要100万,附笔记心得)
(原标题:涨姿势:创业做一个App要花多少钱?) 作为互联网从业者,被外行的朋友们问及最多的问题是,“做一个网站需要多少钱?”或者“做一个APP需要多少钱?”. 作为做过完整网站项目和APP的人,今天 ...
- Android:创建Android工程
创建Android工程,在Eclipse左栏右键 new->project.. (版本不一样,名字会有所区别) 然后选择Android下的Android application projec ...
- HDU2521反素数
只是了解下这种简单的数论定义,解释可以戳这个 http://www.cnblogs.com/Findxiaoxun/p/3460450.html ,然后按Ctrl+ F搜索 反素数 ,找到那一部 ...
- 使用CXF暴露您的REST服务
使用CXF暴露您的REST服务 REST应用服务器SpringBeanServlet 1. 前言 现在互联网Open API流行,将您的Web应用也可以开放Open API给其他第三方使用.达到一 ...
- Git教程之时光穿梭(3)
我们已经成功地添加并提交了一个readme.txt文件,现在我们继续修改readme.txt文件,改成如下内容:
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- (转)最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1)
最近一直有朋友在问,最新版的Spring.Struts.Hibernate整合老是有问题,昨晚大概看了一下.从Hibernate 4 开始,本身已经很好的实现了数据库事务模块,而Spring也把Hib ...
- MariaDB10.2.X-新特性2-支持check约束and with as
前几天写了一篇MariaDB10.2支持分析函数,大家印象中MySQL不支持with as ,check约束,那么MariaDB10.2也同样给你惊喜 1.with as MariaDB [test1 ...