leetcode笔记--6 Add Digits

question:

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

answer:(which don‘t think by myself)

思路:

举例:

38,11,2    38-2=36(38-11=27,11-2=9,都是9的倍数)

57,12,3    57-3=54(57-12=45,12-3=9,都是9的倍数)

所以直接用57/9 得余数为3 ??

其实不对.......忘记了特殊情况(就是整除的时候)

18,9

余数为0,不符合上述方法

所以需要先减去一个数,最后余数再加上一个数,可以避免这种情况,但是可以随意先加后减一个数吗??

不可以,举例10-2=8 8对9取余数为8,8+2=10,不符合条件

所以只能是1

时间: 2024-10-12 16:59:57

leetcode笔记--6 Add Digits的相关文章

LeetCode No.258 Add Digits

LeetCode  Algorithum # title solution difficulty 258  Add Digits  java  easy         NO.258 (2015.10.26 16:09:00) Given a non-negative integer num,repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the pro

我也来刷LeetCode——3、Add Digits

唉,做完这道题目后,有种羞愧无比的感觉,果然还是读书太少... 什么意思呢?就像这道题目一样,要求你编程计算出从1加到100的结果.于是你用循环从1累加到100,结果为5050.而大家都知道,从数学上的角度来解答,结果就是(a[1]+a[n])* n / 2. LeetCode上面的这道[Add Digits]题,其实是道数学题.它这个求解的结果,在数学上称为数根. 原题大概意思是这样,给定一个正整数,循环累加它的各位,直到结果为一位数.如 num 是38,计算的过程是 3+8=11,1+1=2

【一天一道LeetCode】#258. Add Digits

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【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 any

LeetCode OJ: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】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. 参考这一篇wiki百科 https://en.wikipedia.

LeetCode OJ 之 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 withou

【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 21_Merge Two Sorted Lists & leetcode_258 Add Digits & leetcode_66plus one

l leetcode 21_Merge Two Sorted Lists 题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解法: #include <iostream> using namespace std; struct ListNode { int