LeetCode 258. 各位相加(Add Digits)
258. 各位相加
258. Add Digits
题目描述
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
LeetCode258. Add Digits
示例:
输出: 2
解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。
进阶:
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?
Java 实现
class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
相似题目
参考资料
LeetCode 258. 各位相加(Add Digits)的更多相关文章
- Java实现 LeetCode 258 各位相加
258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...
- [Swift]LeetCode258. 各位相加 | Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Leetcode -- 258 数位相加
258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- Leetcode——258.各位相加【水题】
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...
- LeetCode(258.各位相加)的思路及解决过程
问题如下: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...
- leetcode 258. 各位相加 (python)
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- 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 ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
随机推荐
- Sublime Text3注册及汉化(支持Windows、MAC OS)
苹果mac 版本下载 点击下载https://download.sublimetext.com/Sublime%20Text%20Build%203114.dmg PART_A 注册 v3103及以上 ...
- vmware exsi安装部署
本文章参考:https://blog.csdn.net/fishinhouse/article/details/80980051 1.VMware-ESXi-6.5.0镜像下载 网盘链接:https: ...
- mysql 导出数据或结构
mysql 导出数据或结构 语法说明 mysqldump -h 数据库地址 -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据 mysqldump -uroot ...
- 生成uuid 和 检验
//注意replaceAll前面的是正则表达式 String uuid = UUID.randomUUID().toString().replaceAll("-","&q ...
- Shell修改密码为原密码
密码到期后更改密码为原密码,服务器多时工作量巨大,编写脚本完成. 登陆后修改提示符为: doraemon# 方便expect匹配 修改LANG=C,提示为英文防止干扰: #!/usr/bin/expe ...
- idea创建类,接口,枚举等如何设置注释
进入设置: File -> Settings 依次选择: Editor -> File and Code Templates -> Files -> Class (根据需要 ...
- TCP为什么会出现 RST
就目前遇到的情况而言,都是负载设备,或健康检查设备发送的. 为什么会出现 RST 因为具有周期性,我大概猜到了,是 lvs 对我的后端服务的健康检查导致的,联系了网络运营服务客服人员,我把.pcap给 ...
- 001 centos7中的安装
以前学习过centos6.4的安装使用,然后新使用centos7,发现有些修改配置变化了,记录一下. 1.修改主机名 查看主机名: 修改: 二:ip的固定 1.参考文章 https://www.jia ...
- 原理分析dubbo分布式应用中使用zipkin做链路追踪(转)
作者:@nele本文为作者原创,转载请注明出处:https://www.cnblogs.com/nele/p/10171794.html 目录 zipkin是什么为什么使用Zipkinzipkin架构 ...
- Leetcode: Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...