This is No.215 in Leetcode. The description: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. My own solution is: class Solution { public int findKthLargest(int[] nums, int k) { // sort array first Arrays.sort(nums); // get the …
Convert Sorted List to Binary Search Tree
This is No.109 in leetcode. Below is the solution: /** * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int * x) { val = x; } } */ /** * Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode * right; TreeNode(int x) { …
Continue reading “Convert Sorted List to Binary Search Tree”
Reverse Linked List II
This is No.92 in leetcode. The problem description: Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. I have a wrong idea on this problem, so I post the reference solution below: class Solution { // Object level variables since we …
Remove Duplicates from Sorted List II
This is No.82. in leetcode. My code is given below: /** * 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; } * } */ class …
Rotate List
This is No.61 in leetcode. The description: Given a linked list, rotate the list to the right by k places, where k is non-negative. This is my original solution: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = …
Jump Game
This is No.55 in leetcode. The problem description is: "Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index." Here are 4 approaches to the …
Combination Sum
This is a problem in leetcode. To solve this problem, I decide to use recursion on it: class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> rec = new ArrayList<Integer>(); search( candidates, target, res, rec, 0); return res; } private void search(int[] arr, int tar, List<List<Integer>>res, List<Integer>rec, int …
Rotate Array
This is No.189 problem in LeetCode. The problem description is: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to …
Convert Sorted Array to Binary Search Tree
This is NO.108 problem in leetcode. The description of this problem is: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ …
Continue reading “Convert Sorted Array to Binary Search Tree”
Symmetric Tree – recursion and iteration
This is No.101 problem in leetcode. The question is: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 1.Recursion: Recursion occurs when a thing is defined in terms of itself or of its type. In this problem, I will solve it in recursion first. The thought is …