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
class TreeNode { | |
var value: Int | |
var left: TreeNode? | |
var right: TreeNode? | |
init(_ value: Int) { | |
self.value = 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
/*Given a pointer to the head of a singly-linked list, print each value from the reversed list. If the given list is empty, do not print anything. | |
Example | |
refers to the linked list with values | |
Print the following: | |
3 | |
2 | |
1 | |
Function Description | |
Complete the reversePrint function in the editor below. | |
reversePrint has the following parameters: |
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
/* | |
Given a binary array, find the maximum number of consecutive 1s in this array. | |
Example 1: | |
Input: [1,1,0,1,1,1] | |
Output: 3 | |
Explanation: The first two digits or the last three digits are consecutive 1s. | |
The maximum number of consecutive 1s is 3. | |
Note: |
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
/*Given an array, rotate the array to the right by k steps, where k is non-negative. | |
Follow up: | |
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. | |
Could you do it in-place with O(1) extra space? | |
Example 1: |
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
import UIKit | |
@IBDesignable | |
class DesignableUITextField: UITextField { | |
// Provides left padding for images | |
override func leftViewRect(forBounds bounds: CGRect) -> CGRect { | |
var textRect = super.leftViewRect(forBounds: bounds) | |
textRect.origin.x += leftPadding | |
return textRect |
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
//: Playground - noun: a place where people can play | |
import UIKit | |
var str = "Hello, playground" | |
/// A Mark is an value in the range 1...9 | |
/// | |
/// An assertion failure will be triggered if an attempt is made |
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 binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>) -> Int? { | |
if range.lowerBound >= range.upperBound { | |
// If we get here, then the search key is not present in the array. | |
return nil | |
} else { | |
// Calculate where to split the array. | |
let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2 | |
// Is the search key in the left half? |
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
/* | |
Returns the Greatest Common Divisor of two numbers. | |
*/ | |
func gcd(_ x: Int, _ y: Int) -> Int { | |
var a = 0 | |
var b = max(x, y) | |
var r = min(x, y) | |
while r != 0 { | |
a = b |
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 fetchCount(array: [Int], length: Int, symmetric: Bool) -> Int { | |
var count = 0 | |
for index in array.indices where index + length < array.count { | |
let subArray = array[index..<(index + length)] | |
print("Sub-array \"\(subArray)\" is \(subArray.allEqual() == true ? "Symmetrical" : "Asymmetrical")") | |
if symmetric == subArray.allEqual() { | |
count = count + 1 | |
} | |
} |
NewerOlder