最近闲来无事,温习了一下算法,还是别有风情。
1、逆波兰式
问题描述:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
思路:
从运算表达式来看,最好用一个栈来存放这些字符串,这样取出后,判断运算符,从而将栈中的两数字进行运算。
实现代码:
public int rp(String[] strs) {
Stack<String> stack = new Stack<String>();
String operators = "+-*/";
for (String str : strs) {
if (!operators.contains(str)) {
stack.push(str);
} else {
Integer a = Integer.valueOf(stack.pop());
Integer b = Integer.valueOf(stack.pop());
int index = operators.indexOf(str);
switch (index) {
case 0:
stack.push(String.valueOf(a + b));
break;
case 1:
stack.push(String.valueOf(b - a));
break;
case 2:
stack.push(String.valueOf(a * b));
break;
case 3:
stack.push(String.valueOf(b / a));
break;
}
}
}
return Integer.valueOf(stack.pop());
}
2、字梯
问题描述:
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
思路:
对初给的字符串,逐字符的替换后,进行比较。
实现代码:
public int getLength(String start, String end, Set<String> dict) {
if (dict.size() == 0) {
return 0;
}
LinkedList<String> wordQueue = new LinkedList<String>(); // 存放单词
LinkedList<Integer> distanceQueue = new LinkedList<Integer>();// 存放距离
wordQueue.add(start);
distanceQueue.add(1);
while (!wordQueue.isEmpty()) {
String currWord = wordQueue.pop();
int distance = distanceQueue.pop();
if (currWord.equals(end)) {
return distance;
}
for (int i = 0; i < currWord.length(); i++) {
char[] chs = currWord.toCharArray();
// 从a~z 逐个替换 看是否符合
for (char ch = 'a'; ch <= 'z'; ch++) {
chs[i] = ch;
String newWord = String.valueOf(chs);
// 新组成的单词是否和end相等
if (newWord.equals(end)) {
return distance + 1;
}
// 否则,接字
if (dict.contains(newWord)) {
wordQueue.push(newWord);
distanceQueue.push(distance + 1);
// 移除比较过的,否则会重复计算
dict.remove(newWord);
}
}
}
}
return 0;
}
3、最长回文串
问题描述:
输入一个字符串,返回最长回文串。比如,输入‘adasffsff',输出'ffsff'。
直接上代码:
// 得到最长回文
public String findLongestPalindrome(String str) {
if (str.isEmpty()) {
return null;
}
if (str.length() == 1) {
return str;
}
int longest = 0;
String longeststr = "";
for (int i = 0; i < str.length(); i++) {
String substr = getSubstr(str, i, i); // 这种得到的是类似于这种情况:_aba_
if (substr.length() > longest) {
longest = substr.length();
longeststr = substr;
}
substr = getSubstr(str, i, i + 1); // 这种得到的是类似于这种情况:_sffs_
if (substr.length() > longest) {
longest = substr.length();
longeststr = substr;
}
}
return longeststr;
}
// 回文子串
public String getSubstr(String str, int start, int end) {
while (start >= 0 && end < str.length()
&& str.charAt(start) == str.charAt(end)) {
// 扩大比较范围
start--;
end++;
}
return str.substring(++start, end);
}
路漫漫其修远兮,吾将上下而求索。
待续...