Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aabad5

Browse files
author
Michael Ho
committed
Added new solutions
1 parent fcb0715 commit 9aabad5

File tree

19 files changed

+367
-30
lines changed

19 files changed

+367
-30
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// LeetCode: https://leetcode.com/problems/remove-element/description/
2+
3+
class Solution {
4+
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
5+
nums = nums.filter { $0 != val }
6+
return nums.count
7+
}
8+
}
9+
10+
let solution = Solution()
11+
var arr1 = [3,2,2,3]
12+
print("\(solution.removeElement(&arr1, 3))") // 2
13+
print(arr1)
14+
var arr2 = [0,1,2,2,3,0,4,2]
15+
print("\(solution.removeElement(&arr2, 2))") // 5
16+
print(arr2)
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'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// LeetCode: https://leetcode.com/problems/implement-strstr/description/
2+
3+
class Solution {
4+
func strStr(_ haystack: String, _ needle: String) -> Int {
5+
var index = -1
6+
guard needle.count > 0 else {
7+
return 0
8+
}
9+
var nArr = Array(needle)
10+
var hArr = Array(haystack)
11+
for (idx, c) in hArr.enumerated() {
12+
if c == nArr[0], idx + (nArr.count) <= hArr.count, String(hArr[idx..<idx + (nArr.count)]) == needle {
13+
index = idx
14+
break
15+
}
16+
}
17+
return index
18+
}
19+
}
20+
21+
let solution = Solution()
22+
print(solution.strStr("hello", "ll")) // 2
23+
print(solution.strStr("aaa", "aa")) // 0
24+
print(solution.strStr("aaa", "a")) // 0
25+
print(solution.strStr("", "")) // 0
26+
print(solution.strStr("a", "a")) // 0
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>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// LeetCode: https://leetcode.com/problems/search-insert-position/description/
2+
3+
class Solution {
4+
func searchInsert(_ nums: [Int], _ target: Int) -> Int {
5+
var output = 0
6+
for i in 0..<nums.count {
7+
if target > nums[i] {
8+
output = i + 1
9+
} else if target == nums[i] {
10+
output = i
11+
}
12+
}
13+
return output
14+
}
15+
}
16+
17+
let solution = Solution()
18+
print(solution.searchInsert([1,3,5,6], 5)) // 2
19+
print(solution.searchInsert([1,3,5,6], 2)) // 1
20+
print(solution.searchInsert([1,3,5,6], 7)) // 4
21+
print(solution.searchInsert([1,3,5,6], 0)) // 0
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>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// LeetCode: https://leetcode.com/problems/maximum-subarray/description/
2+
3+
class Solution {
4+
func maxSubArray(_ nums: [Int]) -> Int {
5+
var max = nums[0]
6+
for i in 0..<nums.count {
7+
var current = nums[i]
8+
max = current > max ? current : max
9+
var index = i + 1
10+
while index < nums.count {
11+
current += nums[index]
12+
max = current > max ? current : max
13+
index += 1
14+
}
15+
}
16+
return max
17+
}
18+
}
19+
20+
let solution = Solution()
21+
print(solution.maxSubArray([-2,1,-3,4,-1,2,1,-5,4])) // 6
22+
print(solution.maxSubArray([-2,1])) // 1
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>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// LeetCode: https://leetcode.com/problems/container-with-most-water/description/
2+
3+
class Solution {
4+
func maxArea(_ height: [Int]) -> Int {
5+
var area = 0
6+
var start = 0
7+
var end = height.count - 1
8+
while start < end {
9+
let areaH = min(height[start], height[end])
10+
let width = end - start
11+
if areaH * width > area {
12+
area = areaH * width
13+
}
14+
if height[start] < height[end] {
15+
start += 1
16+
} else {
17+
end -= 1
18+
}
19+
}
20+
return area
21+
}
22+
}
23+
24+
let solution = Solution()
25+
print("\(solution.maxArea([1,8,6,2,5,4,8,3,7]))") // 49
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)