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+
classSolution{
4+
func removeElement(_ nums:inout[Int], _ val:Int)->Int{
5+
nums = nums.filter{ $0 != val }
6+
return nums.count
7+
}
8+
}
9+
10+
letsolution=Solution()
11+
vararr1=[3,2,2,3]
12+
print("\(solution.removeElement(&arr1,3))") // 2
13+
print(arr1)
14+
vararr2=[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+
<playgroundversion='5.0'target-platform='ios'>
3+
<timelinefileName='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+
classSolution{
4+
func strStr(_ haystack:String, _ needle:String)->Int{
5+
varindex=-1
6+
guard needle.count >0else{
7+
return0
8+
}
9+
varnArr=Array(needle)
10+
varhArr=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+
letsolution=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+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='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+
classSolution{
4+
func searchInsert(_ nums:[Int], _ target:Int)->Int{
5+
varoutput=0
6+
foriin0..<nums.count {
7+
if target >nums[i]{
8+
output = i +1
9+
}elseif target ==nums[i]{
10+
output = i
11+
}
12+
}
13+
return output
14+
}
15+
}
16+
17+
letsolution=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+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='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+
classSolution{
4+
func maxSubArray(_ nums:[Int])->Int{
5+
varmax=nums[0]
6+
foriin0..<nums.count {
7+
varcurrent=nums[i]
8+
max = current > max ? current : max
9+
varindex= 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+
letsolution=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+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='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+
classSolution{
4+
func maxArea(_ height:[Int])->Int{
5+
vararea=0
6+
varstart=0
7+
varend= height.count -1
8+
while start < end {
9+
letareaH=min(height[start],height[end])
10+
letwidth= end - start
11+
if areaH * width > area {
12+
area = areaH * width
13+
}
14+
ifheight[start]<height[end]{
15+
start +=1
16+
}else{
17+
end -=1
18+
}
19+
}
20+
return area
21+
}
22+
}
23+
24+
letsolution=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+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
(0)