Skip to content

Instantly share code, notes, and snippets.

View spurscho's full-sized avatar
😃

spurs spurscho

😃
  • Seoul
View GitHub Profile
@spurscho
spurscho / 24. Swap Nodes in Pairs.swift
Created January 19, 2020 12:40
24. Swap Nodes in Pairs
class Solution {
func swapPairs(_ head: ListNode?) -> ListNode? {
guard let newHead = head?.next else {
return head
}
// 1 -> 2 -> 3 -> 4
// 2 -> 1 -> 4 -> 3
let tmp = head?.next?.next
head?.next?.next = head
head?.next = swapPairs(tmp)
@spurscho
spurscho / 23. Merge k Sorted Lists.swift
Created January 19, 2020 12:40
23. Merge k Sorted Lists
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
@spurscho
spurscho / 22. Generate Parentheses.swift
Created January 19, 2020 12:40
22. Generate Parentheses
class Solution {
func generateParenthesis(_ n: Int) -> [String] {
var result = [String]()
if(n == 0) {
return result
}
var str = ""
generateParenthesisUtil(&result,str,n,0,0)
return result
}
@spurscho
spurscho / 15. 3Sum.swift
Created January 8, 2020 05:09
15. 3Sum ( not understand )
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
var nums = nums; nums.sort()
var result = [[Int]]()
guard nums.count >= 3 else {return result}
//i,l,r indices
for i in 0..<nums.count-2{
if(i > 0 && nums[i] == nums[i-1]) {continue}
var l=i+1, r=nums.count-1
var targetValue = nums[i]
@spurscho
spurscho / 21. Merge Two Sorted Lists.swift
Created January 8, 2020 02:53
21. Merge Two Sorted Lists
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
@spurscho
spurscho / 19. Remove Nth Node From End of List.swift
Created January 7, 2020 10:21
19. Remove Nth Node From End of List
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
@spurscho
spurscho / Sample Linked List.swift
Created January 7, 2020 08:55
Sample Linked List.swift
import UIKit
class Node {
let value: Int
var next: Node?
init(_ value:Int, _ next: Node?) {
self.value = value
self.next = next
}
@spurscho
spurscho / 17. Letter Combinations of a Phone Number.swift
Created January 6, 2020 05:41
17. Letter Combinations of a Phone Number
class Solution {
let phoneDict: [Int : String] = [
2 : "abc",
3 : "def",
4 : "ghi",
5 : "jkl",
6 : "mno",
7 : "pqrs",
8 : "tuv",
9 : "wxyz"
@spurscho
spurscho / 20. Valid Parentheses.swift
Created January 6, 2020 02:59
20. Valid Parentheses
class Solution {
func isValid(_ s: String) -> Bool {
var stack = [Character]()
let closingPairs: [Character:Character] = [ "{":"}", "[":"]", "(":")" ]
let openBrackets = Set<Character>(closingPairs.keys)
for char in s {
if openBrackets.contains(char) {
stack.append(char)
} else {
guard let last = stack.popLast() else {
@spurscho
spurscho / addTwoNumbers.swift
Last active January 7, 2020 11:06
addTwoNumbers
var p1: ListNode? = l1
var p2: ListNode? = l2
var resultNode: ListNode? = ListNode(0)
let head = resultNode
var carry = 0
while p1 != nil || p2 != nil || carry > 0 {
let val1 = p1?.val ?? 0
let val2 = p2?.val ?? 0
let sum = val1 + val2 + carry