Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde48d99

Browse files
committed
facebook
1 parentdc15508 commitde48d99

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

‎facebook/SortWeight.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
packageAlgorithms.facebook;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Collections;
5+
importjava.util.HashMap;
6+
importjava.util.HashSet;
7+
importjava.util.Iterator;
8+
importjava.util.LinkedList;
9+
10+
publicclassSortWeight {
11+
publicstaticvoidmain(String[]args) {
12+
Stringinput1 ="crab hotdog 9.0 chicken 9.2 pig 9.2";
13+
Stringinput2 ="pizza 1 hotdog 2.0";
14+
Stringinput3 ="pizza 500 hotdog 2.0";
15+
Stringinput4 ="pizza 500 2.0";
16+
17+
LinkedList<String>list1 =sortWeight(input1);
18+
LinkedList<String>list2 =sortWeight(input2);
19+
LinkedList<String>list3 =sortWeight(input3);
20+
LinkedList<String>list4 =sortWeight(input4);
21+
22+
System.out.println(list1.toString());
23+
System.out.println(list2.toString());
24+
System.out.println(list3.toString());
25+
System.out.println(list4.toString());
26+
}
27+
28+
publicstaticLinkedList<String>sortWeight(Stringinput) {
29+
LinkedList<String>ret =newLinkedList<String>();
30+
if (input ==null) {
31+
returnret;
32+
}
33+
34+
String[]strs =input.split(" ");
35+
36+
floatdefautWeight =5;
37+
38+
// 当weight = -1,表示这一组食物-重量链还未完成
39+
Stringfood =null;
40+
floatweight =0;
41+
42+
// 记录某重量下所有的食物
43+
HashMap<Float,ArrayList<String>>map =newHashMap<Float,ArrayList<String>>();
44+
45+
// Go through the string.
46+
for (Strings:strs) {
47+
// 上一次的food-weight对已经结束
48+
if (weight != -1) {
49+
food =s;
50+
weight = -1;
51+
}else {
52+
floattmp =stringToNumber(s);
53+
// This is a float, so just add a food to the list.
54+
if (tmp != -1) {
55+
weight =tmp;
56+
addFoodToMap(map,food,weight);
57+
}else {
58+
// This is not a float, means that there should be
59+
// a new food.
60+
addFoodToMap(map,food,defautWeight);
61+
62+
// 开始新一轮的食物-重量链
63+
food =s;
64+
weight = -1;
65+
}
66+
}
67+
}
68+
69+
//System.out.println(map.toString());
70+
71+
if (weight == -1) {
72+
addFoodToMap(map,food,defautWeight);
73+
}
74+
75+
ArrayList<Float>array =newArrayList<Float>(map.keySet());
76+
Collections.sort(array);
77+
78+
for (Floatw:array) {
79+
ArrayList<String>foods =map.get(w);
80+
for (Stringelement:foods) {
81+
ret.addFirst(element);
82+
}
83+
}
84+
85+
returnret;
86+
}
87+
88+
publicstaticvoidaddFoodToMap(HashMap<Float,ArrayList<String>>map,Stringfood,
89+
floatweight) {
90+
// 把上一次的食物-重量终结
91+
ArrayList<String>list =map.get(weight);
92+
if (list !=null) {
93+
// 在相应的重量下添加食物
94+
list.add(food);
95+
}else {
96+
// 新建一个重量链
97+
ArrayList<String>listNew =newArrayList<String>();
98+
listNew.add(food);
99+
map.put(weight,listNew);
100+
}
101+
}
102+
103+
// when it is not a float, return -1;
104+
publicstaticfloatstringToNumber(Stringcur) {
105+
floatresult = -1;
106+
107+
try {
108+
result =Float.parseFloat(cur);
109+
}catch (NumberFormatExceptione) {
110+
result = -1;
111+
}
112+
113+
returnresult;
114+
}
115+
}

‎tree/MinDepth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public int minDepthRec(TreeNode root) {
4747
把 null 设置为返回一个最大值,这样的话,如果出现空子树,它不会影响最小值。但是如果左
4848
右均为空,则应返回1(即是仅仅为根节点)
4949
50-
而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。
50+
而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。
5151
* */
5252
publicintminDepthHelp(TreeNoderoot) {
5353
if (root ==null) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp