From 71b42d116f3011e8308bf1c2395f9c902ba13def Mon Sep 17 00:00:00 2001 From: MarcoQQ <1007284166@qq.com> Date: Fri, 1 Dec 2017 09:23:56 +0800 Subject: [PATCH 1/4] Update HashMap.java --- Data Structures/HashMap/HashMap.java | 141 --------------------------- 1 file changed, 141 deletions(-) diff --git a/Data Structures/HashMap/HashMap.java b/Data Structures/HashMap/HashMap.java index 1cce6260e52c..0af0b58d61b7 100644 --- a/Data Structures/HashMap/HashMap.java +++ b/Data Structures/HashMap/HashMap.java @@ -140,144 +140,3 @@ private void rehash() throws Exception{ } } -======= -import java.util.ArrayList; -import java.util.LinkedList; - -public class HashMap { - public class hmnodes{ //HashMap nodes - K key; - V value; - } - - private int size=0; //size of hashmap - private LinkedList buckets[]; //array of addresses of list - - public HashMap(){ - buckets=new LinkedList[4]; //initially create bucket of any size - for(int i=0;i<4;i++) - buckets[i]=new LinkedList<>(); - } - - public void put(K key,V value) throws Exception{ - int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index - int fountAt=find(bi,key); //check if key already exists or not - if(fountAt==-1){ - hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert - temp.key=key; - temp.value=value; - buckets[bi].addLast(temp); - this.size++; - }else{ - buckets[bi].get(fountAt).value=value;//if already exist modify the value - } - - double lambda = (this.size*1.0)/this.buckets.length; - if(lambda>2.0){ - rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0 - } - - return; - } - - - public V get(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - return buckets[bi].get(fountAt).value; - } - } - - public V remove(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - this.size--; - return buckets[bi].remove(fountAt).value; - } - } - - public boolean containskey(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return false; - }else{ - return true; - } - } - - public int size(){ - return this.size; - } - - - public boolean isempty(){ - return this.size==0; - } - - public ArrayList keyset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i valueset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i"+temp.value+"]"); - } - System.out.println(); - } - } - - public int find(int bi,K key) throws Exception{ - for(int i=0;i ob[]= buckets; - buckets=new LinkedList[ob.length*2]; - for(int i=0;i(); - - size = 0; - for(int i=0;i>>>>>> 7e3a8c55c865471a33f6932a022a1059c5243fc3:data_structures/HashMap/HashMap.java From 4c487b950d6500432f81b02bb49e4cc7b4ebb59c Mon Sep 17 00:00:00 2001 From: MarcoQQ <1007284166@qq.com> Date: Mon, 4 Dec 2017 12:28:49 +0800 Subject: [PATCH 2/4] Update Matrix.java --- Data Structures/Matrix/Matrix.java | 44 ++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index edbdcdbf0794..90de4452c975 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -9,13 +9,15 @@ public class Matrix { public static void main(String[] args) { - int[][] data1 = new int[0][0]; + int[][] data1 = new int[0][0]; int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}; + int[][] data4 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}; Matrix m1 = new Matrix(data1); Matrix m2 = new Matrix(data2); Matrix m3 = new Matrix(data3); + Matrix m4 = new Matrix(data4); System.out.println("m1 --> Rows: " + m1.getRows() + " Columns: " + m1.getColumns()); System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns()); @@ -32,12 +34,13 @@ public static void main(String[] args) { System.out.println("m2==m1: " + m2.equals(m1)); //false System.out.println("m2==m2: " + m2.equals(m2)); //true System.out.println("m2==m3: " + m2.equals(m3)); //false + System.out.println("m3==m4: " + m3.equals(m4)); //true //test operations (valid) System.out.println("2 * m2:\n" + m2.scale(2)); System.out.println("m2 + m3:\n" + m2.plus(m3)); System.out.println("m2 - m3:\n" + m2.minus(m3)); - System.out.println("m2 * m3: \n"+m2.multiply(m3)); + System.out.println("m2 * m3: \n"+m2.multiply(m3)); //product } @@ -191,34 +194,45 @@ public Matrix multiply(Matrix other) throws RuntimeException { * @param other : the other matrix * @return boolean */ - public boolean equals(Matrix other) { - return this == other; + public boolean equals(Matrix other){ + if(other == null || this == null)return false; + if(other.getColumns() == getColumns() && other.getRows() == getRows()){ + for(int i = 0; i < getRows(); i++) + for(int j = 0; j < getColumns(); j++){ + if(other.getElement(i, j) != getElement(i, j)){ + return false; + } + } + return true; + } + return false; } /** * Returns the Matrix as a String in the following format * - * [ a b c ] ... - * [ x y z ] ... - * [ i j k ] ... + * [ a, b, c ] ... + * [ x, y, z ] ... + * [ i, j, k ] ... * ... * * @return Matrix as String * TODO: Work formatting for different digit sizes */ - public String toString() { + public String toString() { String str = ""; - - for(int i = 0; i < this.data.length; i++) { - str += "[ "; - for(int j = 0; j < this.data[0].length; j++) { - str += data[i][j]; - str += " "; + for(int i = 0; i < getRows(); i++){ + str +="[ "; + for(int j = 0; j < getColumns(); j++){ + str += getElement(i, j); + if(j != getColumns()-1) + str += ", "; + else + str += " "; } str += "]"; str += "\n"; } - return str; } } From 1ff536fe0ccc23b5bf40978151ae44e1a1651987 Mon Sep 17 00:00:00 2001 From: MarcoQQ <1007284166@qq.com> Date: Mon, 4 Dec 2017 12:35:06 +0800 Subject: [PATCH 3/4] Update Matrix.java --- Data Structures/Matrix/Matrix.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index 90de4452c975..0d1c60db111e 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -26,6 +26,7 @@ public static void main(String[] args) { //check for reference issues System.out.println("m2 -->\n" + m2); data2[1][1] = 101; + //m2 = new Matrix(data2); System.out.println("m2 -->\n" + m2); //test equals From a34b39b6cc7fbc91f356378760caa32978b128a4 Mon Sep 17 00:00:00 2001 From: MarcoQQ <1007284166@qq.com> Date: Mon, 4 Dec 2017 12:36:58 +0800 Subject: [PATCH 4/4] Update Matrix.java --- Data Structures/Matrix/Matrix.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index 0d1c60db111e..9ea2368430e2 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -196,7 +196,7 @@ public Matrix multiply(Matrix other) throws RuntimeException { * @return boolean */ public boolean equals(Matrix other){ - if(other == null || this == null)return false; + if(other == null)return false; if(other.getColumns() == getColumns() && other.getRows() == getRows()){ for(int i = 0; i < getRows(); i++) for(int j = 0; j < getColumns(); j++){