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
/** | |
* 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 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
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
/** | |
* 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
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
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
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
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 |