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
/* | |
Symmetric Tree | |
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 | |
/ \ / \ |
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
import java.util.Stack; | |
/* | |
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. | |
* | |
* push(x) -- Push element x onto stack. | |
* pop() -- Removes the element on top of the stack. | |
* top() -- Get the top element. | |
* getMin() -- Retrieve the minimum element in the stack. | |
*/ |
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
import java.util.LinkedList; | |
import java.util.Stack; | |
/* | |
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. | |
* | |
* push(x) -- Push element x onto stack. | |
* pop() -- Removes the element on top of the stack. | |
* top() -- Get the top element. | |
* getMin() -- Retrieve the minimum element in the stack. |
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 list, rotate the list to the right by k places, where k is non-negative. | |
* | |
* For example: | |
* Given 1->2->3->4->5->NULL and k = 2, | |
* return 4->5->1->2->3->NULL. | |
*/ | |
public class Solution { | |
public ListNode rotateRight(ListNode head, int n) { | |
// make sure list length > 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
public class Solution1 { | |
public int reverse(int x) { | |
if(x < 10 && x > -10){ | |
return x; | |
} | |
boolean negative = x < 0; | |
char[] c = null; | |
if(negative) { | |
c = (x+"").substring(1).toCharArray(); |
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(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
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 words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) | |
You have the following 3 operations permitted on a word: | |
a) Insert a character | |
b) Delete a character | |
c) Replace a character | |
*/ |
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, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). | |
For example: | |
Given binary tree {3,9,20,#,#,15,7}, | |
3 | |
/ \ | |
9 20 | |
/ \ | |
15 7 |
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 strings, return their sum (also a binary string). | |
For example, | |
a = "11" | |
b = "1" | |
Return "100". | |
*/ | |
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 an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. | |
You may assume that the array is non-empty and the majority element always exist in the array. | |
*/ | |
public class Solution { | |
public int majorityElement(int[] num) { | |
int candidate = num[0]; | |
int counter = 0; |
OlderNewer