-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path504.七进制数.cs
56 lines (55 loc) · 1.01 KB
/
504.七进制数.cs
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
55
/*
* @lc app=leetcode.cn id=504 lang=csharp
*
* [504] 七进制数
*
* https://leetcode-cn.com/problems/base-7/description/
*
* algorithms
* Easy (45.53%)
* Likes: 20
* Dislikes: 0
* Total Accepted: 5.3K
* Total Submissions: 11.4K
* Testcase Example: '100'
*
* 给定一个整数,将其转化为7进制,并以字符串形式输出。
*
* 示例 1:
*
*
* 输入: 100
* 输出: "202"
*
*
* 示例 2:
*
*
* 输入: -7
* 输出: "-10"
*
*
* 注意: 输入范围是 [-1e7, 1e7] 。
*
*/
using System.Text;
using System.Linq;
public class Solution {
public string ConvertToBase7(int num) {
if(num == 0)
{
return "0";
}
if(num < 0)
{
return "-" + ConvertToBase7(-1 * num);
}
StringBuilder sb = new StringBuilder();
while(num > 0)
{
sb.Append((num % 7).ToString());
num /= 7;
}
return new string(sb.ToString().Reverse().ToArray());
}
}