File tree 5 files changed +67
-0
lines changed
5 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,7 @@ All solutions will be accepted!
129
129
| 455| [ assign cookies] ( https://leetcode-cn.com/problems/assign-cookies/description/ ) | [ java/py/js] ( ./algorithms/AssignCookies ) | Easy|
130
130
| 415| [ Add Strings] ( https://leetcode-cn.com/problems/add-strings/description/ ) | [ java/py/js] ( ./algorithms/AddStrings ) | Easy|
131
131
| 67| [ Add Binary] ( https://leetcode-cn.com/problems/add-binary/description/ ) | [ java/py/js] ( ./algorithms/AddBinary ) | Easy|
132
+ | 504| [ Base 7] ( https://leetcode-cn.com/problems/base-7/description/ ) | [ java/py/js] ( ./algorithms/Base7 ) | Easy|
132
133
133
134
# Database
134
135
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Base 7
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String convertToBase7 (int num ) {
3
+ String res = "" ;
4
+ boolean isNegative = false ;
5
+
6
+ if (num == 0 ) {
7
+ res = "0" ;
8
+ } else {
9
+ if (num < 0 ) {
10
+ num = -num ;
11
+ isNegative = true ;
12
+ }
13
+
14
+ while (num > 0 ) {
15
+ res = String .valueOf (num % 7 ) + res ;
16
+ num /= 7 ;
17
+ }
18
+ }
19
+
20
+ return isNegative ? "-" + res : res ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } num
3
+ * @return {string }
4
+ */
5
+ var convertToBase7 = function ( num ) {
6
+ let res = '' ,
7
+ isNegative = false
8
+
9
+ if ( num === 0 ) {
10
+ res = '0'
11
+ } else {
12
+ if ( num < 0 ) {
13
+ num = - num
14
+ isNegative = true
15
+ }
16
+
17
+ while ( num > 0 ) {
18
+ res = String ( num % 7 ) + res
19
+ num = parseInt ( num / 7 )
20
+ }
21
+ }
22
+
23
+ return isNegative ? '-' + res : res
24
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def convertToBase7 (self , num ):
3
+ """
4
+ :type num: int
5
+ :rtype: str
6
+ """
7
+ res = ''
8
+ is_negative = False
9
+
10
+ if num == 0 :
11
+ res = '0'
12
+ if num < 0 :
13
+ num = - num
14
+ is_negative = True
15
+ while num > 0 :
16
+ res = str (num % 7 ) + res
17
+ num = num / 7
18
+ return '-' + res if is_negative else res
You can’t perform that action at this time.
0 commit comments