-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/onlyliuxin/coding2017.git
- Loading branch information
Showing
40 changed files
with
1,907 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
81 changes: 81 additions & 0 deletions
81
...0084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/queue/CircleQueue.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.xukai.coderising.queue; | ||
|
||
/** | ||
* 用数组实现循环队列 | ||
* @author liuxin | ||
* | ||
* @param <E> | ||
*/ | ||
public class CircleQueue <E> { | ||
|
||
private final static int DEFAULT_SIZE = 10; | ||
|
||
private int count = 0; | ||
|
||
//用数组来保存循环队列的元素 | ||
private Object[] elementData = new Object[DEFAULT_SIZE] ; | ||
|
||
//队头 | ||
private int front = 0; | ||
//队尾 | ||
private int rear = 0; | ||
|
||
public boolean isEmpty() { | ||
return count == 0; | ||
|
||
} | ||
|
||
public int size() { | ||
return count; | ||
} | ||
|
||
|
||
|
||
public void enQueue(E data) { | ||
if (count == DEFAULT_SIZE ) { | ||
throw new RuntimeException(); | ||
} | ||
elementData[front] = data; | ||
front = (front + 1) % DEFAULT_SIZE; | ||
count++; | ||
} | ||
|
||
public E deQueue() { | ||
if (count == 0) { | ||
return null; | ||
} | ||
count--; | ||
E e = (E)elementData[rear]; | ||
rear = (rear + 1) % DEFAULT_SIZE; | ||
return e; | ||
} | ||
|
||
public static void main(String[] args) { | ||
CircleQueue<Integer> queue = new CircleQueue<>(); | ||
queue.enQueue(1); | ||
queue.enQueue(2); | ||
queue.enQueue(3); | ||
queue.enQueue(4); | ||
queue.enQueue(5); | ||
queue.enQueue(6); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue());System.out.println(queue.deQueue());System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
queue.enQueue(7); | ||
queue.enQueue(8); | ||
queue.enQueue(9); | ||
queue.enQueue(0); | ||
queue.enQueue(1); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
|
||
} | ||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...7220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/queue/Josephus.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.xukai.coderising.queue; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 用Queue来实现Josephus问题 | ||
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 | ||
* 该方法返回一个List, 包含了被杀死人的次序 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class Josephus { | ||
|
||
|
||
public static List<Integer> execute(int n, int m){ | ||
Preconditions.checkArgument(n > 0); | ||
ArrayList<Integer> deadList = new ArrayList<>(); | ||
ArrayDeque<Integer> deque1 = new ArrayDeque<>(); | ||
ArrayDeque<Integer> deque2 = new ArrayDeque<>(); | ||
for (int i = 0; i < n; i++) { | ||
deque1.offer(i); | ||
} | ||
int count = 0; | ||
while(n >= m){ | ||
if (deque1.isEmpty()) { | ||
while (!deque2.isEmpty()){ | ||
deque1.offer(deque2.poll()); | ||
} | ||
} | ||
count++; | ||
if (count == m) { | ||
deadList.add(deque1.poll()); | ||
count = 0; | ||
n--; | ||
continue; | ||
} | ||
deque2.offer(deque1.poll()); | ||
|
||
} | ||
|
||
return deadList; | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/queue/JosephusTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.xukai.coderising.queue; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
public class JosephusTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testExecute() { | ||
|
||
Assert.assertEquals("[1, 3, 5, 0, 4, 2]", Josephus.execute(7, 2).toString()); | ||
|
||
|
||
|
||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...kai_coding/coding-common/src/main/java/org/xukai/coderising/queue/QueueWithTwoStacks.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.xukai.coderising.queue; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* 用两个栈来实现一个队列 | ||
* @author liuxin | ||
* | ||
* @param <E> | ||
*/ | ||
public class QueueWithTwoStacks<E> { | ||
private Stack<E> stack1; | ||
private Stack<E> stack2; | ||
|
||
|
||
public QueueWithTwoStacks() { | ||
stack1 = new Stack<E>(); | ||
stack2 = new Stack<E>(); | ||
} | ||
|
||
|
||
|
||
|
||
public boolean isEmpty() { | ||
return stack1.isEmpty(); | ||
} | ||
|
||
|
||
|
||
public int size() { | ||
return stack1.size(); | ||
} | ||
|
||
|
||
|
||
public void enQueue(E item) { | ||
stack1.push(item); | ||
} | ||
|
||
public E deQueue() { | ||
if (stack1.isEmpty()) { | ||
return null; | ||
} | ||
while (!stack1.isEmpty()) { | ||
stack2.push(stack1.pop()); | ||
} | ||
E e = stack2.pop(); | ||
while (!stack2.isEmpty()) { | ||
stack1.push(stack2.pop()); | ||
} | ||
return e; | ||
} | ||
|
||
public static void main(String[] args) { | ||
QueueWithTwoStacks queue = new QueueWithTwoStacks(); | ||
queue.enQueue(1); | ||
queue.enQueue(2); | ||
queue.enQueue(3); | ||
queue.enQueue(4); | ||
queue.enQueue(5); | ||
queue.enQueue(6); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue());System.out.println(queue.deQueue());System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
queue.enQueue(7); | ||
queue.enQueue(8); | ||
queue.enQueue(9); | ||
queue.enQueue(0); | ||
queue.enQueue(1); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
System.out.println(queue.deQueue()); | ||
|
||
|
||
} | ||
|
||
} | ||
|
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
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
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
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
Oops, something went wrong.