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 Main { | |
public static void main(String[] args) { | |
System.out.println("Hello world!"); | |
} | |
} |
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
// 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 |
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 { | |
* int val; | |
* ListNode next; | |
* ListNode() {} | |
* ListNode(int val) { this.val = val; } | |
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | |
* } | |
*/ |
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
// 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; |
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
// 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))); |
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
// 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 | |
} |
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
// 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) |
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
// 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 } |
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
// 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 { |
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
// 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] | |
} |
OlderNewer