forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from runMark/master
第一次作业
- Loading branch information
Showing
17 changed files
with
902 additions
and
0 deletions.
There are no files selected for viewing
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,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.mark</groupId> | ||
<artifactId>algo</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
17 changes: 17 additions & 0 deletions
17
group14/676615845/algo/src/main/java/algo/BinarySearch.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,17 @@ | ||
package algo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by mark on 17/2/23. | ||
*/ | ||
public class BinarySearch { | ||
|
||
public static int rank(int key, int[] a) { | ||
List list = new ArrayList(); | ||
list = new LinkedList(); | ||
return -1; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
group14/676615845/algo/src/main/java/com/coding/basic/Array.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,16 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by mark on 17/2/24. | ||
*/ | ||
public class Array { | ||
|
||
public static Object[] grow(Object[] src, int size) { | ||
return Arrays.copyOf(src, src.length + size); | ||
// Object[] target = new Object[src.length + size]; | ||
// System.arraycopy(src, 0, target, 0, src.length); | ||
|
||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.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,73 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size; // ArrayList 中的实际元素个数 | ||
private Object[] elementData; | ||
|
||
public ArrayList() { | ||
size = 0; | ||
elementData = new Object[100]; | ||
} | ||
|
||
public void add(Object o){ | ||
if (size >= elementData.length) { | ||
elementData = Array.grow(elementData, 100); | ||
} | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
if (size >= elementData.length) { | ||
elementData = Array.grow(elementData, 100); | ||
} | ||
|
||
if (index > size || index < 0) throw new ArrayIndexOutOfBoundsException(); | ||
|
||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index > size) throw new ArrayIndexOutOfBoundsException(); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
if (index >= size || index < 0) throw new ArrayIndexOutOfBoundsException(); | ||
|
||
Object result = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
elementData[--size] = null; | ||
return result; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
return new Iterator() { | ||
|
||
private int next = 0; // 下一个返回元素所在的位置 | ||
|
||
public boolean hasNext() { | ||
return next < size; | ||
} | ||
|
||
public Object next() { | ||
if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); | ||
return elementData[next++]; | ||
} | ||
|
||
public Object remove() { | ||
if (next <= 0) throw new IllegalStateException(); | ||
return ArrayList.this.remove(--next); | ||
} | ||
}; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.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,41 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode implements Comparable { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
return null; | ||
} | ||
|
||
public int compareTo(Object obj) { | ||
if (obj == null || obj.getClass() != Integer.class) throw new IllegalArgumentException(); | ||
return Integer.compare(((Integer) data).intValue(), ((Integer) obj).intValue()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
group14/676615845/algo/src/main/java/com/coding/basic/Iterator.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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
boolean hasNext(); | ||
Object next(); | ||
Object remove(); | ||
} |
164 changes: 164 additions & 0 deletions
164
group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.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,164 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node first = null; | ||
private Node last = null; | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
Node node = new Node(o); | ||
if (first == null) { | ||
first = node; | ||
} else { | ||
last.next = node; | ||
node.prev = last; | ||
} | ||
last = node; | ||
size++; | ||
} | ||
|
||
public void add(int index , Object o) { | ||
if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException(); | ||
|
||
Node node = new Node(o); | ||
|
||
if (first == null) { | ||
first = node; | ||
last = node; | ||
} else { | ||
if (index == 0) { | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} else if (index == size) { | ||
last.next = node; | ||
node.prev = last; | ||
last = node; | ||
} else { | ||
Node temp = first; | ||
while (--index > 0) { | ||
temp = temp.next; | ||
} | ||
node.next = temp.next; | ||
temp.next.prev = node; | ||
temp.next = node; | ||
node.prev = temp; | ||
} | ||
} | ||
size++; | ||
} | ||
public Object get(int index){ | ||
if (index < 0 || index > size - 1) throw new ArrayIndexOutOfBoundsException(); | ||
Node node = first; | ||
while (index-- > 0) { | ||
node = node.next; | ||
} | ||
return node.data; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index < 0 || index >= size) throw new ArrayIndexOutOfBoundsException(); | ||
|
||
Node node = null; | ||
if (index == 0) { | ||
node = first; | ||
if (size == 1) { | ||
first = null; | ||
last = null; | ||
} else { | ||
first = first.next; | ||
first.prev = null; | ||
} | ||
} else if (index == size - 1) { | ||
node = last; | ||
last = last.prev; | ||
last.next = null; | ||
} else { | ||
node = first; | ||
Node temp = null; | ||
while (index-- > 0) { | ||
node = node.next; | ||
} | ||
temp = node.prev; | ||
temp.next = node.next; | ||
node.next.prev = temp; | ||
} | ||
size--; | ||
return node.data; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object obj){ | ||
add(0, obj); | ||
} | ||
|
||
public void addLast(Object obj){ | ||
add(size, obj); | ||
} | ||
|
||
public Object removeFirst(){ | ||
return remove(0); | ||
} | ||
|
||
public Object removeLast(){ | ||
return remove(size - 1); | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
if (first == null || last == null) throw new IllegalStateException(); | ||
|
||
return new InnerIterator(); | ||
} | ||
|
||
private class InnerIterator implements Iterator { | ||
|
||
private Node nextNode = first; | ||
|
||
public boolean hasNext() { | ||
return nextNode != null; | ||
} | ||
|
||
public Object next() { | ||
if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); | ||
Node node = nextNode; | ||
nextNode = nextNode.next; | ||
return node.data; | ||
} | ||
|
||
public Object remove() { | ||
if (nextNode == first) throw new IllegalStateException(); | ||
|
||
Node node = nextNode.prev; | ||
if (nextNode == first.next) { | ||
first = nextNode; | ||
first.prev = null; | ||
} else if (nextNode == null) { | ||
node = last; | ||
last = last.prev; | ||
last.next = null; | ||
} else { | ||
node.prev = node.next; | ||
node.next.prev = node.prev; | ||
} | ||
return node.data; | ||
} | ||
} | ||
|
||
private static class Node{ | ||
|
||
Object data; | ||
Node next; | ||
Node prev; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
next = null; | ||
prev = null; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group14/676615845/algo/src/main/java/com/coding/basic/List.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,9 @@ | ||
package com.coding.basic; | ||
|
||
public interface List { | ||
void add(Object o); | ||
void add(int index, Object o); | ||
Object get(int index); | ||
Object remove(int index); | ||
int size(); | ||
} |
22 changes: 22 additions & 0 deletions
22
group14/676615845/algo/src/main/java/com/coding/basic/Queue.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,22 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList linkedList = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
linkedList.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return linkedList.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return linkedList.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return linkedList.size(); | ||
} | ||
} |
Oops, something went wrong.