Skip to content

Commit

Permalink
Merge branch 'kosoraYintai-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Feb 4, 2018
2 parents b838e2f + f31071e commit ec2c5f7
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ LeetCode
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| [C++](./algorithms/cpp/lowestCommonAncestorOfABinarySearchTree/LowestCommonAncestorOfABinarySearchTree.cpp)|Easy|
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [C++](./algorithms/cpp/palindromeLinkedList/PalindromeLinkedList.cpp)|Easy|
|233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| [C++](./algorithms/cpp/numberOfDigitOne/NumberOfDigitOne.cpp)|Medium|
|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| [C++](./algorithms/cpp/implementQueueUsingStacks/ImplementQueueUsingStacks.cpp)|Easy|
|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| [C++](./algorithms/cpp/implementQueueUsingStacks/ImplementQueueUsingStacks.cpp), [Java](./algorithms/java/src/myQueue/MyQueue.java)|Easy|
|231|[Power of Two](https://leetcode.com/problems/power-of-two/)| [C++](./algorithms/cpp/powerOfTwo/PowerOfTwo.cpp)|Easy|
|230|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [C++](./algorithms/cpp/kthSmallestElementInaBST/KthSmallestElementInABst.cpp)|Medium|
|229|[Majority Element II](https://oj.leetcode.com/problems/majority-element-ii/) | [C++](./algorithms/cpp/majorityElement/majorityElement.II.cpp)|Medium|
|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [C++](./algorithms/cpp/summaryRanges/SummaryRanges.cpp)|Easy|
|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| [C++](./algorithms/cpp/basicCalculator/BasicCalculator.II.cpp)|Medium|
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [C++](./algorithms/cpp/invertBinaryTree/InvertBinaryTree.cpp)|Easy|
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| [C++](./algorithms/cpp/implementStackUsingQueues/ImplementStackUsingQueues.cpp)|Medium|
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| [C++](./algorithms/cpp/implementStackUsingQueues/ImplementStackUsingQueues.cpp), [Java](./algorithms/java/src/myStack/MyStack.java)|Medium|
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)| [C++](./algorithms/cpp/basicCalculator/BasicCalculator.cpp)|Medium|
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [C++](./algorithms/cpp/rectangleArea/RectangleArea.cpp)|Easy|
|222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)| [C++](./algorithms/cpp/countCompleteTreeNodes/CountCompleteTreeNodes.cpp)|Medium|
Expand Down Expand Up @@ -149,7 +149,7 @@ LeetCode
|198|[House Robber](https://leetcode.com/problems/house-robber/)| [C++](./algorithms/cpp/houseRobber/houseRobber.cpp)|Easy|
|191|[Number of 1 Bits](https://oj.leetcode.com/problems/number-of-1-bits/)| [C++](./algorithms/cpp/numberOf1Bits/numberOf1Bits.cpp)|Easy|
|190|[Reverse Bits](https://oj.leetcode.com/problems/reverse-bits/)| [C++](./algorithms/cpp/reverseBits/reverseBits.cpp)|Easy|
|189|[Rotate Array](https://oj.leetcode.com/problems/rotate-array/)| [C++](./algorithms/cpp/rotateArray/rotateArray.cpp)|Easy|
|189|[Rotate Array](https://oj.leetcode.com/problems/rotate-array/)| [C++](./algorithms/cpp/rotateArray/rotateArray.cpp), [Java](./algorithms/java/src/rotateArray/RotateArray.java)|Easy|
|188|[Best Time to Buy and Sell Stock IV](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [C++](./algorithms/cpp/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.IV.cpp)|Hard|
|187|[Repeated DNA Sequences](https://oj.leetcode.com/problems/repeated-dna-sequences/)| [C++](./algorithms/cpp/repeatedDNASequences/repeatedDNASequences.cpp)|Medium|
|186|[Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | [C++](./algorithms/cpp/reverseWordsInAString/reverseWordsInAString.II.cpp)|Medium|
Expand All @@ -173,7 +173,7 @@ LeetCode
|158|[Read N Characters Given Read4 II - Call multiple times](https://oj.leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥ | [C++](./algorithms/cpp/readNCharactersGivenRead4/readNCharactersGivenRead4.II.cpp)|Hard|
|157|[Read N Characters Given Read4](https://oj.leetcode.com/problems/read-n-characters-given-read4/) ♥ | [C++](./algorithms/cpp/readNCharactersGivenRead4/readNCharactersGivenRead4.cpp)|Easy|
|156|[Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) ♥ | [C++](./algorithms/cpp/binaryTreeUpsideDown/binaryTreeUpsideDown.cpp)|Medium|
|155|[Min Stack](https://oj.leetcode.com/problems/min-stack/)| [C++](./algorithms/cpp/minStack/minStack.cpp)|Easy|
|155|[Min Stack](https://oj.leetcode.com/problems/min-stack/)| [C++](./algorithms/cpp/minStack/minStack.cpp), [Java](./algorithms/java/src/minStack/MinStack.java)|Easy|
|154|[Find Minimum in Rotated Sorted Array II](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| [C++](./algorithms/cpp/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.II.cpp)|Hard|
|153|[Find Minimum in Rotated Sorted Array](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [C++](./algorithms/cpp/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.cpp), [Java](./algorithms/java/src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.java)|Medium|
|152|[Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/)| [C++](./algorithms/cpp/maximumProductSubarray/maximumProductSubarray.cpp)|Medium|
Expand Down Expand Up @@ -302,7 +302,7 @@ LeetCode
|29|[Divide Two Integers](https://oj.leetcode.com/problems/divide-two-integers/)| [C++](./algorithms/cpp/divideTwoInt/divideTwoInt.cpp)|Medium|
|28|[Implement strStr()](https://oj.leetcode.com/problems/implement-strstr/)| [C++](./algorithms/cpp/strStr/strStr.cpp), [Java](./algorithms/java/src/strStr/strStr.java)|Easy|
|27|[Remove Element](https://oj.leetcode.com/problems/remove-element/)| [C++](./algorithms/cpp/removeElement/removeElement.cpp)|Easy|
|26|[Remove Duplicates from Sorted Array](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./algorithms/cpp/removeDuplicatesFromSortedArray/removeDuplicatesFromSortedArray.cpp)|Easy|
|26|[Remove Duplicates from Sorted Array](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./algorithms/cpp/removeDuplicatesFromSortedArray/removeDuplicatesFromSortedArray.cpp), [Java](./algorithms/java/src/removeDuplicatesFromSortedArray/RemoveDuplicatesFromSortedArray.java)|Easy|
|25|[Reverse Nodes in k-Group](https://oj.leetcode.com/problems/reverse-nodes-in-k-group/)| [C++](./algorithms/cpp/reverseNodesInKGroup/reverseNodesInKGroup.cpp)|Hard|
|24|[Swap Nodes in Pairs](https://oj.leetcode.com/problems/swap-nodes-in-pairs/)| [C++](./algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp)|Medium|
|23|[Merge k Sorted Lists](https://oj.leetcode.com/problems/merge-k-sorted-lists/)| [C++](./algorithms/cpp/mergeKSortedLists/mergeKSortedLists.cpp)|Hard|
Expand Down
60 changes: 60 additions & 0 deletions algorithms/java/src/minStack/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Source : https://leetcode.com/problems/min-stack/description/
// Author : Tianming Cao
// Date : 2018-02-02

/**********************************************************************************
* 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.
* Example:
* MinStack minStack = new MinStack();
* minStack.push(-2);
* minStack.push(0);
* minStack.push(-3);
* minStack.getMin(); --> Returns -3.
* minStack.pop();
* minStack.top(); --> Returns 0.
* minStack.getMin(); --> Returns -2.
*
**********************************************************************************/
package minStack;

import java.util.Stack;

public class MinStack {
public Stack<Integer> mainStack;
/**
* Call an extra stack named assistStack to store the min value.
* While we doing push operation, compare x with the top of assistStack and push the smaller value into assistStack.
* The other operations pop,top and getMin is very simple.
**/
public Stack<Integer> assistStack;
public MinStack() {
mainStack = new Stack<Integer>();
assistStack = new Stack<Integer>();
}
public void push(int x) {
mainStack.push(x);
if (assistStack.isEmpty()) {
assistStack.push(x);
} else {
assistStack.push(Math.min(x, getMin()));
}
}

public void pop() {
mainStack.pop();
assistStack.pop();
}

public int top() {
return mainStack.peek();
}

public int getMin() {
return assistStack.peek();
}
}
22 changes: 22 additions & 0 deletions algorithms/java/src/minStack/TestMinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package minStack;

import org.junit.Assert;
import org.junit.Test;

/**
* Test for 155. Min Stack
*/
public class TestMinStack {
@Test
public void test() {
MinStack minStack = new MinStack();
minStack.push(3);
minStack.push(4);
minStack.push(1);
minStack.push(2);
Assert.assertTrue(minStack.getMin() == 1);
minStack.pop();
minStack.pop();
Assert.assertTrue(minStack.getMin() == 3);
}
}
102 changes: 102 additions & 0 deletions algorithms/java/src/myQueue/MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Source : https://leetcode.com/problems/implement-queue-using-stacks/description/
// Author : Tianming Cao
// Date : 2018-02-02

/**********************************************************************************
* Implement the following operations of a queue using stacks.
*
* push(x) -- Push element x to the back of queue.
* pop() -- Removes the element from in front of queue.
* peek() -- Get the front element.
* empty() -- Return whether the queue is empty.
*
* Notes:
* You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
* Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
* You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
*
**********************************************************************************/
package myQueue;

import java.util.Stack;

/**
* This problem is a sibling of problem 225(https://leetcode.com/problems/implement-stack-using-queues/description/)
* The solution is:
* 1. stack1 is always in charge of push operation
* 2. stack2 is always in charge of peek and pop operation
* 3. if we want to do peek or pop operation, but stack2 is empty,
* we should first pop all the elements from stack1 and push them into stack2 in turn.
* Give a Example:
*
* First, push numbers "1,2,3,4,5" to stack1, then stack1's structure is:
*
* |5|
* |4|
* |3|
* |2|
* |1|
*
* Second, if we want to get the front element "1",we should pop all the elements of stack1 and push them into stack2,
* after this, stack1 is empty, and stack2's structrue is:
*
* |1|
* |2|
* |3|
* |4|
* |5|
*
* So we can get stack2's top element "1" as the front element of queue.
*
* Next, if we want to push "6" to the back of queue, we should push "6" into stack1 as before, so stack1's structure is:
*
* |6|
*
* Finally, if we want to do pop operation twice ,we should remove the top element of stack2 twice, so stack2's structure is:
*
* |3|
* |4|
* |5|
*
* as expect, the removed element is "1" and "2".
*/
public class MyQueue {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
public int size;
public MyQueue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
size = 0;
}

public void push(int x) {
stack1.push(x);
size++;
}

public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
int value = stack2.pop();
size--;
return value;
}

public int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
int value = stack2.peek();
return value;
}

public boolean empty() {
return size == 0 ? true : false;
}
}
23 changes: 23 additions & 0 deletions algorithms/java/src/myQueue/TestMyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package myQueue;

import org.junit.Assert;
import org.junit.Test;
/**
* Test for 232. Implement Queue using Stacks
*/
public class TestMyQueue {
@Test
public void test(){
MyQueue queue=new MyQueue();
Assert.assertTrue(queue.empty());
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
Assert.assertTrue(queue.pop()==1);
Assert.assertTrue(queue.pop()==2);
queue.push(5);
Assert.assertTrue(queue.peek()==3);
Assert.assertTrue(!queue.empty());
}
}
94 changes: 94 additions & 0 deletions algorithms/java/src/myStack/MyStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Source : https://leetcode.com/problems/implement-stack-using-queues/description/
// Author : Tianming Cao
// Date : 2018-02-02

/**********************************************************************************
* Implement the following operations of a stack using queues.
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* empty() -- Return whether the stack is empty.
* Notes:
* You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
* Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
* You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
*
**********************************************************************************/
package myStack;

import java.util.LinkedList;
import java.util.Queue;
/**********************************************************************************
* This problem is a sibling of problem 232(https://leetcode.com/problems/implement-queue-using-stacks/description/).
* The train of thought is:
* If we want to pop a element,first we should judge which queue is not empty assumed queue1,another empty queue assumed queue2.
* Next,we pop all element from queue1 and push them into queue2 util queue1's size is 1.
* Finally the last element in queue1 is the correct pop value.
* If queue1 is empty and queue2 is not empty,the step is symmetric.
* The top operation is similar with pop operation.
**********************************************************************************/
public class MyStack {
public Queue<Integer> queue1;
public Queue<Integer> queue2;
public int flag;
public int size;
public MyStack() {
queue1=new LinkedList<Integer>();
queue2=new LinkedList<Integer>();
flag=1;
size=0;
}

public void push(int x) {
if(flag==1){
queue1.offer(x);
}else{
queue2.offer(x);
}
size++;
}

public int pop() {
int value;
if(flag==1){
while(queue1.size()>1){
queue2.offer(queue1.poll());
}
value=queue1.poll();
flag=2;
}else{
while(queue2.size()>1){
queue1.offer(queue2.poll());
}
value=queue2.poll();
flag=1;
}
size--;
return value;
}

public int top() {
if(flag==1){
while(queue1.size()>1){
queue2.offer(queue1.poll());
}
int value=queue1.poll();
queue2.offer(value);
flag=2;
return value;
}else{
while(queue2.size()>1){
queue1.offer(queue2.poll());
}
int value=queue2.poll();
queue1.offer(value);
flag=1;
return value;
}
}

public boolean empty() {
return size==0?true:false;
}
}
23 changes: 23 additions & 0 deletions algorithms/java/src/myStack/TestMyStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package myStack;

import org.junit.Assert;
import org.junit.Test;
/**
* Test for 225. Implement Stack using Queues
*/
public class TestMyStack {
@Test
public void test(){
MyStack stack=new MyStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
Assert.assertTrue(stack.empty()==false);
Assert.assertTrue(stack.pop()==4);
Assert.assertTrue(stack.pop()==3);
Assert.assertTrue(stack.top()==2);
stack.push(5);
Assert.assertTrue(stack.top()==5);
}
}
Loading

0 comments on commit ec2c5f7

Please sign in to comment.