10000 Base 7. · lexrus/LeetCode.swift@268a77f · GitHub
[go: up one dir, main page]

Skip to content

Commit 268a77f

Browse files
committed
Base 7.
1 parent 4743035 commit 268a77f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
D73A13E31B9C6FDB001579E8 /* 258.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A13E21B9C6FDB001579E8 /* 258.swift */; };
2121
D73A13E51B9C85C8001579E8 /* 263.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A13E41B9C85C8001579E8 /* 263.swift */; };
2222
D73A42381E668CDE00F34AD9 /* 520.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A42371E668CDE00F34AD9 /* 520.swift */; };
23+
D73A423A1E66929F00F34AD9 /* 504.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A42391E66929F00F34AD9 /* 504.swift */; };
2324
D743F3DC1C2EE7BF00A7BC6E /* 111.swift in Sources */ = {isa = PBXBuildFile; fileRef = D743F3DB1C2EE7BF00A7BC6E /* 111.swift */; };
2425
D7803DC21B95F1FE000C9270 /* 104.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7803DC11B95F1FE000C9270 /* 104.swift */; };
2526
D783CC9B1B2AC9160087FFC1 /* 226.swift in Sources */ = {isa = PBXBuildFile; fileRef = D783CC9A1B2AC9160087FFC1 /* 226.swift */; };
@@ -69,6 +70,7 @@
6970
D73A13E21B9C6FDB001579E8 /* 258.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 258.swift; sourceTree = "<group>"; };
7071
D73A13E41B9C85C8001579E8 /* 263.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 263.swift; sourceTree = "<group>"; };
7172
D73A42371E668CDE00F34AD9 /* 520.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 520.swift; sourceTree = "<group>"; };
73+
D73A42391E66929F00F34AD9 /* 504.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 504.swift; sourceTree = "<group>"; };
7274
D743F3DB1C2EE7BF00A7BC6E /* 111.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 111.swift; sourceTree = "<group>"; };
7375
D7803DC11B95F1FE000C9270 /* 104.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 104.swift; sourceTree = "<group>"; };
7476
D783CC9A1B2AC9160087FFC1 /* 226.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 226.swift; sourceTree = "<group>"; };
@@ -180,6 +182,7 @@
180182
D71C171C1C30E99000936BF7 /* 283.swift */,
181183
D79437E71C4895AC005BA62B /* 326.swift */,
182184
D7B95A0E1E5B227A0059C57D /* 461.swift */,
185+
D73A42391E66929F00F34AD9 /* 504.swift */,
183186
D73A42371E668CDE00F34AD9 /* 520.swift */,
184187
D7FB8E0B1AF899FE00286272 /* Supporting Files */,
185188
);
@@ -292,6 +295,7 @@
292295
D71C17321C33DA1200936BF7 /* 155.swift in Sources */,
293296
D71CE8411B031E9A00D8A35F /* 165.swift in Sources */,
294297
D73A13E31B9C6FDB001579E8 /* 258.swift in Sources */,
298+
D73A423A1E66929F00F34AD9 /* 504.swift in Sources */,
295299
D7CB678A1C3E647100C4209D /* 114.swift in Sources */,
296300
D7FB8E2E1AF89B0400286272 /* 6.swift in Sources */,
297301
D7DE016B1AF9F13600BB2AA0 /* 100.swift in Sources */,

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Travis](https://img.shields.io/travis/lexrus/LeetCode.swift.svg)](https://travis-ci.org/lexrus/LeetCode.swift)
44
![Language](https://img.shields.io/badge/language-Swift%203-orange.svg)
55
![License](https://img.shields.io/github/license/lexrus/ios-dev-playbook.svg?style=flat)
6-
![Progress](https://img.shields.io/badge/Progress-42%20%2F%20490%20=%208.57%25-ff69b4.svg)
6+
![Progress](https://img.shields.io/badge/Progress-43%20%2F%20490%20=%208.78%25-ff69b4.svg)
77

88
> 愚者千虑,必有一得。
99
@@ -489,7 +489,7 @@ let 😎 = "Easy", 😐 = "Medium", 😨 = "Hard", ㊙️ = "require premium mem
489489
- [ ] 501 😎 [Find Mode in Binary Search Tree](https://leetcode.comhttps://leetcode.com/problems/find-mode-in-binary-search-tree)
490490
- [ ] 502 😨 [IPO](https://leetcode.comhttps://leetcode.com/problems/ipo)
491491
- [ ] 503 😐 [Next Greater Element II](https://leetcode.comhttps://leetcode.com/problems/next-greater-element-ii)
492-
- [ ] 504 😎 [Base 7](https://leetcode.comhttps://leetcode.com/problems/base-7)
492+
- [x] 504 😎 [Base 7](https://leetcode.comhttps://leetcode.com/problems/base-7) [.swift](./Tests/504.swift)
493493
- [ ] 505 😐 [The Maze II](https://leetcode.comhttps://leetcode.com/problems/the-maze-ii) ㊙️
494494
- [ ] 506 😎 [Relative Ranks](https://leetcode.comhttps://leetcode.com/problems/relative-ranks)
495495
- [ ] 508 😐 [Most Frequent Subtree Sum](https://leetcode.comhttps://leetcode.com/problems/most-frequent-subtree-sum)

Tests/504.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// 504.swift
3+
// LeetCode
4+
//
5+
// Created by Lex on 3/1/17.
6+
// Copyright © 2017 Lex Tang. All rights reserved.
7+
//
8+
9+
/*
10+
Given an integer, return its base 7 string representation.
11+
12+
Example 1:
13+
Input: 100
14+
Output: "202"
15+
Example 2:
16+
Input: -7
17+
Output: "-10"
18+
Note: The input will be in range of [-1e7, 1e7].
19+
*/
20+
21+
import Foundation
22+
import XCTest
23+
24+
func base7(_ n: Int) -> String {
25+
if n < 0 {
26+
return "-" + base7(-n)
27+
}
28+
29+
if n < 7 {
30+
return String(n)
31+
}
32+
33+
return base7(n / 7) + String(n % 7)
34+
}
35+
36+
class TestBase7 : XCTestCase {
37+
38+
func testBase7() {
39+
XCTAssertEqual(base7(0), "0")
40+
XCTAssertEqual(base7(7), "10")
41+
XCTAssertEqual(base7(-7), "-10")
42+
XCTAssertEqual(base7(14), "20")
43+
XCTAssertEqual(base7(15), "21")
44+
}
45+
46+
}

0 commit comments

Comments
 (0)
0