From 374d7cd9761115ae13f190b74ab4106d81a019a6 Mon Sep 17 00:00:00 2001 From: gongxun Date: Mon, 27 Feb 2017 17:39:00 +0800 Subject: [PATCH] homework part 1 --- .../{ => 2.26}/binarytree/BinaryTree.java | 110 +++--- .../{ => 2.26}/binarytree/DLRSearchType.java | 32 +- .../{ => 2.26}/binarytree/LDRSearchType.java | 30 +- .../{ => 2.26}/binarytree/LFSearchType.java | 54 +-- .../{ => 2.26}/binarytree/LRDSearchType.java | 30 +- .../{ => 2.26}/binarytree/SearchType.java | 18 +- .../785396327/{ => 2.26}/list/ArrayList.java | 288 ++++++++------- .../785396327/{ => 2.26}/list/Iterator.java | 26 +- .../785396327/{ => 2.26}/list/LinkedList.java | 336 +++++++++--------- group17/785396327/{ => 2.26}/list/List.java | 55 +-- group17/785396327/{ => 2.26}/queue/Queue.java | 84 ++--- group17/785396327/{ => 2.26}/stack/Stack.java | 60 ++-- group17/785396327/3.5/array/ArrayUtils.java | 103 ++++++ .../785396327/3.5/array/ArrayUtilsTest.java | 81 +++++ 14 files changed, 764 insertions(+), 543 deletions(-) rename group17/785396327/{ => 2.26}/binarytree/BinaryTree.java (96%) rename group17/785396327/{ => 2.26}/binarytree/DLRSearchType.java (95%) rename group17/785396327/{ => 2.26}/binarytree/LDRSearchType.java (96%) rename group17/785396327/{ => 2.26}/binarytree/LFSearchType.java (87%) rename group17/785396327/{ => 2.26}/binarytree/LRDSearchType.java (96%) rename group17/785396327/{ => 2.26}/binarytree/SearchType.java (93%) rename group17/785396327/{ => 2.26}/list/ArrayList.java (71%) rename group17/785396327/{ => 2.26}/list/Iterator.java (91%) rename group17/785396327/{ => 2.26}/list/LinkedList.java (96%) rename group17/785396327/{ => 2.26}/list/List.java (84%) rename group17/785396327/{ => 2.26}/queue/Queue.java (95%) rename group17/785396327/{ => 2.26}/stack/Stack.java (94%) create mode 100644 group17/785396327/3.5/array/ArrayUtils.java create mode 100644 group17/785396327/3.5/array/ArrayUtilsTest.java diff --git a/group17/785396327/binarytree/BinaryTree.java b/group17/785396327/2.26/binarytree/BinaryTree.java similarity index 96% rename from group17/785396327/binarytree/BinaryTree.java rename to group17/785396327/2.26/binarytree/BinaryTree.java index d55c65f8e4..bf5a6b639c 100644 --- a/group17/785396327/binarytree/BinaryTree.java +++ b/group17/785396327/2.26/binarytree/BinaryTree.java @@ -1,55 +1,55 @@ -package binarytree; - -/** - * Created by william on 2017/2/16. - */ -public class BinaryTree { - private Node root; - - class Node { - private Node left; - private Node right; - private Comparable data; - - public Node(Node left, Node right, Comparable data) { - this.left = left; - this.right = right; - this.data = data; - } - - private void add(Comparable data) { - if (this.data.compareTo(data) >= 0) - if (this.left == null) - this.left = new Node(null, null, data); - else - left.add(data); - else if (this.data.compareTo(data) < 0) - if (this.right == null) - this.right = new Node(null, null, data); - else - this.right.add(data); - } - - public Comparable getData() { - return this.data; - } - - public Node getLeft() { - return this.left; - } - - public Node getRight() { - return this.right; - } - } - - public void add(Comparable data) { - if (this.root == null) - root = new Node(null, null, data); - else this.root.add(data); - } - - public void printByType(SearchType type) { - type.printByType(this.root); - } -} +package binarytree; + +/** + * Created by william on 2017/2/16. + */ +public class BinaryTree { + private Node root; + + class Node { + private Node left; + private Node right; + private Comparable data; + + public Node(Node left, Node right, Comparable data) { + this.left = left; + this.right = right; + this.data = data; + } + + private void add(Comparable data) { + if (this.data.compareTo(data) >= 0) + if (this.left == null) + this.left = new Node(null, null, data); + else + left.add(data); + else if (this.data.compareTo(data) < 0) + if (this.right == null) + this.right = new Node(null, null, data); + else + this.right.add(data); + } + + public Comparable getData() { + return this.data; + } + + public Node getLeft() { + return this.left; + } + + public Node getRight() { + return this.right; + } + } + + public void add(Comparable data) { + if (this.root == null) + root = new Node(null, null, data); + else this.root.add(data); + } + + public void printByType(SearchType type) { + type.printByType(this.root); + } +} diff --git a/group17/785396327/binarytree/DLRSearchType.java b/group17/785396327/2.26/binarytree/DLRSearchType.java similarity index 95% rename from group17/785396327/binarytree/DLRSearchType.java rename to group17/785396327/2.26/binarytree/DLRSearchType.java index 7f9ba1c38d..3d1adbbdc7 100644 --- a/group17/785396327/binarytree/DLRSearchType.java +++ b/group17/785396327/2.26/binarytree/DLRSearchType.java @@ -1,16 +1,16 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class DLRSearchType implements SearchType { - - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - System.out.print(root.getData()+" "); - printByType(root.getLeft()); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class DLRSearchType implements SearchType { + + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + System.out.print(root.getData()+" "); + printByType(root.getLeft()); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LDRSearchType.java b/group17/785396327/2.26/binarytree/LDRSearchType.java similarity index 96% rename from group17/785396327/binarytree/LDRSearchType.java rename to group17/785396327/2.26/binarytree/LDRSearchType.java index f67b6dcb81..da18dbc1b9 100644 --- a/group17/785396327/binarytree/LDRSearchType.java +++ b/group17/785396327/2.26/binarytree/LDRSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LDRSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - System.out.print(root.getData() + " "); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LDRSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + System.out.print(root.getData() + " "); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LFSearchType.java b/group17/785396327/2.26/binarytree/LFSearchType.java similarity index 87% rename from group17/785396327/binarytree/LFSearchType.java rename to group17/785396327/2.26/binarytree/LFSearchType.java index e88e76b210..5f908664c9 100644 --- a/group17/785396327/binarytree/LFSearchType.java +++ b/group17/785396327/2.26/binarytree/LFSearchType.java @@ -1,27 +1,27 @@ -package binarytree; - -import java.util.LinkedList; - -/** - * Created by william on 2017/2/18. - */ -public class LFSearchType implements SearchType { - private LinkedList queue = new LinkedList<>(); - - @Override - public void printByType(BinaryTree.Node root) { - if (root == null) - return; - queue.offer(root); - while (!queue.isEmpty()) { - BinaryTree.Node curNode = queue.poll(); - System.out.print(curNode.getData() + " "); - if (curNode.getLeft() != null) - queue.offer(curNode.getLeft()); - if (curNode.getRight() != null) - queue.offer(curNode.getRight()); - } - - } - -} +package binarytree; + +import java.util.LinkedList; + +/** + * Created by william on 2017/2/18. + */ +public class LFSearchType implements SearchType { + private LinkedList queue = new LinkedList(); + + @Override + public void printByType(BinaryTree.Node root) { + if (root == null) + return; + queue.offer(root); + while (!queue.isEmpty()) { + BinaryTree.Node curNode = queue.poll(); + System.out.print(curNode.getData() + " "); + if (curNode.getLeft() != null) + queue.offer(curNode.getLeft()); + if (curNode.getRight() != null) + queue.offer(curNode.getRight()); + } + + } + +} diff --git a/group17/785396327/binarytree/LRDSearchType.java b/group17/785396327/2.26/binarytree/LRDSearchType.java similarity index 96% rename from group17/785396327/binarytree/LRDSearchType.java rename to group17/785396327/2.26/binarytree/LRDSearchType.java index dbd5f1e820..250645e6df 100644 --- a/group17/785396327/binarytree/LRDSearchType.java +++ b/group17/785396327/2.26/binarytree/LRDSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LRDSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - printByType(root.getRight()); - System.out.print(root.getData() + " "); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LRDSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + printByType(root.getRight()); + System.out.print(root.getData() + " "); + } + } +} diff --git a/group17/785396327/binarytree/SearchType.java b/group17/785396327/2.26/binarytree/SearchType.java similarity index 93% rename from group17/785396327/binarytree/SearchType.java rename to group17/785396327/2.26/binarytree/SearchType.java index 78f43d0c2d..606124a781 100644 --- a/group17/785396327/binarytree/SearchType.java +++ b/group17/785396327/2.26/binarytree/SearchType.java @@ -1,9 +1,9 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public interface SearchType { - - void printByType(T root); -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public interface SearchType { + + void printByType(T root); +} diff --git a/group17/785396327/list/ArrayList.java b/group17/785396327/2.26/list/ArrayList.java similarity index 71% rename from group17/785396327/list/ArrayList.java rename to group17/785396327/2.26/list/ArrayList.java index f4e5f26f40..119079f7ce 100644 --- a/group17/785396327/list/ArrayList.java +++ b/group17/785396327/2.26/list/ArrayList.java @@ -1,135 +1,153 @@ -package list; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 10; - private int size; - private Object[] elementData; - - public ArrayList() { - elementData = new Object[DEFAULT_CAPACITY]; - } - - public ArrayList(int initialCapacity) { - if (initialCapacity < 0) - throw new RuntimeException("非法初始化大小参数!"); - elementData = new Object[initialCapacity]; - } - - public boolean add(T ele) { - grow(); - elementData[size] = ele; - size++; - return true; - } - - public T get(int index) { - checkBounds(index); - return (T) elementData[index]; - } - - public T remove(int index) { - checkBounds(index); - T removeEle = (T) elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index); - size--; - return removeEle; - } - - public boolean add(int index, T ele) { - checkBounds(index); - size++;//有效元素内容先加,保证长度极限情况grow在添加前生效 - grow(); - //将原本数组从待插入的index截取,将原本index后的有效值,复制到原本数组index+1之后 - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = ele; - return true; - } - - @Override - public boolean remove(T ele) { - int index; - if ((index = indexOf(ele)) == -1) - return false; - remove(index); - return true; - } - - private void checkBounds(int index) { - if (index < 0 || index >= size) - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - public int size() { - return size; - } - - private void grow() { - if (size >= elementData.length) { - int curLen = elementData.length; - int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); - elementData = Arrays.copyOf(elementData, newLen); - } - } - - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - return indexOf(ele) != -1; - } - - public int indexOf(T ele) { - for (int i = 0; i < size; i++) { - if (ele == null) - if (null == elementData[i]) - return i; - else if (ele.equals(elementData[i])) - return i; - } - return -1; - } - - public Iterator iterator() { - return new Itr(); - } - - private class Itr implements Iterator { - int cursor;//待遍历元素的下标 - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - public T next() { - if (cursor >= size) - throw new NoSuchElementException(); - return (T) elementData[cursor++]; - } - - @Override - public void remove() { - if (cursor >= size) - throw new NoSuchElementException(); - ArrayList.this.remove(cursor--); - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - for (Object ele : elementData) { - sb.append(ele).append(" "); - } - return sb.append("]").toString(); - } -} +package list; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) + throw new RuntimeException("非法初始化大小参数!"); + elementData = new Object[initialCapacity]; + } + + public ArrayList(T[] array) { + if (array == null) + throw new NullPointerException(); + elementData = array; + } + + public boolean add(T ele) { + grow(size); + elementData[size++] = ele; + return true; + } + + public T get(int index) { + checkBounds(index); + return (T) elementData[index]; + } + + public T remove(int index) { + checkBounds(index); + T removeEle = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return removeEle; + } + + public boolean add(int index, T ele) { + checkBounds(index); + grow(size++); + //将原本数组从待插入的index截取,将原本index后的有效值,复制到原本数组index+1之后 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = ele; + return true; + } + + + @Override + public boolean remove(T ele) { + int index; + if ((index = indexOf(ele)) == -1) + return false; + remove(index); + return true; + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + public int size() { + return size; + } + + private void grow(int miniCapacity) { + if (miniCapacity >= elementData.length) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : oldCapacity + (oldCapacity >> 1); + if (newCapacity - miniCapacity < 0) + newCapacity = miniCapacity > Integer.MAX_VALUE ? Integer.MAX_VALUE : miniCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + return indexOf(ele) != -1; + } + + public int indexOf(T ele) { + for (int i = 0; i < size; i++) { + if ((ele == null && elementData[i] == null) || (ele.equals(elementData[i]))) + return i; + } + return -1; + } + + @Override + public boolean addAll(List l) { + Object[] eles = l.toArray(); + grow(eles.length + size); + System.arraycopy(eles, 0, elementData, size, eles.length); + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size); + } + + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//待遍历元素的下标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public T next() { + if (cursor >= size) + throw new NoSuchElementException(); + return (T) elementData[cursor++]; + } + + @Override + public void remove() { + if (cursor >= size) + throw new NoSuchElementException(); + ArrayList.this.remove(cursor--); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + for (Object ele : elementData) { + sb.append(ele).append(" "); + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/Iterator.java b/group17/785396327/2.26/list/Iterator.java similarity index 91% rename from group17/785396327/list/Iterator.java rename to group17/785396327/2.26/list/Iterator.java index 382dbf0c84..0df87c6cf1 100644 --- a/group17/785396327/list/Iterator.java +++ b/group17/785396327/2.26/list/Iterator.java @@ -1,13 +1,13 @@ -package list; - -/** - * Created by IBM on 2017/2/25. - */ -public interface Iterator { - - boolean hasNext(); - - T next(); - - void remove(); -} +package list; + +/** + * Created by IBM on 2017/2/25. + */ +public interface Iterator { + + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group17/785396327/list/LinkedList.java b/group17/785396327/2.26/list/LinkedList.java similarity index 96% rename from group17/785396327/list/LinkedList.java rename to group17/785396327/2.26/list/LinkedList.java index c0cbaf7670..a55f723ecb 100644 --- a/group17/785396327/list/LinkedList.java +++ b/group17/785396327/2.26/list/LinkedList.java @@ -1,163 +1,173 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public class LinkedList implements List { - private int size; - private Node first; - private Node last; - - private static class Node { - Node next; - Node prev; - T data; - - Node(Node prev, Node next, T data) { - this.prev = prev; - this.next = next; - this.data = data; - } - } - - @Override - public int size() { - return this.size; - } - - @Override - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - Node head = first; - while (head != null) { - if ((ele == null && head.data == null) || (ele.equals(head.data))) - return true; - head = head.next; - } - return false; - } - - @Override - public boolean add(T ele) { - if (first == null) - first = last = new Node(null, null, ele); - else { - //新添加节点的上一个节点是原来链表的最后一个节点 - Node addNode = new Node(last, null, ele); - //原来链表的最后一个节点的下一个节点需要指向新添加的节点 - last.next = addNode; - //更新最后一个节点为新添加的节点 - last = addNode; - } - size++; - return true; - } - - @Override - public boolean add(int index, T ele) { - checkBounds(index, true); - if (index == size) add(ele); - else { - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index - 1)//得到要插入位置的前一个节点 - head.next = new Node(head, head.next, ele); - else - head = head.next; - } - } - size++; - return true; - } - - @Override - public boolean remove(T ele) { - if (!contains(ele)) - return false; - Node head = first; - Node prev = head.prev; - while (head != null) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) { - prev.next = head.next; - size--; - return true; - } - prev = head; - head = head.next; - } - return false; - } - - @Override - public T remove(int index) { - checkBounds(index, false); - T removeEle = get(index); - remove(removeEle); - return removeEle; - } - - @Override - public T get(int index) { - checkBounds(index, false); - if (index > (size >> 1)) { - //索引位置大于1/2size,从后往前 - Node tail = last; - for (int i = size - 1; i >= 0; i--) { - if (i == index) - return (T) tail.data; - else - tail = tail.prev; - } - } else { - //从前往后 - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index) - return (T) head.data; - else - head = head.next; - } - } - return null; - } - - @Override - public int indexOf(T ele) { - if (first == null) return -1; - Node head = first; - for (int i = 0; i < size; i++) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) - return i; - head = head.next; - } - return -1; - } - - /** - * 指定位置查找元素和插入元素到指定位置IndexOutofBounds的判断标准不一样 - * - * @param index - * @param isInsert - */ - private void checkBounds(int index, boolean isInsert) { - if (isInsert && (index < 0 || index > size))//允许插入到最后一个元素之后,不能排除= - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - if (index < 0 || index >= size)//查询从0 --- size-1 - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - Node head = first; - while (head != null) { - sb.append(head.data + " "); - head = head.next; - } - return sb.append("]").toString(); - } -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + + private static class Node { + Node next; + Node prev; + T data; + + Node(Node prev, Node next, T data) { + this.prev = prev; + this.next = next; + this.data = data; + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + Node head = first; + while (head != null) { + if ((ele == null && head.data == null) || (ele.equals(head.data))) + return true; + head = head.next; + } + return false; + } + + @Override + public boolean add(T ele) { + if (first == null) + first = last = new Node(null, null, ele); + else { + //新添加节点的上一个节点是原来链表的最后一个节点 + Node addNode = new Node(last, null, ele); + //原来链表的最后一个节点的下一个节点需要指向新添加的节点 + last.next = addNode; + //更新最后一个节点为新添加的节点 + last = addNode; + } + size++; + return true; + } + + @Override + public boolean add(int index, T ele) { + checkBounds(index, true); + if (index == size) add(ele); + else { + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index - 1)//得到要插入位置的前一个节点 + head.next = new Node(head, head.next, ele); + else + head = head.next; + } + } + size++; + return true; + } + + @Override + public boolean remove(T ele) { + if (!contains(ele)) + return false; + Node head = first; + Node prev = head.prev; + while (head != null) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) { + prev.next = head.next; + size--; + return true; + } + prev = head; + head = head.next; + } + return false; + } + + @Override + public T remove(int index) { + checkBounds(index, false); + T removeEle = get(index); + remove(removeEle); + return removeEle; + } + + @Override + public T get(int index) { + checkBounds(index, false); + if (index > (size >> 1)) { + //索引位置大于1/2size,从后往前 + Node tail = last; + for (int i = size - 1; i >= 0; i--) { + if (i == index) + return (T) tail.data; + else + tail = tail.prev; + } + } else { + //从前往后 + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index) + return (T) head.data; + else + head = head.next; + } + } + return null; + } + + @Override + public int indexOf(T ele) { + if (first == null) return -1; + Node head = first; + for (int i = 0; i < size; i++) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) + return i; + head = head.next; + } + return -1; + } + + @Override + public boolean addAll(List l) { + return false; + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + /** + * 指定位置查找元素和插入元素到指定位置IndexOutofBounds的判断标准不一样 + * + * @param index + * @param isInsert + */ + private void checkBounds(int index, boolean isInsert) { + if (isInsert && (index < 0 || index > size))//允许插入到最后一个元素之后,不能排除= + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + if (index < 0 || index >= size)//查询从0 --- size-1 + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + Node head = first; + while (head != null) { + sb.append(head.data + " "); + head = head.next; + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/List.java b/group17/785396327/2.26/list/List.java similarity index 84% rename from group17/785396327/list/List.java rename to group17/785396327/2.26/list/List.java index 54fb72108d..e03b4cfa7c 100644 --- a/group17/785396327/list/List.java +++ b/group17/785396327/2.26/list/List.java @@ -1,25 +1,30 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public interface List { - - int size(); - - boolean isEmpty(); - - boolean contains(T ele); - - boolean add(T ele); - - boolean add(int index, T ele); - - boolean remove(T ele); - - T remove(int index); - - T get(int index); - - int indexOf(T ele); -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean contains(T ele); + + boolean add(T ele); + + boolean add(int index, T ele); + + boolean remove(T ele); + + T remove(int index); + + T get(int index); + + int indexOf(T ele); + + boolean addAll(List l); + + Object[] toArray(); + +} diff --git a/group17/785396327/queue/Queue.java b/group17/785396327/2.26/queue/Queue.java similarity index 95% rename from group17/785396327/queue/Queue.java rename to group17/785396327/2.26/queue/Queue.java index 7e399961ed..62404a951e 100644 --- a/group17/785396327/queue/Queue.java +++ b/group17/785396327/2.26/queue/Queue.java @@ -1,41 +1,43 @@ -package queue; - -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class Queue extends LinkedList { - - public boolean add(T ele) { - return add(ele); - } - - public T element() { - if (size() == 0) - throw new NoSuchElementException("队列中没有元素!"); - return get(0); - } - - public boolean offer(T ele) { - return add(ele); - } - - public T peek() { - if (size() == 0) - return null; - return get(0); - } - - public T poll() { - if (size() == 0) - return null; - return remove(0); - } - - public T remove() { - if (size() == 0) - throw new NoSuchElementException("队列中没有元素!"); - return remove(0); - } -} +package queue; + +import list.LinkedList; + +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class Queue extends LinkedList { + + public boolean add(T ele) { + return add(ele); + } + + public T element() { + if (size() == 0) + throw new NoSuchElementException("队列中没有元素!"); + return get(0); + } + + public boolean offer(T ele) { + return add(ele); + } + + public T peek() { + if (size() == 0) + return null; + return get(0); + } + + public T poll() { + if (size() == 0) + return null; + return remove(0); + } + + public T remove() { + if (size() == 0) + throw new NoSuchElementException("队列中没有元素!"); + return remove(0); + } +} diff --git a/group17/785396327/stack/Stack.java b/group17/785396327/2.26/stack/Stack.java similarity index 94% rename from group17/785396327/stack/Stack.java rename to group17/785396327/2.26/stack/Stack.java index 980ca2c0e6..58efd9c0c3 100644 --- a/group17/785396327/stack/Stack.java +++ b/group17/785396327/2.26/stack/Stack.java @@ -1,29 +1,31 @@ -package stack; - -import java.util.EmptyStackException; - -/** - * Created by william on 2017/2/25. - */ -public class Stack extends ArrayList { - - public boolean empty() { - return isEmpty(); - } - - public T peek() { - if (size() == 0) - throw new EmptyStackException(); - return (T) get(0); - } - - public T pop() { - if (size() == 0) - throw new EmptyStackException(); - return (T) remove(0); - } - - public void push(T ele) { - add(0, ele); - } -} +package stack; + +import list.ArrayList; + +import java.util.EmptyStackException; + +/** + * Created by william on 2017/2/25. + */ +public class Stack extends ArrayList { + + public boolean empty() { + return isEmpty(); + } + + public T peek() { + if (size() == 0) + throw new EmptyStackException(); + return (T) get(0); + } + + public T pop() { + if (size() == 0) + throw new EmptyStackException(); + return (T) remove(0); + } + + public void push(T ele) { + add(0, ele); + } +} diff --git a/group17/785396327/3.5/array/ArrayUtils.java b/group17/785396327/3.5/array/ArrayUtils.java new file mode 100644 index 0000000000..9334dbe5f2 --- /dev/null +++ b/group17/785396327/3.5/array/ArrayUtils.java @@ -0,0 +1,103 @@ +package array; + +import list.ArrayList; +import list.List; + +import java.util.Arrays; +import java.util.NoSuchElementException; + + +/** + * Created by william on 2017/2/27. + */ +public class ArrayUtils { + private int[] fibonacciArray; + + + public static void reserveArray(int[] src) { + int begin = 0; + int end = src.length - 1; + while (begin < end) { + swap(src, begin++, end--); + } + } + + public static int[] removeZero(int[] oldArray) { + List newResult = new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + newResult.add(oldArray[i]); + } + + return toIntArray(newResult); + } + + public static int[] merge(int[] array1, int[] array2) { + int[] temp = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, temp, 0, array1.length); + System.arraycopy(array2, 0, temp, array1.length, array2.length); + Arrays.sort(temp); + List result = new ArrayList(); + for (int ele : temp) { + if (!result.contains(ele)) + result.add(ele); + } + return toIntArray(result); + } + + public static int[] grow(int[] oldArray, int size) { + if (size <= 0) + throw new NoSuchElementException(); + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + public static int[] fibonacci(int max) { + return null; + } + + public static int[] getPrimes(int max) { + List result = new ArrayList(); + for (int i = 0; i < max; i++) { + if (i % 2 == 1) + result.add(i); + } + return toIntArray(result); + } + + public static int[] getPerfectNumbers(int max) { + List result = new ArrayList(); + for (int i = 1; i <= max; i++) { + int sum = 0; + for (int j = 1; j < i / 2 + 1; j++) { + if (i % j == 0) + sum += j; + } + if (i == sum) + result.add(i); + } + return toIntArray(result); + } + + private static int[] toIntArray(List src) { + int[] result = new int[src.size()]; + for (int i = 0; i < src.size(); i++) { + result[i] = src.get(i); + } + return result; + } + + public static String join(int[] array, String seperator) { + String value = Arrays.toString(array).replaceAll(", ", seperator == null ? "-" : seperator); + return value.substring(1, value.length() - 1); + } + + + private static void swap(int[] array, int i, int j) { + array[i] ^= array[j]; + array[j] ^= array[i]; + array[i] ^= array[j]; + } + +} diff --git a/group17/785396327/3.5/array/ArrayUtilsTest.java b/group17/785396327/3.5/array/ArrayUtilsTest.java new file mode 100644 index 0000000000..5bafc329c8 --- /dev/null +++ b/group17/785396327/3.5/array/ArrayUtilsTest.java @@ -0,0 +1,81 @@ +package array; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +/** + * Created by william on 2017/2/27. + */ +public class ArrayUtilsTest { + int[] array; + private static final int RANGE = 9; + private static final int BOUNDS = 10; + private static Random random = new Random(); + + + @Before + public void setUp() { + array = randomArray(RANGE); + } + + private int[] randomArray(int range) { + int[] array = new int[range]; + for (int i = 0; i < range; i++) { + array[i] = random.nextInt(BOUNDS); + } + return array; + } + + @Test + public void reverseArrayTest() { + System.out.println(Arrays.toString(array)); + ArrayUtils.reserveArray(array); + System.out.println(Arrays.toString(array)); + } + + @Test + public void removeZeroTest() { + System.out.println(Arrays.toString(array)); + int[] newArray = ArrayUtils.removeZero(array); + System.out.println(Arrays.toString(newArray)); + } + + @Test + public void mergeTest() { + System.out.println(Arrays.toString(array)); + int[] array2 = {2, 5, 1, 6, 8}; + int[] merge = ArrayUtils.merge(array, array2); + System.out.println(Arrays.toString(merge)); + } + + @Test + public void growTest() { + System.out.println(Arrays.toString(array)); + int[] result = ArrayUtils.grow(array, 4); + System.out.println(Arrays.toString(result)); + } + + @Test + public void getPrimesTest() { + int[] primes = ArrayUtils.getPrimes(54); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbersTest() { + int[] perfectNumbers = ArrayUtils.getPerfectNumbers(100000); + System.out.println(Arrays.toString(perfectNumbers)); + } + + @Test + public void joinTest() { + String value = ArrayUtils.join(array, "-"); + System.out.println(value); + } + +}