From 059c50d1a0133bc02e2bc12273116906db770d05 Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 15:46:56 -0500 Subject: [PATCH 01/34] Create file1.java --- basic/file1.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 basic/file1.java diff --git a/basic/file1.java b/basic/file1.java new file mode 100644 index 0000000..a5d9152 --- /dev/null +++ b/basic/file1.java @@ -0,0 +1,2 @@ +//This file is written for file reading and wirting under different circumstances +//Since Java From 368104e947d10f636e79eb4adef5aa10540cc91d Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:12:08 -0500 Subject: [PATCH 02/34] Update and rename file1.java to file_instruction.java --- basic/file1.java | 2 -- basic/file_instruction.java | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) delete mode 100644 basic/file1.java create mode 100644 basic/file_instruction.java diff --git a/basic/file1.java b/basic/file1.java deleted file mode 100644 index a5d9152..0000000 --- a/basic/file1.java +++ /dev/null @@ -1,2 +0,0 @@ -//This file is written for file reading and wirting under different circumstances -//Since Java diff --git a/basic/file_instruction.java b/basic/file_instruction.java new file mode 100644 index 0000000..48c0f96 --- /dev/null +++ b/basic/file_instruction.java @@ -0,0 +1,41 @@ +//This file is written for file reading and wirting under different circumstances +//Since Java always uses Reader and Writer abstract class and their inheritance class, read, write, flush and close methods +//need to be override. +//If we are trying to treat with text file + +//use FileReader for .txt file +FileReader fr = new FileReader("targetpath/targetfile.txt"); +int next = 0; +while((next = fr.read())!=-1) //also can use read(char[] next, int off, int length) +{ + System.out.println(next); +} + +//Actually, all FileReader class is from the InputStreamReader. To imporve efficiency, BufferedReader class has the readLine() method +BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("targetpath/targetfile.txt"))); +String next = null; +while(next = (br.readLine())! = null) +{ + System.out.println(next); +} + +//Use Writer class for .txt writing +FileWriter fw = new FileWriter("targetpath/targetfile.txt"); +String next = "hello world!"; +fw.write(next,0,next.length()); +fw.flush(); + +//Use OutputStreamWriter +OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("targetpath/targetfile.txt")); +osw.write(next,0,next.length()); +osw.flush(); + +//Use PrintWriter, what is the difference? +PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("targetpath/targetfile.txt")),true); +pw.println(next); + +//Use BufferedWriter + + + +//multi-thread comparing the time-consuming From 1483e7fe98150b8755485f042b5fa4edb1ace6ad Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:24:34 -0500 Subject: [PATCH 03/34] Create file_example.java --- basic/file_example.java | 250 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 basic/file_example.java diff --git a/basic/file_example.java b/basic/file_example.java new file mode 100644 index 0000000..2052335 --- /dev/null +++ b/basic/file_example.java @@ -0,0 +1,250 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.RandomAccessFile; +import java.io.Reader; +public class ReadFromFile { +/** +* Read file by byte, for .txt, sound and image files +* @param fileName targetfile name +*/ +public static void readFileByBytes(String fileName){ +File file = new File(fileName); +InputStream in = null; +try { +System.out.println("Read file by byte"); +// One byte at each time +in = new FileInputStream(file); +int tempbyte; +while((tempbyte=in.read()) != -1){ + System.out.write(tempbyte); +} +in.close(); +} catch (IOException e) { +e.printStackTrace(); +return; +} +try { +System.out.println("以字节为单位读取文件内容,一次读多个字节:"); +//一次读多个字节 +byte[] tempbytes = new byte[100]; +int byteread = 0; +in = new FileInputStream(fileName); +ReadFromFile.showAvailableBytes(in); +//读入多个字节到字节数组中,byteread为一次读入的字节数 +while ((byteread = in.read(tempbytes)) != -1){ +System.out.write(tempbytes, 0, byteread); +} +} catch (Exception e1) { +e1.printStackTrace(); +} finally { +if (in != null){ +try { +in.close(); +} catch (IOException e1) { +} +} +} +} +/** +* 以字符为单位读取文件,常用于读文本,数字等类型的文件 +* @param fileName 文件名 +*/ +public static void readFileByChars(String fileName){ +File file = new File(fileName); +Reader reader = null; +try { +System.out.println("以字符为单位读取文件内容,一次读一个字节:"); +// 一次读一个字符 +reader = new InputStreamReader(new FileInputStream(file)); +int tempchar; +while ((tempchar = reader.read()) != -1){ +//对于windows下,rn这两个字符在一起时,表示一个换行。 +//但如果这两个字符分开显示时,会换两次行。 +//因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。 +if (((char)tempchar) != 'r'){ +System.out.print((char)tempchar); +} +} +reader.close(); +} catch (Exception e) { +e.printStackTrace(); +} +try { +System.out.println("以字符为单位读取文件内容,一次读多个字节:"); +//一次读多个字符 +char[] tempchars = new char[30]; +int charread = 0; +reader = new InputStreamReader(new FileInputStream(fileName)); +//读入多个字符到字符数组中,charread为一次读取字符数 +while ((charread = reader.read(tempchars))!=-1){ +//同样屏蔽掉r不显示 +if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] != 'r')){ +System.out.print(tempchars); +}else{ +for (int i=0; i 4) ? 4 : 0; +//将读文件的开始位置移到beginIndex位置。 +randomFile.seek(beginIndex); +byte[] bytes = new byte[10]; +int byteread = 0; +//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 +//将一次读取的字节数赋给byteread +while ((byteread = randomFile.read(bytes)) != -1){ +System.out.write(bytes, 0, byteread); +} +} catch (IOException e){ +e.printStackTrace(); +} finally { +if (randomFile != null){ +try { +randomFile.close(); +} catch (IOException e1) { +} +} +} +} +/** +* 显示输入流中还剩的字节数 +* @param in +*/ +private static void showAvailableBytes(InputStream in){ +try { +System.out.println("当前字节输入流中的字节数为:" + in.available()); +} catch (IOException e) { +e.printStackTrace(); +} +} +public static void main(String[] args) { +String fileName = "C:/temp/newTemp.txt"; +ReadFromFile.readFileByBytes(fileName); +ReadFromFile.readFileByChars(fileName); +ReadFromFile.readFileByLines(fileName); +ReadFromFile.readFileByRandomAccess(fileName); +} +} +二、将内容追加到文件尾部 +import java.io.FileWriter; +import java.io.IOException; +import java.io.RandomAccessFile; +/** +* 将内容追加到文件尾部 +*/ +public class AppendToFile { +/** +* A方法追加文件:使用RandomAccessFile +* @param fileName 文件名 +* @param content 追加的内容 +*/ +public static void appendMethodA(String fileName, + +String content){ +try { +// 打开一个随机访问文件流,按读写方式 +RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); +// 文件长度,字节数 +long fileLength = randomFile.length(); +//将写文件指针移到文件尾。 +randomFile.seek(fileLength); +randomFile.writeBytes(content); +randomFile.close(); +} catch (IOException e){ +e.printStackTrace(); +} +} +/** +* B方法追加文件:使用FileWriter +* @param fileName +* @param content +*/ +public static void appendMethodB(String fileName, String content){ +try { +//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 +FileWriter writer = new FileWriter(fileName, true); +writer.write(content); +writer.close(); +} catch (IOException e) { +e.printStackTrace(); +} +} +public static void main(String[] args) { +String fileName = "C:/temp/newTemp.txt"; +String content = "new append!"; +//按方法A追加文件 +AppendToFile.appendMethodA(fileName, content); +AppendToFile.appendMethodA(fileName, "append end. n"); +//显示文件内容 +ReadFromFile.readFileByLines(fileName); +//按方法B追加文件 +AppendToFile.appendMethodB(fileName, content); +AppendToFile.appendMethodB(fileName, "append end. n"); +//显示文件内容 +ReadFromFile.readFileByLines(fileName); +} +} From f64ad8cea9ac3c15638ea3fd240174153a68e016 Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:47:29 -0500 Subject: [PATCH 04/34] Update file_example.java --- basic/file_example.java | 137 ++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 69 deletions(-) diff --git a/basic/file_example.java b/basic/file_example.java index 2052335..0ab487c 100644 --- a/basic/file_example.java +++ b/basic/file_example.java @@ -9,63 +9,63 @@ import java.io.Reader; public class ReadFromFile { /** -* Read file by byte, for .txt, sound and image files +* Read file by byte, for sound and image files * @param fileName targetfile name */ public static void readFileByBytes(String fileName){ File file = new File(fileName); InputStream in = null; + try { -System.out.println("Read file by byte"); -// One byte at each time -in = new FileInputStream(file); -int tempbyte; -while((tempbyte=in.read()) != -1){ - System.out.write(tempbyte); -} -in.close(); + System.out.println("Read file by byte"); + // One byte at each time + in = new FileInputStream(file); + int tempbyte; + while((tempbyte=in.read()) != -1){ + System.out.write(tempbyte); + } + in.close(); } catch (IOException e) { -e.printStackTrace(); -return; + e.printStackTrace(); + return; } try { -System.out.println("以字节为单位读取文件内容,一次读多个字节:"); -//一次读多个字节 -byte[] tempbytes = new byte[100]; -int byteread = 0; -in = new FileInputStream(fileName); -ReadFromFile.showAvailableBytes(in); -//读入多个字节到字节数组中,byteread为一次读入的字节数 -while ((byteread = in.read(tempbytes)) != -1){ -System.out.write(tempbytes, 0, byteread); -} + System.out.println("Read file by byte, multiple bytes one time"); + //multiple bytes one time + byte[] tempbytes = new byte[100]; + int byteread = 0; + in = new FileInputStream(fileName); + ReadFromFile.showAvailableBytes(in); + //byteread = length + while ((byteread = in.read(tempbytes)) != -1){ + System.out.write(tempbytes, 0, byteread); + } } catch (Exception e1) { -e1.printStackTrace(); + e1.printStackTrace(); } finally { -if (in != null){ -try { -in.close(); -} catch (IOException e1) { + if (in != null){ + try { + in.close(); + } catch (IOException e1) { } } } } + /** -* 以字符为单位读取文件,常用于读文本,数字等类型的文件 -* @param fileName 文件名 +* Read file by char, use for text and number files +* @param fileName */ public static void readFileByChars(String fileName){ File file = new File(fileName); Reader reader = null; try { -System.out.println("以字符为单位读取文件内容,一次读一个字节:"); -// 一次读一个字符 +System.out.println("Read file by char, one char each time"); reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1){ -//对于windows下,rn这两个字符在一起时,表示一个换行。 -//但如果这两个字符分开显示时,会换两次行。 -//因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。 +//because under windows, rn mean new line, when these two chars split, there will be two new lines. +//delete r can deduce useless blank lines if (((char)tempchar) != 'r'){ System.out.print((char)tempchar); } @@ -75,14 +75,11 @@ public static void readFileByChars(String fileName){ e.printStackTrace(); } try { -System.out.println("以字符为单位读取文件内容,一次读多个字节:"); -//一次读多个字符 +System.out.println("Read file by chars, multiple chars each time"); char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); -//读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars))!=-1){ -//同样屏蔽掉r不显示 if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] != 'r')){ System.out.print(tempchars); }else{ @@ -106,21 +103,21 @@ public static void readFileByChars(String fileName){ } } } + /** -* 以行为单位读取文件,常用于读面向行的格式化文件 -* @param fileName 文件名 +* Read file by line +* @param fileName */ public static void readFileByLines(String fileName){ File file = new File(fileName); BufferedReader reader = null; try { -System.out.println("以行为单位读取文件内容,一次读一整行:"); +System.out.println("Read file by line: "); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; -//一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null){ -//显示行号 +//line number System.out.println("line " + line + ": " + tempString); line++; } @@ -137,25 +134,25 @@ public static void readFileByLines(String fileName){ } } /** -* 随机读取文件内容 -* @param fileName 文件名 +* Random read file content +* @param fileName */ public static void readFileByRandomAccess(String fileName){ RandomAccessFile randomFile = null; try { -System.out.println("随机读取一段文件内容:"); -// 打开一个随机访问文件流,按只读方式 +System.out.println("Random read file "); +// open a file using read-only randomFile = new RandomAccessFile(fileName, "r"); -// 文件长度,字节数 +// file length long fileLength = randomFile.length(); -// 读文件的起始位置 +// Start pos int beginIndex = (fileLength > 4) ? 4 : 0; -//将读文件的开始位置移到beginIndex位置。 +//move to beginIndex pos randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; -//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 -//将一次读取的字节数赋给byteread +//each time at most 10 bytes +//length = readbyte while ((byteread = randomFile.read(bytes)) != -1){ System.out.write(bytes, 0, byteread); } @@ -171,46 +168,48 @@ public static void readFileByRandomAccess(String fileName){ } } /** -* 显示输入流中还剩的字节数 +* display inputstream remaining byte number * @param in */ private static void showAvailableBytes(InputStream in){ try { -System.out.println("当前字节输入流中的字节数为:" + in.available()); +System.out.println("current inputstream byte number is " + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { -String fileName = "C:/temp/newTemp.txt"; +String fileName = "target.txt"; ReadFromFile.readFileByBytes(fileName); ReadFromFile.readFileByChars(fileName); ReadFromFile.readFileByLines(fileName); ReadFromFile.readFileByRandomAccess(fileName); } } -二、将内容追加到文件尾部 + + +//add contents to the end of a file import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; /** -* 将内容追加到文件尾部 +* */ public class AppendToFile { /** -* A方法追加文件:使用RandomAccessFile -* @param fileName 文件名 -* @param content 追加的内容 +* RandomAccessFile +* @param fileName +* @param content */ public static void appendMethodA(String fileName, String content){ try { -// 打开一个随机访问文件流,按读写方式 +// random access a file using rw RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); -// 文件长度,字节数 +// byte number long fileLength = randomFile.length(); -//将写文件指针移到文件尾。 +//pointer move to the end of the file randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); @@ -219,13 +218,13 @@ public static void appendMethodA(String fileName, } } /** -* B方法追加文件:使用FileWriter +* FileWriter * @param fileName * @param content */ public static void appendMethodB(String fileName, String content){ try { -//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 +//open a writer method, use 'true' means to add contents to the file FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); @@ -234,17 +233,17 @@ public static void appendMethodB(String fileName, String content){ } } public static void main(String[] args) { -String fileName = "C:/temp/newTemp.txt"; +String fileName = "target.txt"; String content = "new append!"; -//按方法A追加文件 +// AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. n"); -//显示文件内容 +// ReadFromFile.readFileByLines(fileName); -//按方法B追加文件 +// AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. n"); -//显示文件内容 +// ReadFromFile.readFileByLines(fileName); } } From 42b8c573f59c415d0b3164df12627cb3f7e2be4e Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:48:44 -0500 Subject: [PATCH 05/34] Rename basic/file_example.java to basic/file/file_example.java --- basic/{ => file}/file_example.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename basic/{ => file}/file_example.java (100%) diff --git a/basic/file_example.java b/basic/file/file_example.java similarity index 100% rename from basic/file_example.java rename to basic/file/file_example.java From 83dad20b1fbb509c769cde7471343cd95a6bdfa4 Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:49:01 -0500 Subject: [PATCH 06/34] Rename basic/file_instruction.java to basic/file/file_instruction.java --- basic/{ => file}/file_instruction.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename basic/{ => file}/file_instruction.java (100%) diff --git a/basic/file_instruction.java b/basic/file/file_instruction.java similarity index 100% rename from basic/file_instruction.java rename to basic/file/file_instruction.java From 6c5b362a62d4680d8bcc4d6d03b31e7957dd5825 Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 16:53:46 -0500 Subject: [PATCH 07/34] Create folderAndFile --- basic/file/folderAndFile | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 basic/file/folderAndFile diff --git a/basic/file/folderAndFile b/basic/file/folderAndFile new file mode 100644 index 0000000..57398c9 --- /dev/null +++ b/basic/file/folderAndFile @@ -0,0 +1,39 @@ +// this file contains some methods for creating and deleting a folder and a file + +//1. create a folder +public void createDir(String path)...{ + File dir=new File(path); + if(!dir.exists()) + dir.mkdir(); +} + +//2. create a new file +public void createFile(String path,String filename) throws IOException...{ + File file=new File(path+"/"+filename); + if(!file.exists()) + file.createNewFile(); +} + +//3. delete a file +public void delFile(String path,String filename)...{ + File file=new File(path+"/"+filename); + if(file.exists()&&file.isFile()) + file.delete(); +} + + +//4. delete a folder +public void delDir(String path)...{ + File dir=new File(path); + if(dir.exists())...{ + File[] tmp=dir.listFiles(); + for(int i=0;i Date: Mon, 17 Feb 2014 19:14:06 -0500 Subject: [PATCH 08/34] Create collections.java --- interview/ctci/basic/collections.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 interview/ctci/basic/collections.java diff --git a/interview/ctci/basic/collections.java b/interview/ctci/basic/collections.java new file mode 100644 index 0000000..0f803c0 --- /dev/null +++ b/interview/ctci/basic/collections.java @@ -0,0 +1 @@ +//java has a lot of useful collection including the list, queue, map, etc... From 835f7c23c72fd2e7e910dbc4a44e019d6adb020b Mon Sep 17 00:00:00 2001 From: Zihan Date: Mon, 17 Feb 2014 19:15:15 -0500 Subject: [PATCH 09/34] try to create all instance and usage for jcolle. java collection --- interview/ctci/basic/collections.java | 1 + 1 file changed, 1 insertion(+) diff --git a/interview/ctci/basic/collections.java b/interview/ctci/basic/collections.java index 0f803c0..14782c1 100644 --- a/interview/ctci/basic/collections.java +++ b/interview/ctci/basic/collections.java @@ -1 +1,2 @@ //java has a lot of useful collection including the list, queue, map, etc... +//list From 9297afcdc70d128658ca273fe2887dbb692b368e Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Tue, 18 Feb 2014 21:21:13 -0500 Subject: [PATCH 10/34] Create list.java --- interview/ctci/basic/list.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 interview/ctci/basic/list.java diff --git a/interview/ctci/basic/list.java b/interview/ctci/basic/list.java new file mode 100644 index 0000000..a91cda0 --- /dev/null +++ b/interview/ctci/basic/list.java @@ -0,0 +1,3 @@ +/* + * this file has all useful implenmentations of list abstract class +*/ From 7f1b830389680b7073263e1b4343cb2647116764 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Tue, 18 Feb 2014 23:58:42 -0500 Subject: [PATCH 11/34] Update list.java --- interview/ctci/basic/list.java | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/interview/ctci/basic/list.java b/interview/ctci/basic/list.java index a91cda0..ba422a7 100644 --- a/interview/ctci/basic/list.java +++ b/interview/ctci/basic/list.java @@ -1,3 +1,58 @@ /* * this file has all useful implenmentations of list abstract class */ + +//ArrayList +import java.util.ArrayList; +import java.util.Iterator; + +public class List { + public void exampleList(){ + ArrayList list1 =new ArrayList (); + ArrayList sublist = new ArrayList (); + list1.add("test1"); + sublist.add("test2"); + list1.addAll(sublist); + + System.out.println("The super list: " + list1); + System.out.println("The sub list: " + sublist); + + System.out.println("--------------------------------------------------------------------"); + System.out.println("Whether the super list contains test1: " + list1.contains("test1")); + System.out.println("Whether the super list contains sublist: " + list1.containsAll(sublist)); + System.out.println("Whether the super list equals sublist: " + list1.equals(sublist)); + System.out.println("Hash code for super and sub list: "+ list1.hashCode() +" "+ sublist.hashCode()); + sublist.clear(); + System.out.println("Clear all elements in sublist: "+sublist.isEmpty()); + + Iterator it1 = list1.iterator(); + System.out.println("The first element in iterator: " + it1.next()); + + ArrayList sublist2 = new ArrayList (); + sublist2.add("test3"); + list1.addAll(sublist2); + System.out.println("--------------------------------------------------------------------"); + System.out.println("After add another sublist, the new list1 is " + list1 + " size: "+ list1.size()); + list1.remove("test2"); + list1.removeAll(sublist2); + System.out.println("Remove sublist and sublist2: " + list1.toString()); + + //System.out.println(list1.toArray()); + + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + List newList = new List(); + newList.exampleList(); + } +} + + +//LinkedList + + + From 8f0458affec24b5691b99b79beda701efa94a8ef Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Tue, 18 Feb 2014 23:58:54 -0500 Subject: [PATCH 12/34] Update collections.java --- interview/ctci/basic/collections.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/interview/ctci/basic/collections.java b/interview/ctci/basic/collections.java index 14782c1..d950fee 100644 --- a/interview/ctci/basic/collections.java +++ b/interview/ctci/basic/collections.java @@ -1,2 +1,5 @@ //java has a lot of useful collection including the list, queue, map, etc... -//list +/* abstract collection: AbstractList, AbstractMap, AbstractQueue, AbstractSequentialList, AbstractSet +interface: Collection, List, Queue, Comparator, ListIterator, RandomAccess, Deque, Map, Set, Enumeration, Map.Entry, + SortedMap, EventListener, NavigableMap, SortedSet, Formattable, NavigableSet, Iterator, Observer + From 4775f87eb8f7edbcc02866975f0f97dfcdae5edd Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Wed, 19 Feb 2014 21:48:46 -0500 Subject: [PATCH 13/34] Update list.java --- interview/ctci/basic/list.java | 40 +++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/interview/ctci/basic/list.java b/interview/ctci/basic/list.java index ba422a7..c073212 100644 --- a/interview/ctci/basic/list.java +++ b/interview/ctci/basic/list.java @@ -3,6 +3,8 @@ */ //ArrayList +package com.dreamx.collection; + import java.util.ArrayList; import java.util.Iterator; @@ -41,6 +43,39 @@ public void exampleList(){ } + public void advancedExample(){ + //ArrayList has three main constructors shown: + //ArrayList(), ArrayList(Collection c), ArrayList(int Capacity) + long heapSize = Runtime.getRuntime().freeMemory(); + System.out.println("Size : "+ heapSize); + + ArrayList list1 = new ArrayList (); + //ArrayList list2 = new ArrayList (); + list1.add("test"); + + //ensureCapacity(int) method does influence the freeMemory remain. + System.out.println("Size before take larger memory space: "+ heapSize); + + list1.ensureCapacity(100000); + heapSize = Runtime.getRuntime().freeMemory(); + System.out.println("Size after take larger memory space: "+ heapSize); + + //trimToSize() can reduce the space and set the arraylist to the actual size, but cannot really influence the memory space + list1.trimToSize(); + heapSize = Runtime.getRuntime().freeMemory(); + System.out.println("Size after trim memory space: "+ heapSize); + } + + public void ArrayListToArray(){ + ArrayList list1 = new ArrayList (); + list1.add("1"); + list1.add("second"); + String array[] = new String[list1.size()]; + list1.toArray(array); + for(String s:array) + System.out.println(s); + } + /** * @param args */ @@ -48,9 +83,12 @@ public static void main(String[] args) { // TODO Auto-generated method stub List newList = new List(); newList.exampleList(); + newList.advancedExample(); + newList.ArrayListToArray(); + } -} +} //LinkedList From 028630683ccd26b3d987688dd194c7dfb5d565da Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 03:14:04 -0500 Subject: [PATCH 14/34] Create queuex.java --- interview/ctci/basic/queuex.java | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 interview/ctci/basic/queuex.java diff --git a/interview/ctci/basic/queuex.java b/interview/ctci/basic/queuex.java new file mode 100644 index 0000000..e47e690 --- /dev/null +++ b/interview/ctci/basic/queuex.java @@ -0,0 +1,90 @@ +package com.dreamx.collection; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; + +public class Queueimp { + + public void queueexample(){ + Queue q1 = new LinkedList(); + q1.add("test1"); + q1.add(1); + System.out.println(q1.peek()); + System.out.println(q1.peek()); + q1.remove(); + System.out.println(q1.peek()); + } + + public LinkedList bfsexample(Node root){ + //use bfs to get the Node whose no is larger than 3 but less than 5 + Queue queue = new LinkedList(); + LinkedList nodes = new LinkedList(); + queue.add(root); + while(!queue.isEmpty()){ + Node current = queue.remove(); + current.visited = true; + if(current.left!=null){ + queue.add(current.left); + } + if (current.right!=null){ + queue.add(current.right); + } + if(current.no>=3&¤t.no<=5){ + nodes.add(current); + current.printNode(); + } + } + return nodes; + } + + public Node setNode(Node root){ + //construct a simple tree structure + Node h21 = new Node(2,"height2-1"); + Node h22 = new Node(2,"height2-2"); + root.left = h21; + root.right = h22; + Node h31 = new Node(3,"height3-1"); + Node h32 = new Node(3,"height3-2"); + Node h33 = new Node(0,"height3-3"); + h21.left = h31; + h22.left = h32; + h22.right = h33; + return root; + } + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Queueimp newqueue = new Queueimp(); + //newqueue.queueexample(); + Node root = new Node(1,"root"); + newqueue.setNode(root); + LinkedList nodes = newqueue.bfsexample(root); + } +} + +class Node{ + int no = 0; + boolean visited = false; + String data = null; + public Node left = null; + public Node right = null; + + public Node(int size, String data){ + this.no = size; + this.data = data; + } + + public void visit(){ + this.visited = true; + } + + public void printNode(){ + System.out.println(this.visited + ", "+this.data); + } + +} + From 98ec3f189d34d64e0f47b9a8e95ce71a58885d11 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 13:58:20 -0500 Subject: [PATCH 15/34] Create stackex.java --- interview/ctci/basic/stackex.java | 107 ++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 interview/ctci/basic/stackex.java diff --git a/interview/ctci/basic/stackex.java b/interview/ctci/basic/stackex.java new file mode 100644 index 0000000..df50228 --- /dev/null +++ b/interview/ctci/basic/stackex.java @@ -0,0 +1,107 @@ +package com.dreamx.collection; + +import java.util.*; + +public class Stackimp { + + public static Stack stackexample(){ + Stack s1 = new Stack(); + s1.add("test1"); + s1.add("test2"); + System.out.println(s1); + s1.pop(); + System.out.println(s1); + return s1; + } + + public void example2(){ + Stack s = new Stack(); + s.add("test1"); + s.add(1); + System.out.println(s); + s.add(1, 2);//stack can choose the element adding position + System.out.println(s); + s.push("test2"); + System.out.println(s); + s.pop(); + System.out.println(s); + System.out.println(s.peek()); + s.pop(); + System.out.println(s.peek()); + } + + public HashMap dfsexample(Node root){ + HashMap map = new HashMap(); + Stack stack = new Stack (); + stack.push(root); + while(!stack.isEmpty()){ + Node current = stack.pop(); + current.printNode(); + current.visited = true; + if(current.left != null){ + stack.push(current.left); + } + if (current.right!= null){ + stack.push(current.right); + } + } + return map; + } + + public Node setNode(Node root){ + //construct a simple tree structure + Node h21 = new Node(2,"height2-1"); + Node h22 = new Node(2,"height2-2"); + root.left = h21; + root.right = h22; + Node h31 = new Node(3,"height3-1"); + Node h32 = new Node(3,"height3-2"); + Node h33 = new Node(3,"height3-3"); + Node h41 = new Node(4,"height4-1"); + Node h51 = new Node(5,"height5-1"); + h21.left = h31; + h22.left = h32; + h22.right = h33; + h31.left = h41; + h41.left = h51; + return root; + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Stackimp newstack = new Stackimp(); + //Stack s2 = Stackimp.stackexample(); + //newstack.example2(); + Node root = new Node(1,"root"); + newstack.setNode(root); + root.left.left.left.left.printNode(); + HashMap map = newstack.dfsexample(root); + } + +} +/*already defined in Queueimp.java +class Node{ + int no = 0; + boolean visited = false; + String data = null; + public Node left = null; + public Node right = null; + + public Node(int size, String data){ + this.no = size; + this.data = data; + } + + public void visit(){ + this.visited = true; + } + + public void printNode(){ + System.out.println(this.visited + ", "+this.data); + } + +} +*/ From a606d585c826493489adb0dd5f8bd272fc73ab2a Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 13:58:42 -0500 Subject: [PATCH 16/34] Rename stackex.java to Stackimp.java --- interview/ctci/basic/{stackex.java => Stackimp.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename interview/ctci/basic/{stackex.java => Stackimp.java} (100%) diff --git a/interview/ctci/basic/stackex.java b/interview/ctci/basic/Stackimp.java similarity index 100% rename from interview/ctci/basic/stackex.java rename to interview/ctci/basic/Stackimp.java From 1055e4c81f97469eadef330fa3324229c61adadb Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 13:58:58 -0500 Subject: [PATCH 17/34] Rename list.java to List.java --- interview/ctci/basic/{list.java => List.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename interview/ctci/basic/{list.java => List.java} (100%) diff --git a/interview/ctci/basic/list.java b/interview/ctci/basic/List.java similarity index 100% rename from interview/ctci/basic/list.java rename to interview/ctci/basic/List.java From 1d9d45ed9d556b8a9690fce0f7ee442ca934096e Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 13:59:23 -0500 Subject: [PATCH 18/34] Rename queuex.java to Queueimp.java --- interview/ctci/basic/{queuex.java => Queueimp.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename interview/ctci/basic/{queuex.java => Queueimp.java} (100%) diff --git a/interview/ctci/basic/queuex.java b/interview/ctci/basic/Queueimp.java similarity index 100% rename from interview/ctci/basic/queuex.java rename to interview/ctci/basic/Queueimp.java From 42a317c0d3b2862147a755b94ba9d1d660ba9f1e Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 15:42:23 -0500 Subject: [PATCH 19/34] Create Mapimp.java --- interview/ctci/basic/Mapimp.java | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 interview/ctci/basic/Mapimp.java diff --git a/interview/ctci/basic/Mapimp.java b/interview/ctci/basic/Mapimp.java new file mode 100644 index 0000000..fa1a0aa --- /dev/null +++ b/interview/ctci/basic/Mapimp.java @@ -0,0 +1,66 @@ +package com.dreamx.collection; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; + +public class Mapimp { + + public void treemapex(){ + //THE KEY OF TREEMAP is sorted! + Map map = new TreeMap (); + map.put("a","a"); + map.put("w","b"); + map.put("s","c"); + map.put("c","b"); + map.put("wds","d"); + map.put("aaa","f"); + + Iterator it = map.values().iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + System.out.println("The values are not sorted while keys are sorted."); + System.out.println(map.keySet().toString()); + System.out.println(map.values().toString()); + Iterator it2 = map.keySet().iterator(); + while(it2.hasNext()){ + System.out.println(it2.next()); + } + } + + public void hashmapex(){ + //THE KEY OF TREEMAP is sorted! + HashMap map = new HashMap (); + map.put("a","a"); + map.put("w","n"); + map.put("s","b"); + map.put("c","c"); + map.put("wds","b"); + map.put("aaa","d"); + + Iterator it = map.values().iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + System.out.println("The values and keys are not sorted."); + System.out.println(map.keySet().toString()); + System.out.println(map.values().toString()); + Iterator it2 = map.keySet().iterator(); + while(it2.hasNext()){ + System.out.println(it2.next()); + } + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Mapimp imp = new Mapimp(); + imp.treemapex(); + imp.hashmapex(); + } + +} From 1990d081d44ecf0a4828caa2f115f7c26ff062b9 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Thu, 20 Feb 2014 15:45:44 -0500 Subject: [PATCH 20/34] Update Queueimp.java --- interview/ctci/basic/Queueimp.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interview/ctci/basic/Queueimp.java b/interview/ctci/basic/Queueimp.java index e47e690..fc8ceda 100644 --- a/interview/ctci/basic/Queueimp.java +++ b/interview/ctci/basic/Queueimp.java @@ -10,7 +10,7 @@ public class Queueimp { public void queueexample(){ Queue q1 = new LinkedList(); q1.add("test1"); - q1.add(1); + q1.add(12); System.out.println(q1.peek()); System.out.println(q1.peek()); q1.remove(); From 7dad3abb0fc29048a2dbd2249323d3f7b0135f05 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Fri, 21 Feb 2014 01:46:57 -0500 Subject: [PATCH 21/34] Create computeString.java --- interview/question/computeString.java | 163 ++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 interview/question/computeString.java diff --git a/interview/question/computeString.java b/interview/question/computeString.java new file mode 100644 index 0000000..d590807 --- /dev/null +++ b/interview/question/computeString.java @@ -0,0 +1,163 @@ +/** + * Given two string representations of binary numbers, return a string + * representation of their sum. For example: + * + * "101" + * + "11" => badd("101", "11") -> "1000" + * ------ + * "1000" + * + * "11" + * + "10" => badd("11", "10") -> "101" + * ------ + * "101" + * + * "01" + * + "10" => badd("01", "10") -> "011" + * ------ (leading '0' is optional) + * "011" + * + */ + +public class computeString{ +public String badd(String x, String y) { + + Stack stack1 = new Stack (); + Stack stack2 = new Stack (); + Stack stack3 = new Stack (); + String z = null; + + for(int i=0; i list = new LinkedList (); + while (!stack3.empty()){ + System.out.println(stack3.pop()); + //list.add(stack3.pop()); + } + z= list.toString(); + return z; + } + +/* int getInt(String x){ + result =0; + for (int i = 0; i Date: Fri, 21 Feb 2014 11:30:47 -0500 Subject: [PATCH 22/34] Update computeString.java --- interview/question/computeString.java | 36 ++++++++++----------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/interview/question/computeString.java b/interview/question/computeString.java index d590807..9919dc3 100644 --- a/interview/question/computeString.java +++ b/interview/question/computeString.java @@ -18,18 +18,16 @@ * "011" * */ - +import java.util.Stack; public class computeString{ public String badd(String x, String y) { - - Stack stack1 = new Stack (); + Stack stack1 = new Stack (); Stack stack2 = new Stack (); Stack stack3 = new Stack (); - String z = null; + String z = ""; for(int i=0; i list = new LinkedList (); + if(remain == 1){ + stack3.push(remain); + } + else{stack3.push(0);} + while (!stack3.empty()){ - System.out.println(stack3.pop()); - //list.add(stack3.pop()); + z = z+stack3.pop(); } - z= list.toString(); return z; } - -/* int getInt(String x){ - result =0; - for (int i = 0; i Date: Sat, 22 Feb 2014 01:15:10 -0500 Subject: [PATCH 23/34] Create PossiblePath.java --- interview/question/PossiblePath.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 interview/question/PossiblePath.java diff --git a/interview/question/PossiblePath.java b/interview/question/PossiblePath.java new file mode 100644 index 0000000..03dd1e4 --- /dev/null +++ b/interview/question/PossiblePath.java @@ -0,0 +1,5 @@ +/* +Print all possible paths from top left to bottom right of a mXn matrix +The problem is to print all the possible paths from top left to bottom right of a mXn matrix with the constraints that +from each cell you can either move only to right or down. +*/ From 38b4427c8f6671e907c2f291d6b1ba70b5f804f9 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sat, 22 Feb 2014 01:16:33 -0500 Subject: [PATCH 24/34] Update PossiblePath.java --- interview/question/PossiblePath.java | 1 + 1 file changed, 1 insertion(+) diff --git a/interview/question/PossiblePath.java b/interview/question/PossiblePath.java index 03dd1e4..56e8518 100644 --- a/interview/question/PossiblePath.java +++ b/interview/question/PossiblePath.java @@ -3,3 +3,4 @@ The problem is to print all the possible paths from top left to bottom right of a mXn matrix with the constraints that from each cell you can either move only to right or down. */ +//http://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/ From fee168ac6cc132c5119b1ed6f5fa704b2a6d9aa7 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sat, 22 Feb 2014 01:17:53 -0500 Subject: [PATCH 25/34] Create CountDistinctPair.java --- interview/question/CountDistinctPair.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 interview/question/CountDistinctPair.java diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java new file mode 100644 index 0000000..603e953 --- /dev/null +++ b/interview/question/CountDistinctPair.java @@ -0,0 +1,16 @@ +/* +Count all distinct pairs with difference equal to k +Given an integer array and a positive integer k, count all distinct pairs with difference equal to k. + +Examples: + +Input: arr[] = {1, 5, 3, 4, 2}, k = 3 +Output: 2 +There are 2 pairs with difference 3, the pairs are {1, 4} and {5, 2} + +Input: arr[] = {8, 12, 16, 4, 0, 20}, k = 4 +Output: 5 +There are 5 pairs with difference 4, the pairs are {0, 4}, {4, 8}, +{8, 12}, {12, 16} and {16, 20} +*/ +//http://www.geeksforgeeks.org/count-pairs-difference-equal-k/ From afecf44933157b4342d6723d8341c76daa447fb1 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sat, 22 Feb 2014 14:37:09 -0500 Subject: [PATCH 26/34] Update CountDistinctPair.java --- interview/question/CountDistinctPair.java | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java index 603e953..1176903 100644 --- a/interview/question/CountDistinctPair.java +++ b/interview/question/CountDistinctPair.java @@ -14,3 +14,77 @@ {8, 12}, {12, 16} and {16, 20} */ //http://www.geeksforgeeks.org/count-pairs-difference-equal-k/ + +public class MatrixCount { + + public void check (int i, int j, int m, int n, int nexti, int nextj){ + if(i==m-1){ + for(int index=0; index=n-1 && j=m-1 && i Date: Sat, 22 Feb 2014 14:37:46 -0500 Subject: [PATCH 27/34] Update CountDistinctPair.java --- interview/question/CountDistinctPair.java | 74 ----------------------- 1 file changed, 74 deletions(-) diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java index 1176903..603e953 100644 --- a/interview/question/CountDistinctPair.java +++ b/interview/question/CountDistinctPair.java @@ -14,77 +14,3 @@ {8, 12}, {12, 16} and {16, 20} */ //http://www.geeksforgeeks.org/count-pairs-difference-equal-k/ - -public class MatrixCount { - - public void check (int i, int j, int m, int n, int nexti, int nextj){ - if(i==m-1){ - for(int index=0; index=n-1 && j=m-1 && i Date: Sat, 22 Feb 2014 14:38:00 -0500 Subject: [PATCH 28/34] Update PossiblePath.java --- interview/question/PossiblePath.java | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/interview/question/PossiblePath.java b/interview/question/PossiblePath.java index 56e8518..531cd80 100644 --- a/interview/question/PossiblePath.java +++ b/interview/question/PossiblePath.java @@ -4,3 +4,78 @@ from each cell you can either move only to right or down. */ //http://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/ + + +public class MatrixCount { + + public void check (int i, int j, int m, int n, int nexti, int nextj){ + if(i==m-1){ + for(int index=0; index=n-1 && j=m-1 && i Date: Sat, 22 Feb 2014 16:08:35 -0500 Subject: [PATCH 29/34] Update CountDistinctPair.java --- interview/question/CountDistinctPair.java | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java index 603e953..b238742 100644 --- a/interview/question/CountDistinctPair.java +++ b/interview/question/CountDistinctPair.java @@ -14,3 +14,38 @@ {8, 12}, {12, 16} and {16, 20} */ //http://www.geeksforgeeks.org/count-pairs-difference-equal-k/ + +import java.util.ArrayList; +import java.util.List; +public class CountDistinctPair { + + public void count(int[] arr, int k){ + List list = new ArrayList(); + int[] arr2 = arr; + for(int i:arr){ + for(int j:arr2){ + if(i>j){ + if(i-j==k){ + list.add(i); + list.add(j); + } + } + } + } + for(int z=0;z Date: Sat, 22 Feb 2014 16:12:42 -0500 Subject: [PATCH 30/34] Update CountDistinctPair.java --- interview/question/CountDistinctPair.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java index b238742..dbe3809 100644 --- a/interview/question/CountDistinctPair.java +++ b/interview/question/CountDistinctPair.java @@ -32,6 +32,7 @@ public void count(int[] arr, int k){ } } } + System.out.println("Output Num: "+list.size()/2); for(int z=0;z Date: Sat, 22 Feb 2014 16:16:57 -0500 Subject: [PATCH 31/34] Update CountDistinctPair.java --- interview/question/CountDistinctPair.java | 1 + 1 file changed, 1 insertion(+) diff --git a/interview/question/CountDistinctPair.java b/interview/question/CountDistinctPair.java index dbe3809..a0a25b4 100644 --- a/interview/question/CountDistinctPair.java +++ b/interview/question/CountDistinctPair.java @@ -14,6 +14,7 @@ {8, 12}, {12, 16} and {16, 20} */ //http://www.geeksforgeeks.org/count-pairs-difference-equal-k/ +//The method2 in that page is more efficient than this one import java.util.ArrayList; import java.util.List; From d8a5d9d31aa86c8c93571bc354c666fd4fdd42f6 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sat, 22 Feb 2014 22:22:40 -0500 Subject: [PATCH 32/34] Create FindNumPair.java --- interview/question/FindNumPair.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 interview/question/FindNumPair.java diff --git a/interview/question/FindNumPair.java b/interview/question/FindNumPair.java new file mode 100644 index 0000000..79e2456 --- /dev/null +++ b/interview/question/FindNumPair.java @@ -0,0 +1,16 @@ +/*Find number of pairs such that x^y > y^x +Given two arrays X[] and Y[] of positive integers, find number of pairs such that x^y > y^x where x is an element from X[] and y is an element from Y[]. + +Examples: + + Input: X[] = {2, 1, 6}, Y = {1, 5} + Output: 3 + // There are total 3 pairs where pow(x, y) is greater than pow(y, x) + // Pairs are (2, 1), (2, 5) and (6, 1) + + + Input: X[] = {10, 19, 18}, Y[] = {11, 15, 9}; + Output: 2 + // There are total 2 pairs where pow(x, y) is greater than pow(y, x) + // Pairs are (10, 11) and (10, 15) +*/ From f9a09c38e1089527f8a1febfe592441694aeba64 Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sun, 23 Feb 2014 01:02:00 -0500 Subject: [PATCH 33/34] Update FindNumPair.java --- interview/question/FindNumPair.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/interview/question/FindNumPair.java b/interview/question/FindNumPair.java index 79e2456..f2ed69e 100644 --- a/interview/question/FindNumPair.java +++ b/interview/question/FindNumPair.java @@ -14,3 +14,31 @@ // There are total 2 pairs where pow(x, y) is greater than pow(y, x) // Pairs are (10, 11) and (10, 15) */ + +//http://www.geeksforgeeks.org/find-number-pairs-xy-yx/ + +/*mathmatical analysis: +x^y = y^x +take natural log on both sides. +=> yln(x)=xln(y) +=> ln(x)/x = ln(y)/y + +now differentiate ln(x)/x wrt x and compare it with zero. + +=> d/dx(ln(x)/x) +=> 1/x^2 - ln(x)/x^2 +=> (1-ln(x))/x^2 + +for all real x, x^2 >=0 +therefore : (1-ln(x)) >=0 for x<=e (~ 2.71 ) + +1-ln(x) < 0 for x>e +So ln(x)/x is increasing in range <=e, i.e. for integers, its increasing for 1,2 +and decreasing else where. + +(1) x<3, y<3: x need > y +(2) x<3 (x =2): y > 4 because 2^3 < 3^2, 2^4 = 4^2, 2^5>5^2 +(3) x>=3, y>=3: x need > y +(4) x>=3, y <3 (y=1): all + (y=2): x need =3 +*/ From c3321df9ce69dd81af50cd54075187d545e162dd Mon Sep 17 00:00:00 2001 From: Zihan Wang Date: Sun, 23 Feb 2014 17:04:27 -0500 Subject: [PATCH 34/34] Create MoveAllZero.java --- interview/question/MoveAllZero.java | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 interview/question/MoveAllZero.java diff --git a/interview/question/MoveAllZero.java b/interview/question/MoveAllZero.java new file mode 100644 index 0000000..e7d971d --- /dev/null +++ b/interview/question/MoveAllZero.java @@ -0,0 +1,54 @@ +/*Given an array of random numbers, Push all the zero’s of a given array to the end of the array. +For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to +{1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time +complexity is O(n) and extra space is O(1). +*/ + +public class MoveAllZero { + //This will use O(n^2) time and O(1) space + public int[] moveall (int[] arr){ + int[] re = arr; + int i,j; + for (i=0;i