[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 ...
随机推荐
- iOS开发--数组
1.sortedArrayUsingSelector (按Key值大小对NSDictionary排序) NSMutableArray *array = [NSMutableArray arrayWit ...
- React-Flux 介绍及实例演示
一.Flux架构 二.例子 1.TodoApp.react.js /** * Copyright (c) 2014-2015, Facebook, Inc. * All rights reserved ...
- Help And Manual 帮助文件制作工具
Help And Manual 简 介 帮助文件制作工具 支持文件格式 26种 其他功能 制作非常专业的使用手册 一个所见即所得的帮助文件制作工具,是市面上功能最强的 WYSIWYG (所见即所 ...
- 源代码Log
MVC5源代码 https://github.com/aspnet/Mvc MVC4源代码 http://aspnetwebstack.codeplex.com/
- spring cloud config 入门
简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...
- ios中addtarget的用法
1.addtarget 的.部分使用事件没有直接的操作方式,需要进行调用.就要用addTarget. - (void)setupCustomView { self.customView = [[CHV ...
- Xcode学习
http://www.cnblogs.com/ygm900/p/3488881.html
- Oracle 数据集成的实际解决方案
就针对市场与企业的发展的需求,Oracle公司提供了一个相对统一的关于企业级的实时数据解决方案,即Oracle数据集成的解决方案.以下的文章主要是对其解决方案的具体描述,望你会有所收获. Oracle ...
- C++中求两个正整数的最大公约数和最小公倍数
最大公约数直接用辗转相除法,最小公倍数就是两个数的乘积除以最大公约数 #include<iostream> using namespace std; int gys(int x,int y ...
- 带你认识HTML5中的WebSocket
这篇文章主要介绍了带你认识HTML5中的WebSocket,本文讲解了HTML5 中的 WebSocket API 是个什么东东.HTML5 中的 WebSocket API 的用法.带Socket. ...