Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
HCThink
h-blog
Repository navigation
Code
Issues
6
(6)
Pull requests
1
(1)
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
h-blog
/
algorithm
/
leecode
/
lee258.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
54 lines (46 loc) · 1.61 KB
master
Breadcrumbs
h-blog
/
algorithm
/
leecode
/
lee258.js
Copy path
Top
File metadata and controls
Code
Blame
54 lines (46 loc) · 1.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 给定一个非负整数 num, 反复将各个位上的数字相加, 直到结果为一位数。
示例:
输入: 38
输出: 2
解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数, 所以返回 2。
进阶:
你可以不使用循环或者递归, 且在 O(1) 时间复杂度内解决这个问题吗?
*/
/**
* @param {number} num
* @return {number}
* 此题需要数学知识,反正我是一脸懵逼。
*
* https://www.zhihu.com/question/30972581
首先: 12345 = 1 * 9999 + 2 * 999 + 3 * 99 + 4 * 9 +
5 + (1 + 2 + 3 + 4 + 5) 只要证明: 12345 % 9 = (1 + 2 + 3 + 4 + 5) % 9 就能往下递推了。 那么, 我们已知: m % 9 = a;
n % 9 = b 即 m = 9 * x +
a;
n = 9 * y + b; 可推出(m + n) % 9 = a + b = m % 9 + n % 9;[1 * 9999 + 2 * 999 + 3 * 99 + 4 * 9 + (1 +
2 + 3 + 4 + 5)] % 9 = (1 * 9999) % 9 + (2 * 999) % 9 + (3 * 99) % 9 + (4 * 9) %
9 + (1 + 2 + 3 + 4 + 5) % 9 = 0 + 0 + 0 + 0 + (1 + 2 + 3 + 4 + 5) % 9 = (1 + 2 +
3 + 4 + 5) % 9。 证明完成: 12345 % 9 = (1 + 2 + 3 + 4 + 5) % 9;
因为题中最后一个数恰好是小于10, 与取mod 9 结束也一致, 所以:(12345) % 9 = (1 + 2 + 3 + 4 + 5) % 9 = 12 %
9 = (1 + 2) % 9 = 3 % 9 = 3。
*/
var addDigits = function (num) {
return (num - 1) % 9 + 1;
};
const re = addDigits(1);
console.log(re);
/**
* leecode 耗时最少解法
* @param {number} num
* @return {number}
*/
var addDigits = function (num) {
if (num == 0) {
return 0;
}
if (num % 9 == 0) {
return 9;
} else {
return num % 9;
}
};
You can’t perform that action at this time.