Skip to content

Instantly share code, notes, and snippets.

View spurscho's full-sized avatar
😃

spurs spurscho

😃
  • Seoul
View GitHub Profile
@spurscho
spurscho / SimpleTreeNode.swift
Created February 15, 2020 06:29
SimpleTraverserTreeNode.swift
class TreeNode {
var data: Int
var left: TreeNode?
var right: TreeNode?
var parent: TreeNode?
init(_ data: Int) {
self.data = data
@spurscho
spurscho / BST_Implement.swift
Last active February 5, 2020 07:12
BST_Implement.swift
public class BinaryNode<Element> {
public var value: Element // 노드의 값
public var leftChild: BinaryNode? // 왼쪽 노드 (옵셔널)
public var rightChild: BinaryNode? // 오른쪽 노드 (옵셔널)
public init(value: Element) { self.value = value } // 초기화 함수
public func traverseInOrder(visit: (Element) -> Void) {
leftChild?.traverseInOrder(visit: visit)
visit(value)
rightChild?.traverseInOrder(visit: visit)
@spurscho
spurscho / 39. Combination Sum.swift
Created February 4, 2020 02:19
39. Combination Sum
class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
var res = [[Int]]()
var temp = [Int]()
helper(&res, &temp, candidates, 0, target)
return res
}
@spurscho
spurscho / 36. Valid Sudoku.swift
Created February 3, 2020 14:34
36. Valid Sudoku
class Solution {
func isValidSudoku(_ board: [[Character]]) -> Bool {
var rowSet = [Set<Character>](repeating: Set<Character>(), count: 9)
var colSet = [Set<Character>](repeating: Set<Character>(), count: 9)
var squareSet = [Set<Character>](repeating: Set<Character>(), count: 9)
for (rowIndex, row) in board.enumerated() {
for (colIndex, character) in row.enumerated() {
if character == Character(".") {
continue
@spurscho
spurscho / 38. Count and Say.swift
Last active February 3, 2020 14:35
38. Count and Say
class Solution {
func countAndSay(_ n: Int) -> String {
if n == 1 { return "1" }
let val = countAndSay( n - 1 )
let res = "\(count(String(val)))"
return res
}
func count(_ str: String) -> String {
guard str.count > 0 else {
@spurscho
spurscho / Stack_by_array.swift
Last active January 22, 2020 01:58
Stack_by_array
public struct Stack<T> {
private var elements = Array<T>()
public init() {}
public mutating func pop() -> T? {
return self.elements.popLast()
}
public mutating func push(element: T) {
self.elements.append(element)
@spurscho
spurscho / Queue_by_array.swift
Last active January 22, 2020 01:11
Queue_by_array
public struct Queue<T> {
internal var data = Array<T>()
public init() {}
public mutating func dequeue() -> T? {
return data.removeFirst()
}
public func peek() -> T? {
return data.first
@spurscho
spurscho / 28. Implement strStr().swift
Created January 19, 2020 12:41
28. Implement strStr()
class Solution { // my code got timelimted error. this is new codes
func strStr(_ haystack: String, _ needle: String) -> Int {
guard !needle.isEmpty else { return 0 }
guard haystack.count >= needle.count else { return -1 }
let distance = haystack.count - needle.count
for i in 0...distance {
let start = haystack.index(haystack.startIndex, offsetBy: i)
let end = haystack.index(haystack.startIndex, offsetBy: i+needle.count)
@spurscho
spurscho / 27. Remove Element.swift
Created January 19, 2020 12:41
27. Remove Element
class Solution {
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
guard nums.count > 0 else {
return 0
}
var idx: Int = 0
while true {
if idx == nums.count {
break
@spurscho
spurscho / 26. Remove Duplicates from Sorted Array.swift
Created January 19, 2020 12:41
26. Remove Duplicates from Sorted Array
class Solution {
func removeDuplicates(_ nums: inout [Int]) -> Int {
guard nums.count > 0 else {
return 0
}
nums.sort()
var idx: Int = 0
while true {
if idx + 1 == nums.count {