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 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 { |
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 { | |
let phoneDict: [Int : String] = [ | |
2 : "abc", | |
3 : "def", | |
4 : "ghi", | |
5 : "jkl", | |
6 : "mno", | |
7 : "pqrs", | |
8 : "tuv", | |
9 : "wxyz" |
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
import UIKit | |
class Node { | |
let value: Int | |
var next: Node? | |
init(_ value:Int, _ next: Node?) { | |
self.value = value | |
self.next = next | |
} |
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
/** | |
* 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 | |
* } | |
* } |
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
/** | |
* 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 | |
* } | |
* } |
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 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] |
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 generateParenthesis(_ n: Int) -> [String] { | |
var result = [String]() | |
if(n == 0) { | |
return result | |
} | |
var str = "" | |
generateParenthesisUtil(&result,str,n,0,0) | |
return result | |
} |
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
/** | |
* 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 | |
* } | |
* } |
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 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) |
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 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 { |