Skip to content

Instantly share code, notes, and snippets.

View voxqhuy's full-sized avatar
:shipit:
Don’t comment bad code—rewrite it

Huy Vo voxqhuy

:shipit:
Don’t comment bad code—rewrite it
  • San Diego, CA
View GitHub Profile
@voxqhuy
voxqhuy / Main.java
Created March 3, 2019 18:45
UniSexWC created by voxqhuy - https://repl.it/@voxqhuy/UniSexWC
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
@voxqhuy
voxqhuy / leetCode.swift
Last active June 9, 2020 03:22
LeetCode Playground
// Problem 1290: Convert Binary Number in a Linked List to Integer
// Link: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer
// Solution 1: using an array to store the values, multiply each value by its bit later
func getDecimalValue(_ head: ListNode?) -> Int {
if head == nil { return 0 }
var headCopy = head
// an array stores all the values
var vals = [Int]()
var bit = 1
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
// Problem 69. Sqrt(x)
//Link: https://leetcode.com/problems/sqrtx/submissions/
// Solution: Binary seearch
// Time: O(logn), Space: O(1)
class Solution {
public int mySqrt(int x) {
if (x < 2) { return x; }
long left = 0;
long right = x / 2 + 1;
// Problem 118. Pascal's Triangle
// Link: https://leetcode.com/problems/pascals-triangle/
// Solution: An element = one above + one before above
// Time: O(n^2), Space: O(n^2)
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numRows == 0) { return result; }
result.add(new ArrayList<>(Arrays.asList(1)));
// Problem 938. Range Sum of BST
// Link: https://leetcode.com/problems/range-sum-of-bst/
// Solution: Depth first search recursive
// Time: O(n), Space: O(n) TODO why?
var sum = 0
func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
dfs(root, L, R)
return sum
}
// 700. Search in a Binary Search Tree
// link: https://leetcode.com/problems/search-in-a-binary-search-tree/
// Solution: recursive
// Time O(n), Space O(n)
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) { return null; }
if (root.val == val) { return root; }
TreeNode node = root.val < val
? searchBST(root.right, val)
// Time O(n), Space O(1)
func hasConsecutiveOdds(_ nums: [Int]) -> Bool {
var oddCount = 0
var index = 0
while index != nums.count
{
if nums[index] % 2 == 1 {
oddCount += 1
if oddCount == 3 { return true }
// 5. Longest Palindromic Substring
// Link: https://leetcode.com/problems/longest-palindromic-substring/
// Time O(n^2), Space O(1)
var maxLength = 0
var firstIndex = 0
func longestPalindrome(_ s: String) -> String {
if s.count < 2 { return s }
let sArray = Array(s)
for i in 0..<sArray.count-1 {
// 238. Product of Array Except Self
// Link: https://leetcode.com/problems/product-of-array-except-self/
func productExceptSelf(_ nums: [Int]) -> [Int] {
let count = nums.count
var result = Array(repeating: 1, count: count)
for i in 1..<count {
result[i] = result[i - 1] * nums[i - 1]
}