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

Commit 843eaa6

Browse files
author
Michael Ho
committed
New updates are coming in
1 parent b4d6cc3 commit 843eaa6

File tree

28 files changed

+966
-73
lines changed

28 files changed

+966
-73
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Leetcode: https://leetcode.com/problems/balanced-binary-tree/
2+
3+
import XCTest
4+
5+
public class TreeNode {
6+
public var val: Int
7+
public var left: TreeNode?
8+
public var right: TreeNode?
9+
public init(_ val: Int) {
10+
self.val = val
11+
self.left = nil
12+
self.right = nil
13+
}
14+
}
15+
16+
class Solution {
17+
func isBalanced(_ root: TreeNode?) -> Bool {
18+
return false
19+
}
20+
21+
private func getTreeHeight(_ node: TreeNode?) {
22+
23+
}
24+
}
25+
26+
class Tests: XCTestCase {
27+
func testSample1() {
28+
let arr = [3, 9, 20, nil, nil, 15, 7]
29+
let root = convertArrToTree(arr)
30+
inOrderPrint(root)
31+
}
32+
33+
private func convertArrToTree(_ arr: [Int?]) -> TreeNode? {
34+
guard arr.count > 0 else {
35+
return nil
36+
}
37+
let root = TreeNode(arr[0]!)
38+
convert(0, root, arr)
39+
return root
40+
}
41+
42+
private func convert(_ index: Int, _ root: TreeNode?, _ arr: [Int?]) {
43+
guard let root = root, index*2 + 2 < arr.count else { return }
44+
45+
if let leftVal = arr[index*2+1] {
46+
root.left = TreeNode(leftVal)
47+
convert(index*2+1, root.left, arr)
48+
}
49+
50+
if let rightVal = arr[index*2+2] {
51+
root.right = TreeNode(rightVal)
52+
convert(index*2+2, root.right, arr)
53+
}
54+
}
55+
56+
func inOrderPrint(_ root: TreeNode?) {
57+
guard let root = root else {
58+
return
59+
}
60+
if let left = root.left {
61+
inOrderPrint(left)
62+
}
63+
print(root.val)
64+
if let right = root.right {
65+
inOrderPrint(right)
66+
}
67+
}
68+
}
69+
70+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// LeetCode: https://leetcode.com/problems/find-mode-in-binary-search-tree/
2+
3+
public class TreeNode {
4+
public var val: Int
5+
public var left: TreeNode?
6+
public var right: TreeNode?
7+
public init(_ val: Int) {
8+
self.val = val
9+
self.left = nil
10+
self.right = nil
11+
}
12+
}
13+
14+
class Solution {
15+
func findMode(_ root: TreeNode?) -> [Int] {
16+
guard let root = root else { return [] }
17+
var dict: [Int:Int] = [:]
18+
var max = 0
19+
findMode(root, &dict, &max)
20+
21+
var output = [Int]()
22+
for i in dict.keys {
23+
if let val = dict[i], val == max {
24+
output.append(i)
25+
}
26+
}
27+
return output
28+
}
29+
30+
private func findMode(_ root: TreeNode, _ dict: inout [Int:Int], _ max: inout Int) {
31+
if let val = dict[root.val] {
32+
dict[root.val] = val + 1
33+
max = val + 1 > max ? val + 1 : max
34+
} else {
35+
dict[root.val] = 1
36+
max = 1 > max ? 1 : max
37+
}
38+
39+
if let left = root.left {
40+
findMode(left, &dict, &max)
41+
}
42+
43+
if let right = root.right {
44+
findMode(right, &dict, &max)
45+
}
46+
}
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='ios'>
2+
<playground version='5.0' target-platform='macos'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Easy/0001-TwoSum.playground/Contents.swift renamed to Easy/EasyProblems.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// LeetCode: https://leetcode.com/problems/two-sum/description/
22
// Solution: https://github.com/zhubofei/LeetCode-Swift/blob/master/0001-two-sum.playground/Contents.swift
33

4-
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
4+
publicfunc twoSum(_ nums: [Int], _ target: Int) -> [Int] {
55
var dict: [Int: Int] = [:] // [number : indexPosition]
66
for (index, num) in nums.enumerated() {
77
let complement = target - num
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

‎Medium/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift‎

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,51 @@
44
import XCTest
55

66
class Solution {
7+
// func lengthOfLongestSubstring(_ s: String) -> Int {
8+
// guard s.count > 0 else {
9+
// return 0
10+
// }
11+
//
12+
// var maxCount = 1
13+
// if s.count == 1 {
14+
// return maxCount
15+
// }
16+
// let strArr = Array(s) // Key: convert string to array
17+
// for (index, char) in strArr.enumerated() {
18+
// var dict: [Character : Int] = [:]
19+
// var count = 1
20+
// dict[char] = 1
21+
// var movingIdx = index + 1
22+
// while movingIdx < strArr.count, dict[strArr[movingIdx]] ?? 0 < 1 {
23+
// dict[strArr[movingIdx]] = 1
24+
// count += 1
25+
// movingIdx += 1
26+
// }
27+
// maxCount = count > maxCount ? count : maxCount
28+
// }
29+
// return maxCount
30+
// }
31+
// 14
732
func lengthOfLongestSubstring(_ s: String) -> Int {
8-
guard s.count > 0 else {
9-
return 0
10-
}
11-
12-
var maxCount = 1
13-
if s.count == 1 {
14-
return maxCount
15-
}
16-
let strArr = Array(s) // Key: convert string to array
17-
for (index, char) in strArr.enumerated() {
18-
var dict: [Character : Int] = [:]
19-
var count = 1
20-
dict[char] = 1
21-
var movingIdx = index + 1
22-
while movingIdx < strArr.count, dict[strArr[movingIdx]] ?? 0 < 1 {
23-
dict[strArr[movingIdx]] = 1
33+
let arr = Array(s)
34+
var dict = [Character : Int]() // [char : idx]
35+
var maxCount = 0
36+
var count = 0
37+
for idx in 0..<arr.count {
38+
if dict[arr[idx]] == nil {
2439
count += 1
25-
movingIdx += 1
40+
dict[arr[idx]] = idx
41+
} else {
42+
let startIdx = dict[arr[idx]]! + 1
43+
dict[arr[idx]] = idx
44+
for key in dict.keys {
45+
if dict[key]! < startIdx {
46+
dict[key] = nil
47+
}
48+
}
49+
count = idx - startIdx + 1
2650
}
27-
maxCount = count>maxCount? count : maxCount
51+
maxCount = max(count,maxCount)
2852
}
2953
return maxCount
3054
}
@@ -38,12 +62,18 @@ class Tests: XCTestCase {
3862
let expected = 3
3963
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
4064
}
41-
65+
4266
func testSample2() {
4367
let sample = "abcabcbb"
4468
let expected = 3
4569
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
4670
}
71+
72+
func testSample3() {
73+
let sample = "pwwkew"
74+
let expected = 3
75+
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
76+
}
4777
}
4878

4979
Tests.defaultTestSuite.run()

‎Medium/0005-LongestPalindromicSubstring.playground/Contents.swift‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,26 @@ class Solution {
1111
var strArr = Array(s)
1212

1313
while length > 0 {
14-
for i in 0...s.count-length {
15-
var left = i
16-
var right = i+length-1
17-
18-
if right >= strArr.count {
19-
break
20-
}
21-
14+
for i in 0...strArr.count-length {
2215
var isPalindrome = true
23-
while left < right {
24-
if strArr[left] != strArr[right] {
16+
var left = i
17+
var right = left + length - 1
18+
while right > left {
19+
if strArr[left] == strArr[right] {
20+
left += 1
21+
right -= 1
22+
} else {
2523
isPalindrome = false
2624
break
2725
}
28-
29-
left += 1
30-
right -= 1
3126
}
32-
3327
if isPalindrome {
3428
return String(strArr[i..<i+length])
3529
}
3630
}
3731
length -= 1
3832
}
39-
return String(strArr[0])
33+
return ""
4034
}
4135
}
4236

@@ -60,7 +54,7 @@ class Tests: XCTestCase {
6054
let expected = "a"
6155
XCTAssertEqual(s.longestPalindrome(input), expected)
6256
}
63-
57+
6458
func testSample4() {
6559
let input = "abcda"
6660
let expected = "a"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Leetcode: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
2+
3+
import XCTest
4+
5+
public class TreeNode {
6+
public var val: Int
7+
public var left: TreeNode?
8+
public var right: TreeNode?
9+
public init(_ val: Int) {
10+
self.val = val
11+
self.left = nil
12+
self.right = nil
13+
}
14+
}
15+
16+
class Solution {
17+
func flatten(_ root: TreeNode?) {
18+
19+
}
20+
}
21+
22+
class Tests: XCTestCase {
23+
let s = Solution()
24+
25+
func testSample1() {
26+
let input = convertArrToTree([1, 2, 5, 3, 4, nil, 6])
27+
}
28+
29+
private func convertArrToTree(_ arr: [Int?]) -> TreeNode? {
30+
guard arr.count > 0 else {
31+
return nil
32+
}
33+
let root = TreeNode(arr[0]!)
34+
convert(0, root, arr)
35+
return root
36+
}
37+
38+
private func convert(_ index: Int, _ root: TreeNode?, _ arr: [Int?]) {
39+
guard let root = root, index*2 + 2 < arr.count else { return }
40+
41+
if let leftVal = arr[index*2+1] {
42+
root.left = TreeNode(leftVal)
43+
convert(index*2+1, root.left, arr)
44+
}
45+
46+
if let rightVal = arr[index*2+2] {
47+
root.right = TreeNode(rightVal)
48+
convert(index*2+2, root.right, arr)
49+
}
50+
}
51+
}
52+
53+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='tvos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /