[LeetCode] 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 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.

解题思路

思路1:一直相加直到num变为个位数。
思路2:套用维基公式。

实现代码

Java代码 1:

// Runtime: 2 ms
public class Solution {
    public int addDigits(int num) {
        int temp = 0;
        while (num > 9) {
            while (num != 0) {
                temp += num % 10;
                num /= 10;
            }
            num = temp;
            temp = 0;
        }

        return num;
    }
}

Java代码2:

// Runtime: 5 ms
public class Solution {
    public int addDigits(int num) {
        return (int)(num - 9 * Math.ceil((num - 1) / 9));
    }
}

Java代码3:

// Runtime: 2 ms
public class Solution {
    public int addDigits(int num) {
        return 1 + (num - 1) % 9;
    }
}
时间: 2024-09-26 09:55:53

[LeetCode] Add Digits的相关文章

[LeetCode] 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 a

LeetCode 258 Add Digits(数字相加,数字根)

翻译 给定一个非负整型数字,重复相加其所有的数字直到最后的结果只有一位数. 例如: 给定sum = 38,这个过程就像是:3 + 8 = 11,1 + 1 = 2,因为2只有一位数,所以返回它. 紧接着: 你可以不用循环或递归在O(1)时间内完成它吗? 原文 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given

[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

[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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

[LeetCode]66.Plus One

[题目] Given a number represented as an array of digits, plus one to the number. [题意] 给你一个用数组表示的数,求加一之后的结果,结果还是用数组表示. [分析] 从低位到高位,连续遇到9才能加一进位. [代码1] /********************************* * 日期:2014-01-22 * 作者:SJF0115 * 题号: Plus One * 来源:http://oj.leetcode.

[LeetCode] Plus One - 整数字符转换相加

题目概述:Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目解析:给你一个int型数组存储一个非负整数,对整数加1后输出一个int型数组.注意几点:         1.可能存在进位操作,增加一位,

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

UVa 621 Secret Research (water ver.)

621 - Secret Research Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=562 At a certain laboratory results of secret research are thoroughly encrypted. A r

JQuery validate插件验证用户注册信息_jquery

使用JQuery的validate插件做客户端验证非常方便,下面做一个使用validate插件验证用户注册信息的例子. 本实例使用的是1.5版本. 示例是在SSH下做的,代码如下: registe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC &quo