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 of integers, every element appears twice except for one. Find that single one. | |
Note: | |
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? | |
*/ | |
public class Solution{ | |
public int singleNumber(int[] A){ | |
int num = A[0]; | |
for(int i = 1; i < A.length; i++){ |
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 tree, find its maximum depth. | |
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. | |
*/ | |
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; |
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 two binary trees, write a function to check if they are equal or not. | |
* Two binary trees are considered equal if they are structurally identical and the nodes have the same value. | |
*/ | |
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; |
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
/** | |
* Reverse digits of an integer. | |
Example1: x = 123, return 321 | |
Example2: x = -123, return -321 | |
Have you thought about this? | |
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! | |
1. If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. | |
2. Did you notice that the reversed integer might overflow? |
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
/** | |
* Say you have an array for which the ith element is the price of a given stock on day i. | |
* | |
* If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. | |
*/ | |
public class Solution{ | |
public int maxProfit(int[] prices) { | |
if(prices.length == 0 || prices.length == 1) return 0; | |
int max_profit = 0; |
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 n, how many structurally unique BST's (binary search trees) that store values 1...n? | |
* For example, | |
* Given n = 3, there are a total of 5 unique BST's. | |
1 3 3 2 1 | |
\ / / / \ \ | |
3 2 1 1 3 2 | |
/ / \ \ | |
2 1 2 3 |
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 linked list, determine if it has a cycle in it. | |
*/ | |
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { |
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 sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. | |
* You may assume no duplicates in the array. | |
* Here are few examples. | |
* [1,3,5,6], 5 → 2 | |
* [1,3,5,6], 2 → 1 | |
* [1,3,5,6], 7 → 4 | |
* [1,3,5,6], 0 → 0 | |
*/ | |
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 of integers, find two numbers such that they add up to a specific target number. | |
* The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. | |
* Please note that your returned answers (both index1 and index2) are not zero-based. | |
* You may assume that each input would have exactly one solution. | |
* Input: numbers={2, 7, 11, 15}, target=9 | |
* Output: index1=1, index2=2 | |
* | |
*/ | |
public class Solution{ |
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 sorted linked list, delete all duplicates such that each element appear only once. | |
* For example, | |
* Given 1->1->2, return 1->2. | |
* Given 1->1->2->3->3, return 1->2->3. | |
*/ | |
/** | |
* Definition for singly-linked list. | |
* public class ListNode { |
OlderNewer