Skip to content

Instantly share code, notes, and snippets.

View piyushdec's full-sized avatar

Piyush piyushdec

  • United States
View GitHub Profile
class PendingOperations {
lazy var downloadsInProgress: [IndexPath: Operation] = [:]
lazy var downloadQueue: OperationQueue = {
var queue = OperationQueue()
queue.name = "Download queue"
queue.maxConcurrentOperationCount = 1
return queue
}()
}
import UIKit
enum ImageRecordState {
case new, downloaded, failed
}
class ImageRecord {
let name: String
let url: URL
var state = ImageRecordState.new
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:obj.img]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imgView.image = [UIImage imageWithData:imageData];
});
return cell;
}
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url]; UIImage *image = [[UIImage alloc] initWithData:imageData];
func sortedSquares(arr: [Int]) -> [Int] {
var result = Array(repeating: 0, count: arr.count)
var low = 0
var high = arr.count-1
for p in 0..<arr.count {
if arr[low] * arr[low] > arr[high] * arr[high] {
result[p] = arr[low] * arr[low]
func sortArrayByParityII(_ A: [Int]) -> [Int] {
var even: [Int] = []
var odd: [Int] = []
var result: [Int] = []
for element in A {
if(element % 2 == 0) {
even.append(element)
} else {
odd.append(element)
}
class Solution {
func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
guard let node = root else { return nil }
if node.val == val { return root }
if node.val > val { return searchBST(node.left, val) }
if node.val < val { return searchBST(node.right, val) }
return nil
func dividingNumber(_ left: Int, _ right: Int) -> [Int] {
var nums = [Int]()
for i in left...right {
if divisible(i) {
nums.append(i)
}
}
func maximumSwap(number: Int) {
if number == 0 {
return
}
var digits = digitsFromNum(num: number)
var max_index = 0
var left = 0
var right = 0
Check if A[i] == A[i - 1] or A[i] == A[i - 2]
If so, we return A[i]
If not, it must be [x, x, y, z] or [x, y, z, x].
We return A[0] for the cases that we miss.
O(N) time O(1) space
class Solution {
func repeatedNTimesIII(_ A: [Int]) -> Int {
for i in 2..<A.count {