Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Practice-Problems/CSPracticeProblems.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// LeetCode Practice Problems: Algorithms

//---------------------------------------------------------------------------------------------

// MARK: - Binary Search Problems
// Key Words: sorted, range
// Notes:
// - Binary search should be muscle memory
// - Can be implemented iteratively or recursively

//---------------------------------------------------------------------------------------------

// Sqrt(x)
// Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
// Since the return type is an integer, the decimal digits are truncated and only the integer
// part of the result is returned.

func squareRoot(_ x: Int) -> Int {
var min = 0
var max = x
var res = 0

while min <= max {
let mid = Int((min + max) / 2)
let squared = mid * mid
if squared == x {
return mid
} else if squared > x {
max = mid - 1
} else {
min = mid + 1
res = mid
}
}

return res
}

print(squareRoot(16)) // 4
print(squareRoot(9)) // 3
print(squareRoot(8)) // 2
print(squareRoot(1)) // 1
print(squareRoot(0)) // 0

//---------------------------------------------------------------------------------------------

// MARK: - Hash Table Problems
// Key Words: two arrays, intersection, count

//---------------------------------------------------------------------------------------------

// Intersection of Two Arrays II
// Given two arrays, write a function to compute their intersection.

func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
var res: [Int] = []
var dict = [Int: Int]()

for n1 in nums1 {
dict[n1, default: 0] += 1
}

for n2 in nums2 {
if dict[n2, default: 0] > 0 {
res.append(n2)
dict[n2]! -= 1
}
}

return res
}

//---------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.