public class Solution {
public String reverseWords(String s) {
String result[] = s.trim().split(" +");
String reverseResult[] = new String[result.length];
String reverse = "";
if(result.length>0){
for(int i = 0;i<result.length;i++){
reverseResult[i] = result[result.length-1-i].trim();
reverse += reverseResult[i].trim()+" ";
}
}
return reverse.trim();
}
}
2.Evaluate Reverse Polish Notation
public class Solution {
public int evalRPN(String[] tokens) {
List<String> numList = new ArrayList<String>();
for(int i = 0;i<tokens.length;i++){
if(tokens[i].equals("+")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))+Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("-")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))-Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("*")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))*Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("/")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))/Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else{
numList.add(tokens[i]);
} }
return Integer.valueOf(numList.get(0)); } }
3.Max Points on a Line(这个题真是很蛋疼啊,开始时真没想到还会包括出现重复点的情况,导致后来写的有点晕了=.=)
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) { List<Integer> numlist = new ArrayList<Integer>();
HashMap<String, Integer> map;
HashMap<String, Integer> mapPoint;
Integer max = 0;
if (points.length == 1) {
max = 1;
}
for (int i = 0; i < points.length; i++) {
map = new HashMap<String, Integer>();
mapPoint = new HashMap<String, Integer>();
for (int j = i + 1; j < points.length; j++) {
String kString = "";
if ((points[i].x - points[j].x) != 0) {
if (points[i].y - points[j].y == 0) {
kString = String.valueOf(0);
} else {
float k = (float) (points[i].y - points[j].y)
/ (float) (points[i].x - points[j].x);
kString = String.valueOf(k);
}
if (map.containsKey(kString)) {
int num = map.get(kString) + 1;
map.remove(kString);
map.put(kString, num);
} else {
map.put(kString, 2);
}
} else if ((points[i].y - points[j].y) != 0
&& (points[i].x - points[j].x) == 0) {
kString = "no";
if (map.containsKey(kString)) {
int num = map.get(kString) + 1;
map.remove(kString);
map.put(kString, num);
} else {
map.put(kString, 2);
}
} else {
kString = "re";
if (mapPoint.containsKey(kString)) {
int num = mapPoint.get(kString) + 1;
mapPoint.remove(kString);
mapPoint.put(kString, num);
} else {
mapPoint.put(kString, 2);
}
} }
if (!map.isEmpty()) {
for (String key : map.keySet()) {
if (mapPoint.containsKey("re")
&& map.get(key) + mapPoint.get("re")
- 1 > max){
max = map.get(key) + mapPoint.get("re")
- 1;
} else if (map.get(key) > max) {
max = map.get(key);
} }
}
else {
if (mapPoint.containsKey("re")
&& mapPoint.get("re") > max) {
max = mapPoint.get("re");
}
}
} return max; }
}