Skip to content

Commit d92fcd1

Browse files
committed
tree2
1 parent 72f914d commit d92fcd1

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

facebook/SortWeight.java

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.HashMap;
6-
import java.util.HashSet;
76
import java.util.Iterator;
87
import java.util.LinkedList;
98

@@ -14,10 +13,10 @@ public static void main(String[] args) {
1413
String input3 = "pizza 500 hotdog 2.0";
1514
String input4 = "pizza 500 2.0";
1615

17-
LinkedList<String> list1 = sortWeight(input1);
18-
LinkedList<String> list2 = sortWeight(input2);
19-
LinkedList<String> list3 = sortWeight(input3);
20-
LinkedList<String> list4 = sortWeight(input4);
16+
LinkedList<String> list1 = sortWeight2(input1);
17+
LinkedList<String> list2 = sortWeight2(input2);
18+
LinkedList<String> list3 = sortWeight2(input3);
19+
LinkedList<String> list4 = sortWeight2(input4);
2120

2221
System.out.println(list1.toString());
2322
System.out.println(list2.toString());
@@ -85,6 +84,86 @@ public static LinkedList<String> sortWeight(String input) {
8584
return ret;
8685
}
8786

87+
public static class Pair implements Comparable<Pair> {
88+
String food;
89+
float weight;
90+
91+
Pair (String food, float weight) {
92+
this.food = food;
93+
this.weight = weight;
94+
}
95+
96+
// 注意,我们用o来减当前的重量,就能得到降序的排列
97+
public int compareTo(Pair o) {
98+
if (o.weight - this.weight < 0) {
99+
return -1;
100+
}
101+
102+
return 1;
103+
}
104+
}
105+
106+
/*
107+
* 使用自定义结构体 Pair而不是hashmap来记录食物-重量对。可以简化算法。
108+
* 但这时就需要自定义一个compareTo方法
109+
* */
110+
public static LinkedList<String> sortWeight2(String input) {
111+
LinkedList<String> ret = new LinkedList<String>();
112+
if (input == null) {
113+
return ret;
114+
}
115+
116+
String[] strs = input.split(" ");
117+
118+
float defautWeight = 5;
119+
120+
// 当weight = -1,表示这一组食物-重量链还未完成
121+
String food = null;
122+
float weight = 0;
123+
124+
// 使用ArrayList来记录食物-重量对
125+
ArrayList<Pair> list = new ArrayList<Pair>();
126+
127+
// Go through the string.
128+
for (String s: strs) {
129+
// 上一次的food-weight对已经结束
130+
if (weight != -1) {
131+
food = s;
132+
weight = -1;
133+
} else {
134+
float tmp = stringToNumber(s);
135+
// This is a float, so just add a food to the list.
136+
if (tmp != -1) {
137+
weight = tmp;
138+
list.add(new Pair(food, weight));
139+
} else {
140+
// This is not a float, means that there should be
141+
// a new food.
142+
list.add(new Pair(food, defautWeight));
143+
144+
// 开始新一轮的食物-重量链
145+
food = s;
146+
weight = -1;
147+
}
148+
}
149+
}
150+
151+
//System.out.println(map.toString());
152+
153+
if (weight == -1) {
154+
list.add(new Pair(food, defautWeight));
155+
}
156+
157+
Collections.sort(list);
158+
Iterator<Pair> iter = list.iterator();
159+
while (iter.hasNext()) {
160+
ret.add(iter.next().food);
161+
}
162+
163+
return ret;
164+
}
165+
166+
88167
public static void addFoodToMap(HashMap<Float, ArrayList<String>> map, String food,
89168
float weight) {
90169
// 把上一次的食物-重量终结

0 commit comments

Comments
 (0)