This file contains hidden or 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 tree | |
| struct TreeLinkNode { | |
| TreeLinkNode *left; | |
| TreeLinkNode *right; | |
| TreeLinkNode *next; | |
| } | |
| Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. | |
This file contains hidden or 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 tree, return the inorder traversal of its nodes' values. | |
| For example: | |
| Given binary tree {1,#,2,3}, | |
| 1 | |
| \ | |
| 2 | |
| / | |
| 3 |
This file contains hidden or 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 tree, return the preorder traversal of its nodes' values. | |
| For example: | |
| Given binary tree {1,#,2,3}, | |
| 1 | |
| \ | |
| 2 | |
| / | |
| 3 |
This file contains hidden or 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 and a value, remove all instances of that value in place and return the new length. | |
| * The order of elements can be changed. It doesn't matter what you leave beyond the new length. | |
| */ | |
| public class Solution { | |
| public int removeElement(int[] A, int elem) { | |
| int len = A.length; | |
| for(int i = 0; i < len; ){ | |
| if(A[i] == elem) |
This file contains hidden or 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
| /** | |
| * You are climbing a stair case. It takes n steps to reach to the top. | |
| * Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | |
| */ | |
| public class Solution { | |
| public int climbStairs(int n) { | |
| int[] arr = new int[n + 1]; | |
| arr[0] = 1; | |
| arr[1] = 1; |
This file contains hidden or 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 sorted array, remove the duplicates in place such that each element appear only once and return the new length. | |
| * Do not allocate extra space for another array, you must do this in place with constant memory. | |
| * For example, | |
| * Given input array A = [1,1,2], | |
| * Your function should return length = 2, and A is now [1,2]. | |
| */ | |
| public class Solution { | |
| public int removeDuplicates(int[] A) { |
This file contains hidden or 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
| /** | |
| * Find the contiguous subarray within an array (containing at least one number) which has the largest sum. | |
| * For example, given the array [−2,1,−3,4,−1,2,1,−5,4], | |
| * the contiguous subarray [4,−1,2,1] has the largest sum = 6. | |
| */ | |
| public class Solution { | |
| public int maxSubArray(int[] A) { | |
| int res = A[0], sum = 0; | |
| for(int i = 0; i < A.length; ++i){ | |
| sum = Math.max(sum + A[i], A[i]); |
This file contains hidden or 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 roman numeral, convert it to an integer. | |
| * Input is guaranteed to be within the range from 1 to 3999. | |
| */ | |
| public class Solution { | |
| public int romanToInt(String s) { | |
| HashMap<Character, Integer> dic = new HashMap<Character, Integer>(); | |
| dic.put('I', 1); | |
| dic.put('V', 5); |
This file contains hidden or 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 tree, check whether it is a mirror of itself (ie, symmetric around its center). | |
| * For example, this binary tree is symmetric: | |
| * 1 | |
| / \ | |
| 2 2 | |
| / \ / \ | |
| 3 4 4 3 | |
| * But the following is not: | |
| 1 |
This file contains hidden or 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
| /** | |
| * Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. | |
| */ | |
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; | |
| * ListNode next; | |
| * ListNode(int x) { | |
| * val = x; |