LeetCode之Add Digits
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.
2、代码实现
public class Solution {public int getAllSum(int val) {int sum = 0;while (val > 0) {sum += val % 10;val /= 10;}return sum;}public int addDigits(int num) {if (num <= 0) return 0;while (num >= 10) {num = getAllSum(num);}return num;}
}
LeetCode之Add Digits相关推荐
- [leetcode][math] Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode 258 Add Digits
leetcode 上做了一题比较有意思,记录一下,传送门:https://leetcode.com/problems/add-digits/description/ 题目意思是:给一个正整数a,让其个 ...
- leetcode Add Digits
题目连接 https://leetcode.com/problems/add-digits/ Add Digits Description Given a non-negative integer n ...
- Leetcode刷题记录[python]——258 Add Digits
一.前言 做这题有个小收获,关于Digital root的解法,有个极方便的小公式: 二.题258 Add Digits Given a non-negative integer num, repea ...
- LeetCode之Add Two Numbers
LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...
- LeetCode 445. Add Two Numbers II
LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...
- 【注意】LeetCode 2. Add Two Numbers
LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...
- [leetcode]623. Add One Row to Tree
[leetcode]623. Add One Row to Tree Analysis 今天要吃柚子-- [每天刷题并不难0.0] Given the root of a binary tree, t ...
最新文章
- 如何将重复的数据标红_python如何处理重复值数据?
- OpenMP入门教程(二)reduce sum
- 拷贝Python对象、浅拷贝、深拷贝
- Struts.xml配置解释
- linux设备驱动学习(四)——阻塞与非阻塞I/O
- web.xml中的所有配置,Listener和Filter的加载顺序
- python爬虫京东中文乱码_python3爬虫中文乱码之请求头‘Accept-Encoding’:br 的问题...
- Linux中变量#,@,0,1,2,*,$$,$?的含义
- iphone:MKMapView
- 【Vue 3.0 新特性(三)】Vite 工具
- 【java学习之路】(mysql篇)001.mysql基本介绍、常用命令及简单查询
- Android中处理崩溃异常和分析日志的两种思路
- 计算机专业电路基础高考试卷,计算机专业电路基础试题(4页)-原创力文档
- 360服务器linux版,360浏览器Linux版
- python什么叫索引_python中索引是什么意思(一文详解其定义)
- 【R语言】结巴分词与词性提取(以“提取知乎问题标题的频繁词前100个形容词”实战为例)(3月25日学习笔记)
- 25张用Photoshop绘制的顶级图片
- android 列表倒计时,Android ListView列表实现倒计时
- I3D泛读【Que Vadis,Action Recognition?A New Model and the Kinetics Dataset】
- Android传感器——传感器监听器及摇一摇案例
热门文章
- Istio 首次安全评估结果公布
- String是一个奇怪的引用类型
- NET问答: Find() 和 Where().FirstOrDefault() 该如何选择 ?
- 运维小姐姐说这篇Consul集群和ACL配置超给力(保姆级)
- 微软2020开源回顾:止不住的挨骂,停不下的贡献
- 如何使用 C# 在异步代码中处理异常
- 寻找性能更优秀的不可变小字典
- dapr微服务.net sdk入门
- .NET Core 小程序开发零基础系列(1)——开发者启用并牵手成功
- 一本让我多花2倍时间读的书