Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Last active December 14, 2015 12:39
Show Gist options
  • Save charlespunk/5087818 to your computer and use it in GitHub Desktop.
Save charlespunk/5087818 to your computer and use it in GitHub Desktop.
1. Make any given string a palindrome by adding the minimum number of characters.
http://isharemylearning.blogspot.com/2012/08/minimum-number-of-insertions-in-string.html
http://blog.csdn.net/atfield/article/details/1496132
http://blog.csdn.net/atfield/article/details/1496132
2. power of a number (hint: recursion)
3. Describe the file system in hadoop in big view
4. If you are writing a class in Java, "Student" for example; you are storing it in a hashtable; and you want the objects are identical to the names. what can you do?
http://javarevisited.blogspot.com/2011/02/how-to-write-equals-method-in-java.html
5. Yahoo : 找出最长0,1对数相同的子串
http://www.1point3acres.com/bbs/thread-12564-1-1.html
6. Yahoo : Find number of black regions in a plane
http://www.1point3acres.com/bbs/thread-14080-1-1.html
/*
adfbd
dbfda
=> d f d
*/
public static String makePalindromeAddLeastChar(String input){
char[] leftRight = input.toCharArray();
char[] rightLeft = new char[leftRight.length];
for(int i = 0; i < leftRight.length; i++) rightLeft[i] = leftRight[leftRight.length - 1 - i];
int[][] dpMatrix = new int[leftRight.length + 1][leftRight.length + 1];
for(int i = 0; i < dpMatrix.length; i++){
for(int j = 0; j < dpMatrix[0].lenght; j++){
dpMatrix[i][j] = -1;
}
}
int commonLength = LCS(dpMatrix.length - 1, dpMatrix[0].length - 1, leftRight, rightLeft, dpMatrix);
int backTrackRow = dpMatrix.length - 1;
int backTrackColumn = dpMatrix[0].length - 1;
}
public static int LCS(int row, int column, char[] first, char[] second, int[][] dpMatrix[][]){
if(row == 0 || column == 0) return dpMatrix[row][column] = 0;
if(dpMatrix[row][column] == -1){
if(first[row - 1] == second[row - 1]) dpMatrix[row][column] = LCS(row - 1, column - 1, leftRight, rightLeft, dpMatrix) + 1;
else dpMatrix[row][column] =
Math.max(LCS(row - 1, column, leftRight, rightLeft, dpMatrix), LCS(row, column - 1, leftRight, rightLeft, dpMatrix));
}
return dpMatrix[row][column];
}
//2. power of a number (hint: recursion)
public static double power(int input){
if(input > 0 ) return (double) powerRecursive(input);
else return 1d / powerRecursive(input * -1);
}
public static int powerRecursive(int input){
if(input == 0) return 1;
int half = input / 2;
int halfPower = powerRecursive(half);
if(input % 2 == 1) return ( halfPower * halfPower * 2);
else return halfPower * halfPower;
}
/*
4. If you are writing a class in Java, "Student" for example; you are storing it in a hashtable; and you want the objects are identical to
the names. what can you do?
*/
public class Student{
private String name;
public String getName(){
return this.name;
}
@override
public int hashCode(){
final int prime = 37;
int result = 1;
result = result * prime + (this.name == null? 0 : this.name.hashCode());
return result;
}
@override
public boolean equals(Object obj){
if(obj == this) return true;
if(obj == null || obj.getClass() != this.getClass()) return false;
Student thatStudent = (Student) obj;
return (this.name == thatStudent.getName() || (this.name != null && this.name.equals(thatStudent.getName())));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment