Skip to content

Commit a5edfc1

Browse files
committed
Solved linkedList, array n string leetcode problems.
1 parent 689e381 commit a5edfc1

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import UIKit
2+
3+
//Two pointer approach https://leetcode.com/problems/3sum-closest/description/
4+
/* In the solution above, sorting the array takes O(nlog(n)) and the nested loop takes O(n2) to find the triplet. Here, n
5+
is the number of elements in the input array. Therefore, the total time complexity of this solution is O(nlog(n)+n2 ) , which simplifies to O(n
6+
2). */
7+
8+
func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
9+
let sortedArray = nums.sorted()
10+
let length = sortedArray.count
11+
12+
var difference : Int = .max
13+
var result = 0
14+
15+
for index in 0..<length - 1 {
16+
var low = index + 1
17+
var high = length - 1
18+
19+
while low < high {
20+
let sum = sortedArray[index] + sortedArray[low] + sortedArray[high]
21+
sum > target ? (high -= 1) : (low += 1)
22+
23+
let closeDifference = abs(sum - target)
24+
25+
if closeDifference < difference {
26+
difference = closeDifference
27+
result = sum
28+
}
29+
30+
}
31+
}
32+
return result
33+
}
34+
let nums = [-1,2,1,-4]
35+
print(threeSumClosest(nums, 1))
36+
37+
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' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

DataStrucutureQuestions/LinkedList/19. Remove Nth Node From End of List.playground/Contents.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import UIKit
22

3+
//Problem:- https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
4+
5+
//Leetcode Solution : https://leetcode.com/problems/remove-nth-node-from-end-of-list/solutions/4180205/19-remove-nth-node-from-end-of-list-time-complexity-is-o-n-space-complexity-o-1/
6+
37

48
public class ListNode {
59
public var val : Int?
@@ -27,25 +31,26 @@ extension ListNode : CustomStringConvertible {
2731
}
2832

2933
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
30-
var dummyNode = ListNode(0, head)
31-
var left = dummyNode
34+
var left = head
3235
var right = head
3336
var index = n
3437
// advance right to nth position
3538
while index > 0 {
3639
right = right?.next
3740
index -= 1
3841
}
42+
if right == nil {
43+
return head?.next
44+
}
3945
/* then advance both right and left now they are nth postions apart
40-
# when right gets to nil, left will be just before the item to be deleted */
41-
while right != nil {
46+
# when right gets to nil, left will be just before the item to be deleted */
47+
while right?.next != nil {
4248
right = right?.next
43-
left = left.next!
49+
left = left?.next
4450
}
4551
// delete the node
46-
left.next = left.next?.next
47-
48-
return dummyNode.next
52+
left?.next = left?.next?.next
53+
return head
4954
}
5055

5156
let ll = ListNode(1, ListNode(1, ListNode(2, ListNode(3, ListNode(6, ListNode(4, ListNode(5, nil)))))))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/valid-palindrome/?envType=study-plan-v2&envId=top-interview-150
4+
/*Time complexity:
5+
The time complexity is O(n), where n is the number of characters in the string. However, our algorithm will only run O(n/2)times, since two pointers are traversing toward each other.
6+
7+
Space complexity:
8+
The space complexity is O(1), since we use constant space to store two indexes. */
9+
10+
func isPalindrome(_ s: String) -> Bool {
11+
var lower = s.startIndex
12+
var upper = s.index(before: s.endIndex)
13+
while lower < upper {
14+
if !s[lower].isLetter, !s[lower].isNumber {
15+
lower = s.index(after: lower)
16+
continue
17+
}
18+
19+
if !s[upper].isLetter, !s[upper].isNumber {
20+
upper = s.index(before: upper)
21+
continue
22+
}
23+
24+
if s[lower].lowercased() != s[upper].lowercased() {
25+
return false
26+
}
27+
lower = s.index(after: lower)
28+
upper = s.index(before: upper)
29+
}
30+
return true
31+
}
32+
33+
let string = "A man, a plan, a canal: Panama"
34+
print(isPalindrome(string))
35+
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' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)