This file contains 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
extension UIImage { | |
class func circle(diameter: CGFloat, color: UIColor, _ stroke: CGFloat, _ strokeColor: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, stroke) | |
let ctx = UIGraphicsGetCurrentContext()! | |
ctx.saveGState() | |
let rect = CGRect(x: stroke/2, y: stroke/2, width: diameter - stroke, height: diameter - stroke) | |
ctx.setFillColor(color.cgColor) | |
ctx.setLineWidth(stroke) | |
ctx.setStrokeColor(strokeColor.cgColor) |
This file contains 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
func select<T>(from a: [T], count k: Int) -> [T] { | |
var a = a | |
for i in 0..<k { | |
let r = random(min: i, max: a.count - 1) | |
if i != r { | |
swap(&a[i], &a[r]) | |
} | |
} | |
return Array(a[0..<k]) | |
} |
This file contains 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 a binary tree node. | |
* public class TreeNode { | |
* public var val: Int | |
* public var left: TreeNode? | |
* public var right: TreeNode? | |
* public init(_ val: Int) { | |
* self.val = val | |
* self.left = nil | |
* self.right = nil |
This file contains 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
let chars = (97...122).map{Character(UnicodeScalar($0))} | |
func convertToTitle(_ n: Int) -> String { | |
if n == 0{ return "" } | |
return convertToTitle((n-1)/26) + "\(chars[((n-1) % 26)])".uppercased() | |
} |
This file contains 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
func groupAnagrams(_ strs: [String]) -> [[String]] { | |
var dic = [String: [String]]() | |
for (i, value) in strs.enumerated(){ | |
let key = String(value.characters.sorted {$0<$1}) | |
if dic[key] == nil { | |
dic[key] = [value] | |
}else{ | |
dic[key]?.append(value) | |
} | |
} |
This file contains 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
// Each element in the result must be unique. | |
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { | |
var set = Set<Int>() | |
var intersect = Set<Int>() | |
for i in nums1{ | |
set.insert(i) | |
} | |
for i in nums2{ | |
if set.contains(i){ | |
intersect.insert(i) |
This file contains 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
func productExceptSelf(_ nums: [Int]) -> [Int] { | |
var result = nums | |
var temp = 1 | |
for i in 0..<nums.count{ | |
result[i] = temp | |
temp *= nums[i] | |
} | |
temp = 1 | |
for i in 0..<nums.count{ |
This file contains 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
func reverse(_ x: Int) -> Int { | |
var r = 0 | |
var y = x | |
while y != 0 { | |
r = r*10 + y%10 | |
y = y/10 | |
} | |
if r < Int32.min || r > Int32.max { | |
return 0 | |
} |
This file contains 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 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
func topKFrequentElements(_ a: [Int], _ k: Int) -> [Int]{ | |
var frequencyTable = [Int: Int]() | |
var buckets = [[Int]?](repeating: nil, count: a.count) | |
var result = [Int]() | |
for number in a{ | |
if frequencyTable[number] != nil{ | |
frequencyTable[number]! += 1 | |
}else{ | |
frequencyTable[number] = 1 |