This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { | |
var res = [[Int]]() | |
var temp = [Int]() | |
helper(&res, &temp, candidates, 0, target) | |
return res | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode { | |
var data: Int | |
var left: TreeNode? | |
var right: TreeNode? | |
var parent: TreeNode? | |
init(_ data: Int) { | |
self.data = data |
OlderNewer