【程序员面试攻略】一书中的第六章代码:
import java.util.Hashtable;
import java.lang.StringBuffer;
public class Ch6 {
/*
* find the first non-repeated character in one string
* Input: one string
* Output: the first non-repeated character
*/
public Character firstNonRepeated(String str){
int length = str.length();
Character ch;
Integer intgr = null;
Hashtable<Character, Integer> charHash = new Hashtable<Character, Integer>();
Integer numOne = new Integer(1);
Integer numMoreThanOne = new Integer(2);
//get all the character from the string
for(int i = 0; i < length; i++){
ch = new Character(str.charAt(i));
intgr = (Integer) charHash.get(ch);
if(intgr == null)
charHash.put(ch, numOne);
else
charHash.put(ch, numMoreThanOne);
}
//check for the first non-rep char from the hashtable
for (int i = 0; i < length; i++){
ch = new Character(str.charAt(i));
intgr = (Integer) charHash.get(ch);
if(intgr == numOne)
return ch;
}
return null;
}
/*
* remove all the chars from a string
* input: string str, and the string needs to be removed
* output: new string without the chars
*/
public String removeChars(String str, String remove){
StringBuffer sb = new StringBuffer();
for(char c : str.toCharArray()){
if(remove.indexOf(c) == -1)
sb.append(c);
}
return sb.toString();
}
public String removeChar(String str, String remove){
char[] s = str.toCharArray();
char[] r = remove.toCharArray();
boolean[] flags = new boolean[128];
int len = s.length;
int src, dst;
for(src = 0; src < r.length; ++src){
flags[r[src]] = true;
}
src = 0;
dst = 0;
while(src < len){
//for(src = 0, dst = 0; src < len; ++src){
if( !flags[s[src]]){
s[dst++] = s[src];
}
++src;
}
return new String(s,0,dst);
}
/*
* revert a given string.
* input: a string: Do or do not, there is no try.
* output: a reverted string: try. no is there not, do or Do
* the idea is to get each words in order, and output them reversely.
* we can do it by StringBuffer or Stack.
*/
public String revertString(String str){
StringBuffer sb = new StringBuffer();
String[] sa = str.split(" ");
for(int i = sa.length -1; i >= 0; --i ){
sb.append(sa[i].toString() + " ");
}
return sb.toString();
}
/*
* Another way to do reverseString:
* The idea is to get the whole string reversely,
* and then reverse each word in order.
* eg.: in search of algorithmic elegance--> ecnagele cimhtiraglafo hcraes ni
* and then: elegance algorithmic of search in
*/
public char[] revertSentence(char[] str){
int start = 0, end = 0, len;
len = str.length;
//reverse the whole string, will get: .yrt on si ereht ,ton od ro oD
reverseStr(str, start, len-1);
//get each word, and reverse it
while(end < len){
if(str[end] != ' '){
//get the new start for the next word in the string
start = end;
while(end < len && str[end] != ' ')
end++;
//back up to the end of the word
end--;
reverseStr(str, start, end);
}
end++; //skip the empty space
}
return str;
}
private void reverseStr(char[] str, int start, int end){
char temp;
while(start < end){
temp = str[start];
str[start] = str[end];
str[end] = temp;
++start;
--end;
}
}
/*
* convert a string to an int
*/
public int strToInt(String str){
int num = 0, len = str.length();
boolean isNegative = false;
int start = 0;
if(str.charAt(0)== '-'){
isNegative = true;
start = 1;
}
for(int i = start; i < len; ++i){
int temp = str.charAt(i);
num *= 10; //two key steps: Horner rules
num += (temp-'0');
}
if(isNegative)
num *= (-1);
return num;
}
/*
* convert an int to a string
*/
public static final int MAX_DIGITS = 10;
public String intToStr(int num){
StringBuffer sb = new StringBuffer();
boolean isNegative = false;
char[] temp = new char[MAX_DIGITS + 1];
int i = 0;
if(num < 0){
isNegative = true;
num *= -1;
}
do{
temp[i++] = (char)((num % 10) + '0');
num /= 10;
}while( num != 0);
if(isNegative)
sb.append('-');
while( i > 0)
sb.append(temp[--i]); //good!
return sb.toString();
}
}