12-Add Digits
寻找一个数的数根,用了暴力破解的方式,时间复杂度比较高
暂未想到O(1)的方式
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?
class Solution {
public:
int addDigits(int num) {
int res=10;
while(res>=10)
{
res=0;
while(num)
{
res=res+num%10;
num=num/10;
}
num=res;
}
return res;
}
};
12-Add Digits的更多相关文章
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- 【02_258】Add Digits
Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- [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. ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- 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
258. Add Digits Easy Given a non-negative integer num, repeatedly add all its digits until the resul ...
随机推荐
- QEvent
QEvent类是所有事件类的基类,每一个对象都包含事件参数.Qt的主事件循环(QCoreApplication::exec())从事件队列中接收本地窗口系统的事件,并将它们翻译成QEvent,将这些事 ...
- Makefile目标文件搜索(VPATH和vpath
转载:http://c.biancheng.net/view/7051.html 我们都知道一个工程文件中的源文件有很多,并且存放的位置可能不相同(工程中的文件会被放到不同的目录下),所以按照之前的方 ...
- cf 24 Game (观察+.. 想一想)
题意: 给一个数N,从1到N. 每次取两个数,三种操作:加.减.乘,运算完得一个数,把那俩数删了,把这个数加进去. 重复操作N-1次. 问是否可能得到24.若可以,输出每一步操作. 思路: 小于4,不 ...
- Spring事务的介绍,以及基于注解@Transactional的声明式事务
前言 事务是一个非常重要的知识点,前面的文章已经有介绍了关于SpringAOP代理的实现过程:事务管理也是AOP的一个重要的功能. 事务的基本介绍 数据库事务特性: 原子性 一致性 隔离性 持久性 事 ...
- Django 实现分页功能(django 2.2.7 python 3.7.5 )
Django 自带名为 Paginator 的分页工具, 方便我们实现分页功能.本文就讲解如何使用 Paginator 实现分页功能. 一. Paginator Paginator 类的作用是将我们需 ...
- maven+springmvc+cxf 实现简单webservice小例子
1.首先你需要创建一个maven项目[当然是web项目] 2.pom.xml添加以下 <properties> <cxf.version>2.2.3</cxf.versi ...
- 检查redis是否正常运行
[XX@XXX]$ ps -ef | grep redisXX 8047 1 0 10:06 ? 00:00:03 redis-server *:6379XX 9983 9802 0 11:2 ...
- VSCode PHP 开发环境配置 详细教程
VSCode PHP 开发环境配置 详细教程 这篇文章主要介绍了VScode+PHPstudy配置PHP开发环境的步骤,整理了官方以及优秀第三方的内容,对于学习和工作有一定借鉴意义. 配置过程 第一步 ...
- mybatis之结果集的映射方式
查询的几种情况 // 1)查询单行数据返回单个对象 public Employee getEmployeeById(Integer id ); // 2) 查询多行数据返回对象的集合 public L ...
- MySQL 各版本连接驱动字符串
绪论 MySQL版本与连接驱动的版本有严格的对应关系,所以在这里记录一下 MySQL 5.7 jdbc.drive=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql: ...