Skip to content

Commit bff5ebf

Browse files
author
Michael Ho
committed
Push new solutions
1 parent d1c76c4 commit bff5ebf

File tree

17 files changed

+464
-52
lines changed

17 files changed

+464
-52
lines changed

Problems/1-100/Medium/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@ import XCTest
55

66
class Solution {
77
func lengthOfLongestSubstring(_ s: String) -> Int {
8-
let arr = Array(s)
9-
var dict = [Character : Int]() // [char : idx]
8+
guard s.count > 1 else { return s.count }
9+
var dict: [Character: Int] = [:]
10+
var startIdx = 0
1011
var maxCount = 0
11-
var count = 0
12-
for idx in 0..<arr.count {
13-
if dict[arr[idx]] == nil {
14-
count += 1
15-
dict[arr[idx]] = idx
16-
} else {
17-
let startIdx = dict[arr[idx]]! + 1
18-
dict[arr[idx]] = idx
19-
for key in dict.keys {
20-
if dict[key]! < startIdx {
21-
dict[key] = nil
22-
}
23-
}
24-
count = idx - startIdx + 1
12+
13+
for (i, ch) in s.enumerated() {
14+
if let existIdx = dict[ch] {
15+
startIdx = max(startIdx, existIdx)
2516
}
26-
maxCount = max(count, maxCount)
17+
18+
// Include the current one, exclude the existed one
19+
maxCount = max(maxCount, i - startIdx + 1)
20+
21+
// Make the index be excluded
22+
dict[ch] = i + 1
2723
}
24+
2825
return maxCount
2926
}
3027
}
@@ -49,6 +46,12 @@ class Tests: XCTestCase {
4946
let expected = 3
5047
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
5148
}
49+
50+
func testSample4() {
51+
let sample = "au"
52+
let expected = 2
53+
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
54+
}
5255
}
5356

5457
Tests.defaultTestSuite.run()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// LeetCode: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func searchRange(_ nums: [Int], _ target: Int) -> [Int] {
7+
var output = [-1, -1]
8+
guard nums.count > 0 else { return output }
9+
10+
if nums.count == 1 {
11+
return target == nums[0] ? [0, 0] : output
12+
}
13+
14+
var start = 0
15+
var end = nums.count - 1
16+
while start <= end {
17+
let mid = (start+end)/2
18+
if target == nums[mid] {
19+
output = [mid, mid]
20+
break
21+
} else if target < nums[mid] {
22+
end = mid - 1
23+
} else {
24+
start = mid + 1
25+
}
26+
}
27+
28+
while output[0] - 1 >= 0, nums[output[0] - 1] == target {
29+
output[0] -= 1
30+
}
31+
32+
while output[1] + 1 < nums.count, nums[output[1] + 1] == target {
33+
output[1] += 1
34+
}
35+
36+
return output
37+
}
38+
}
39+
40+
class Tests: XCTestCase {
41+
let s = Solution()
42+
43+
func testSample1() {
44+
let input = ([5,7,7,8,8,10], 8)
45+
let expected = [3, 4]
46+
XCTAssertEqual(expected, s.searchRange(input.0, input.1))
47+
}
48+
49+
func testSample2() {
50+
let input = ([5,7,7,8,8,10], 6)
51+
let expected = [-1, -1]
52+
XCTAssertEqual(expected, s.searchRange(input.0, input.1))
53+
}
54+
55+
func testSample3() {
56+
let input = ([1], 1)
57+
let expected = [0, 0]
58+
XCTAssertEqual(expected, s.searchRange(input.0, input.1))
59+
}
60+
61+
func testSample4() {
62+
let input = ([1, 4], 4)
63+
let expected = [1, 1]
64+
XCTAssertEqual(expected, s.searchRange(input.0, input.1))
65+
}
66+
}
67+
68+
Tests.defaultTestSuite.run()
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>

Problems/1-100/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Straightforward solutions
1717
| # | Difficulty | Problem Title | Solution |
1818
|--| :--: | :--: | -- |
1919
| 0001 | 📗 | [Two Sum](https://leetcode.com/problems/two-sum/description/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Easy/0001-TwoSum.playground/Contents.swift) |
20-
| 0002 | 📙 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/0002-AddTwoNumbers.playground/Contents.swift) |
20+
| 0002 | 📙 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Medium/0002-AddTwoNumbers.playground/Contents.swift) |
2121
| 0003 | 📙 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Medium/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift) |
2222
| 0004 | 📕 | [Median of Two Sorted Arrays ](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Hard/0004-MedianOfTwoSortedArrays.playground/Contents.swift) |
2323
| 0005 | 📙 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Medium/0005-LongestPalindromicSubstring.playground/Contents.swift) |
@@ -43,6 +43,7 @@ Straightforward solutions
4343
| 0028 | 📗 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/description/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Easy/0028-ImplementstrStr().playground/Contents.swift)|
4444
| 0029 | 📙 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/description/) | [Solution](https://github.com/twho/LeetCode-Swift/tree/master/Problems/1-100/Medium/0029-DivideTwoIntegers.playground/Contents.swift) |
4545
| 0033 | 📙 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [Solution](https://github.com/twho/LeetCode-Swift/tree/master/Problems/1-100/Medium/0033-SearchInRotatedSortedArray.playground/Contents.swift)|
46+
| 0034 | 📙 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [Solution](https://github.com/twho/LeetCode-Swift/tree/master/Problems/1-100/Medium/0034-FindFirstandLastPositionElementSortedArray.playground/Contents.swift)|
4647
| 0035 | 📗 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Solution](https://github.com/twho/LeetCode-Swift/blob/master/Problems/1-100/Easy/0035-SearchInsertPosition.playground/Contents.swift) |
4748
| 0039 | 📙 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [Solution](https://github.com/twho/LeetCode-Swift/tree/master/Problems/1-100/Medium/0039-CombinationSum.playground/Contents.swift) |
4849
|0040 | 📙 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [Solution](https://github.com/twho/LeetCode-Swift/tree/master/Problems/1-100/Medium/0040-CombinationSumII.playground/Contents.swift)|
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// LeetCode: https://leetcode.com/problems/symmetric-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 isSymmetric(_ root: TreeNode?) -> Bool {
18+
var arr = [Int?]()
19+
inOrder(root, &arr)
20+
21+
if arr.count%2 == 0 {
22+
return false
23+
} else {
24+
for i in 0..<arr.count/2 {
25+
if arr[i] != arr[arr.count-1-i] {
26+
return false
27+
}
28+
}
29+
}
30+
return true
31+
}
32+
33+
private func inOrder(_ root: TreeNode?, _ arr: inout [Int?]) {
34+
if root?.left == nil, root?.right == nil {
35+
arr.append(root?.val)
36+
return
37+
}
38+
inOrder(root?.left, &arr)
39+
arr.append(root?.val)
40+
inOrder(root?.right, &arr)
41+
}
42+
}
43+
44+
class Tests: XCTestCase {
45+
let s = Solution()
46+
47+
func testSample1() {
48+
let input = buildTree([
49+
1,
50+
2, 2,
51+
3, 4, 4, 3
52+
])
53+
XCTAssertTrue(s.isSymmetric(input))
54+
}
55+
56+
func testSample2() {
57+
let input = buildTree([
58+
1,
59+
2, 2,
60+
2, nil, 2
61+
])
62+
XCTAssertFalse(s.isSymmetric(input))
63+
}
64+
65+
func testSample3() {
66+
let input = buildTree([
67+
5,
68+
4,1,
69+
nil,1,nil,4,
70+
2,nil,2,nil
71+
])
72+
XCTAssertFalse(s.isSymmetric(input))
73+
}
74+
75+
private func buildTree(_ arr: [Int?]) -> TreeNode? {
76+
guard arr.count > 0, let first = arr[0] else { return nil }
77+
let root = TreeNode(first)
78+
buildTree(0, root, arr)
79+
return root
80+
}
81+
82+
private func buildTree(_ index: Int, _ root: TreeNode?, _ arr: [Int?]) {
83+
guard let root = root else { return }
84+
85+
if index < arr.count {
86+
if 2*index+1 < arr.count, let val = arr[2*index+1] {
87+
root.left = TreeNode(val)
88+
buildTree(2*index+1, root.left, arr)
89+
}
90+
91+
if 2*index+2 < arr.count, let val = arr[2*index+2] {
92+
root.right = TreeNode(val)
93+
buildTree(2*index+2, root.right, arr)
94+
}
95+
}
96+
}
97+
}
98+
99+
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='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)