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
| // vim: syntax=swift | |
| class Node{ | |
| var key : Int | |
| var val : Int | |
| var pre: Node? | |
| var next : Node? | |
| init(_ key : Int , _ val : Int){ | |
| self.key = key | |
| self.val = val | |
| } |
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
| // vim: syntax=swift | |
| func numIslands(_ grid: [[Character]]) -> Int { | |
| guard grid.count > 0 else { return 0 } | |
| guard grid[0].count > 0 else { return 0 } | |
| var grid = grid | |
| var islandCounter = 0 | |
| for i in 0..<grid.count{ | |
| for j in 0..<grid[i].count{ | |
| if grid[i][j] == "1"{ | |
| islandCounter += 1 |
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
| // vim: syntax=swift | |
| public class TreeNode { | |
| public var val: Int | |
| public var left: TreeNode? | |
| public var right: TreeNode? | |
| public init() { self.val = 0; self.left = nil; self.right = nil; } | |
| public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } | |
| public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { | |
| self.val = val | |
| self.left = left |
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
| // vim: syntax=swift | |
| func binarySearch<T : Equatable & Comparable>(_ arr:[T] , _ val : T)->Bool{ | |
| var l = 0 | |
| var h = arr.count - 1 | |
| while(l < h){ | |
| let m = l + (h-l)/2 | |
| if arr[m] == val{ | |
| return true | |
| } | |
| else if arr[m] < val{ |
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
| // vim: syntax=swift | |
| public class TreeNode { | |
| public var val: Int | |
| public var left: TreeNode? | |
| public var right: TreeNode? | |
| public init() { self.val = 0; self.left = nil; self.right = nil; } | |
| public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } | |
| public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { | |
| self.val = val | |
| self.left = left |
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
| // vim: syntax=swift | |
| func solveSudoku(_ board: inout [[Character]]) { | |
| cansolve(&board) | |
| } | |
| func cansolve(_ board : inout [[Character]])->Bool{ | |
| for i in 0..<9{ | |
| for j in 0..<9{ | |
| if board[i][j] == "."{ | |
| for char in "123456789"{ | |
| if validPlacement(&board, i, j, char){ |
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
| // vim: syntax=swift | |
| func solveNQueens(_ n: Int) -> [[String]] { | |
| var arr : [Int] = [] | |
| var res : [[Int]] = [] | |
| doer(n, 0, &arr, &res) | |
| var ans : [[String]] = [] | |
| for i in 0..<res.count{ | |
| ans.append([]) | |
| for j in 0..<res[i].count{ | |
| var st = "" |
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
| // vim: syntax=swift | |
| func anagram<C: Collection>(items: C) -> [[C.Iterator.Element]] { | |
| var scratch = Array(items) | |
| var result: [[C.Iterator.Element]] = [] | |
| func heap(_ n: Int) { | |
| if n == 1 { | |
| result.append(scratch) | |
| return | |
| } |
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
| // vim: syntax=swift | |
| func gcd(_ a : Int, _ b : Int)->Int{ | |
| if b == 0{ | |
| return a | |
| } | |
| return gcd(b, a%b) | |
| } | |
| func lcm(_ a : Int , b : Int)->Int{ | |
| return (a*b)/gcd(a, b) | |
| } |
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
| // vim: syntax=swift | |
| func letterIndex(_ letter: Character) -> Int { | |
| return Int(letter.unicodeScalars.first!.value) - Int(Unicode.Scalar("a").value) | |
| } |
OlderNewer