- Notifications
You must be signed in to change notification settings - Fork117
Merge latest updates#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
singlemancombat wants to merge66 commits intorpj911:masterChoose a base branch fromsinglemancombat:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes from1 commit
Commits
Show all changes
66 commits Select commitHold shift + click to select a range
bd11ddf
KthSort
yuzhangcmu841e167
add the demos
yuzhangcmudabfdb7
min cut
yuzhangcmu762dcad
Create README.md
yuzhangcmuce3384e
Update README.md
yuzhangcmu078baa7
modify the directors
yuzhangcmuab453fc
move the folder.
yuzhangcmuaf5edeb
modify the tree Demo
yuzhangcmu52407ba
The LCA problem.
yuzhangcmu7c9819f
add some comments.
yuzhangcmubb3eb2b
Arrange the code.
yuzhangcmufa314c2
merge Sort
yuzhangcmu37c4105
merge
yuzhangcmub0e4e32
Five Chessman
yuzhangcmu441dba7
FiveChessman
yuzhangcmu2eec824
notes
yuzhangcmu1551e7f
the largest Common subtree
yuzhangcmu61e79b2
the largest common
yuzhangcmuff798ae
largestCommon
yuzhangcmu718698d
TestPermutation
yuzhangcmu7d60ff4
isCompleteBinaryTree
yuzhangcmuc73a8a7
NextPermutation
yuzhangcmu8c9f0e8
permutation sequence
yuzhangcmub8c8f5f
Fibonacci
yuzhangcmu92f4168
add new file
f928b91
add code.
b6817a6
Recover Binary Search Tree
yuzhangcmua758230
isValidBST
yuzhangcmuc04d009
in order
yuzhangcmu3292319
tree
yuzhangcmu8f28a48
tree
yuzhangcmu2d0cf85
minDepth
yuzhangcmudc15508
min
yuzhangcmude48d99
facebook
yuzhangcmu72f914d
Unique BSTs
yuzhangcmud92fcd1
tree2
yuzhangcmu632666f
merge Sort
yuzhangcmud1c27bd
merge
yuzhangcmuec77e43
remove duplicate
yuzhangcmu127db30
remove duplicates2
yuzhangcmu63dd8d0
array
yuzhangcmu23f07e3
search
yuzhangcmubd02708
maxSubArray
yuzhangcmu88a4d50
the merge
yuzhangcmu0ccff33
strstr
yuzhangcmu096b743
list
yuzhangcmud955d59
divide
yuzhangcmu218d63f
pow
yuzhangcmu4f99b2d
pow
yuzhangcmu7564463
list
yuzhangcmu1ddacc5
sort
yuzhangcmud5573fc
reorder list
yuzhangcmuc144ac6
random
yuzhangcmu2a37fec
reverse2
yuzhangcmu150f920
tree
yuzhangcmu82143ad
name
yuzhangcmuf08140a
lett
yuzhangcmu42796c9
list
yuzhangcmud6d1308
list
yuzhangcmu0060c07
rotate
yuzhangcmu72e5ddf
list
yuzhangcmucfdbfe8
remove
yuzhangcmu732d38a
list
yuzhangcmu53f429f
tree
yuzhangcmu1693ac2
candy
yuzhangcmu7a80b46
combine
yuzhangcmuFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
facebook
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitde48d998f6e210e340fdf6d7eecca41cd2ac5e9d
There are no files selected for viewing
115 changes: 115 additions & 0 deletionsfacebook/SortWeight.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package Algorithms.facebook; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
public class SortWeight { | ||
public static void main(String[] args) { | ||
String input1 = "crab hotdog 9.0 chicken 9.2 pig 9.2"; | ||
String input2 = "pizza 1 hotdog 2.0"; | ||
String input3 = "pizza 500 hotdog 2.0"; | ||
String input4 = "pizza 500 2.0"; | ||
LinkedList<String> list1 = sortWeight(input1); | ||
LinkedList<String> list2 = sortWeight(input2); | ||
LinkedList<String> list3 = sortWeight(input3); | ||
LinkedList<String> list4 = sortWeight(input4); | ||
System.out.println(list1.toString()); | ||
System.out.println(list2.toString()); | ||
System.out.println(list3.toString()); | ||
System.out.println(list4.toString()); | ||
} | ||
public static LinkedList<String> sortWeight(String input) { | ||
LinkedList<String> ret = new LinkedList<String>(); | ||
if (input == null) { | ||
return ret; | ||
} | ||
String[] strs = input.split(" "); | ||
float defautWeight = 5; | ||
// 当weight = -1,表示这一组食物-重量链还未完成 | ||
String food = null; | ||
float weight = 0; | ||
// 记录某重量下所有的食物 | ||
HashMap<Float, ArrayList<String>> map = new HashMap<Float, ArrayList<String>>(); | ||
// Go through the string. | ||
for (String s: strs) { | ||
// 上一次的food-weight对已经结束 | ||
if (weight != -1) { | ||
food = s; | ||
weight = -1; | ||
} else { | ||
float tmp = stringToNumber(s); | ||
// This is a float, so just add a food to the list. | ||
if (tmp != -1) { | ||
weight = tmp; | ||
addFoodToMap(map, food, weight); | ||
} else { | ||
// This is not a float, means that there should be | ||
// a new food. | ||
addFoodToMap(map, food, defautWeight); | ||
// 开始新一轮的食物-重量链 | ||
food = s; | ||
weight = -1; | ||
} | ||
} | ||
} | ||
//System.out.println(map.toString()); | ||
if (weight == -1) { | ||
addFoodToMap(map, food, defautWeight); | ||
} | ||
ArrayList<Float> array = new ArrayList<Float>(map.keySet()); | ||
Collections.sort(array); | ||
for (Float w: array) { | ||
ArrayList<String> foods = map.get(w); | ||
for (String element: foods) { | ||
ret.addFirst(element); | ||
} | ||
} | ||
return ret; | ||
} | ||
public static void addFoodToMap(HashMap<Float, ArrayList<String>> map, String food, | ||
float weight) { | ||
// 把上一次的食物-重量终结 | ||
ArrayList<String> list = map.get(weight); | ||
if (list != null) { | ||
// 在相应的重量下添加食物 | ||
list.add(food); | ||
} else { | ||
// 新建一个重量链 | ||
ArrayList<String> listNew = new ArrayList<String>(); | ||
listNew.add(food); | ||
map.put(weight, listNew); | ||
} | ||
} | ||
// when it is not a float, return -1; | ||
public static float stringToNumber(String cur) { | ||
float result = -1; | ||
try { | ||
result = Float.parseFloat(cur); | ||
} catch (NumberFormatException e) { | ||
result = -1; | ||
} | ||
return result; | ||
} | ||
} |
2 changes: 1 addition & 1 deletiontree/MinDepth.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.