首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 题目解析之 Add Digits

Leetcode 题目解析之 Add Digits

原创
作者头像
ruochen
发布于 2022-01-15 04:09:22
发布于 2022-01-15 04:09:22
1.3K00
代码可运行
举报
运行总次数:0
代码可运行

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?

Hint:

  1. A naive implementation of the above process is trivial. Could you come up with other methods?
  2. What are all the possible results?
  3. How do they occur, periodically or randomly?
  4. You may find this Wikipedia article useful.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public int addDigits(int num) {
        return 1 + (num - 1) % 9;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 258. Add Digits
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
triplebee
2018/01/12
5540
LeetCode笔记:258.Add Digits
首先想到的就是循环,对于一个数字,循环将其除以10的余数加起来,直到其是个位数。加完一次后判断是不是没数字了,也就是等于0,如果还大于0,说明还有多个数字,那就再进行同样的操作。
Cloudox
2021/11/23
1530
关关的刷题日记81 – Leetcode 258. Add Digits
关关的刷题日记81 – Leetcode 258. Add Digits 题目 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. Fol
WZEARW
2018/04/12
6360
leetcode-258-Add Digits
题目描述: 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 an
chenjx85
2018/05/21
5920
Baozi Training Leetcode Solution 258: Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
包子面试培训
2019/08/01
3900
Baozi Training Leetcode Solution 258: Add Digits
LeetCode 0258 - Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Reck Zhang
2021/08/11
3990
LeetCode 258.Add digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
大学里的混子
2018/11/02
3480
LintCode Add Digits分析代码
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
desperate633
2018/08/22
2360
Leetcode 题目解析之 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
ruochen
2022/01/14
1.2K0
Leetcode 题目解析之 Count Primes
As you can see, calculations of 4 × 3 and 6 × 2 are not necessary. Therefore, we only need to consider factors up to √n because, if n is divisible by some number p, then n = p × q and since p ≤ q, we could derive that p ≤ √n.
ruochen
2022/01/10
1.4K0
LeetCode 2 & 455 Add Two Numbers I&II
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
大学里的混子
2018/11/03
4350
LeetCode 题目解答——第 227 到 310 题
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
1.2K0
LeetCode 题目解答——第 227 到 310 题
Leetcode 题目解析之 Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
ruochen
2022/01/09
1.3K0
Leetcode 算法 - 2. Add Two Numbers
需要注意的是: 这里使用的ListNode不是Python内置的list对象, 而是传统(计算机科学)意义上的列表, 通常叫做Linked list(链表), 通常由一系列节点实现, 其每个节点中都持有一个指向下一个节点的引用. 这里就是next属性. 链表分为单向链表和双向链表. 双向链表持有一个指向前一节点的引用.
用户1416054
2018/08/02
2440
Leetcode 2 Add Two Numbers
题目没有说清楚,trick挺多,考察细心程度的水题吧。 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4
triplebee
2018/01/12
4780
LeetCode 0002 - Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Reck Zhang
2021/08/11
1620
图解 LeetCode 链表: 2. Add Two Numbers
今天是 LeetCode 算法的 第 1 阶段第 4 天 ,这一阶段主要学习链表相关的算法题和链表数据结构。
用户2932962
2019/08/16
6180
图解 LeetCode 链表: 2. Add Two Numbers
LeetCode 2. Add Two Numbers题目分析代码
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
desperate633
2018/08/22
4390
【leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
阳光岛主
2019/02/19
3970
Leetcode 题目解析之 Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
ruochen
2022/01/15
1.3K0
相关推荐
Leetcode 258. Add Digits
更多 >
交个朋友
加入云原生工作实战群
云原生落地实践 技术难题攻坚探讨
加入MCP头号玩家交流群
云原生运维进阶交流 MCP认证经验分享
加入前端学习入门群
前端基础系统教学 经验分享避坑指南
换一批
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档