First Round of Google Phone screen interview backup

qeustion 1: Write a program that reads in a text file, and only outputs the unique lines:

(can’t use unix uniq command)


Input:

A

B

A

C


Output:

A

B

C



public void readFile(File file) {
if(file == null)
return;
BufferReader reader = null;
FilerReader fileReader = null;
try{
fileReader = new FileReader(file);
reader = new BufferReader(fileReader);
String line= reader.nextLine();
Set<String> hasSet = new HashSet<String>();
while(line != null) {
if(!hasSet.contains(line)){
   hasSet.put(line);
   System.out.println(line);
}
line = reader.nextLine();
}
}
finally{
if(reader.isOpen())
reader.close();
if(fileReader.isOpen())
fileReader.close();

}

}


----------------------------------------

Question 2: s.l(1 2 3 4) = 4s.l.(1 4 5 6 9) = 3s.l.( 1 3 5 7 9) = 1s.l.(1 2 10 11 12 13)



int straightLen(int[] cards) {
if(cards == null)
return 0;
int length = 1;
int longest = 0;
ing next = Integer.MaxValue;
for(int i = 0; i < cards.length; i++){
if(next == cards[i]) {
length++;
}
else{
length = 1;
}
if(longest < length){
longest = length;
}
next = cards[i] +1;
}

return longest;
}





你可能感兴趣的:(First Round of Google Phone screen interview backup)