Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4f488e4

Browse files
author
zongyanqi
committed
add Easy_504_Base_7
1 parent555086b commit4f488e4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎Easy_504_Base_7.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*
3+
* Given an integer, return its base 7 string representation.
4+
5+
Example 1:
6+
Input: 100
7+
Output: "202"
8+
9+
Example 2:
10+
Input: -7
11+
Output: "-10"
12+
13+
Note: The input will be in range of [-1e7, 1e7].
14+
15+
*/
16+
17+
/**
18+
*@param {number} num
19+
*@return {string}
20+
*/
21+
varconvertToBase7=function(num){
22+
23+
if(num===0)return'0';
24+
varstr='';
25+
varsign=num>0;
26+
num=Math.abs(num);
27+
while(num){
28+
str=(num%7)+str;
29+
num=Math.floor(num/7);
30+
}
31+
32+
returnsign ?str :'-'+str;
33+
34+
};
35+
36+
console.log(convertToBase7(0));
37+
console.log(convertToBase7(100));
38+
console.log(convertToBase7(-7));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp