Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified GenerateCodeTable.class
Binary file not shown.
3 changes: 3 additions & 0 deletions GenerateCodeTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ public static String generateWordPressPage(File[] listOfFiles) {
public static String generateREADME(File[] listOfFiles) {
//Assemble output
String outputContent = "# Java Algorithm Problems\n\n" +
"### 前戏\n" +
"To host Java Solutions to algorithm problems from LintCode, LeetCode...etc.\n" +
"I Will try to revise the solutions once new problem or new testing case occurs.\n" +
"**Mid 2016** I realize that people may want to contribute to this repo, and make it better by contributing fixes, better solutions ... etc. Free free to send pull request. Once verified, I'm happy to merge in!\n" +
"CALM DOWN AND CODE ON! Fellows! \n\n" +
"### News\n" +
"2017年1月17日, 陪我征战多年的 2014 MackBookPro i7 3.xGHz 被一杯清水结束了生命,在这里深切缅怀悼念。这个Git Repo是小M陪我一字一句打出来的,有过蹉跎,也有过辉煌,陪我从Day1刷题一直刷到了Day1之中。直至今日,小M记录的代码还在给广大coder带来福利。为了延续小M无私奉献的精神,我将重新在这个repo活跃起来,重整已有的问题,也会尝试总结一些System Design方面的想法,将小M还没有能够达成的梦想实现。\n\n" +
"| Squence | Problem | Level | Language | Video Tutorial|\n" +
"|:-------:|:--------------|:------:|:---------:|:-------------:|\n";
int count = 0;
Expand Down
60 changes: 59 additions & 1 deletion Java/Convert Expression to Reverse Polish Notation.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,62 @@ public void postTraversal(ArrayList<String> rst, TreeNode node){
}
}

```
```

Here is another approach which is by Shunting-yard algorithm (https://en.wikipedia.org/wiki/Shunting-yard_algorithm)

```
public class Solution {
/**
* @param expression: A string array
* @return: The Reverse Polish notation of this expression
*/
public ArrayList<String> convertToRPN(String[] expression) {
// write your code here
ArrayList<String> res = new ArrayList<>();
if (expression == null || expression.length == 0) return res;
Stack<String> op = new Stack<>();
for (String s : expression) {
if (isNumber(s)) {
res.add(s);
} else if (isOperator(s)) {
while (!op.isEmpty() && !op.peek().equals("(") && getPriority(op.peek()) >= getPriority(s)) {
res.add(op.pop());
}
op.push(s);
} else if (s.equals("(")) {
op.push(s);
} else if (s.equals(")")) {
while (!op.isEmpty() && !op.peek().equals("(")) {
res.add(op.pop());
}
if (!op.isEmpty() && op.peek().equals("(")) {
op.pop();
} else {
return res;
}
}
}
while (!op.isEmpty()) {
res.add(op.pop());
}
return res;
}

private boolean isNumber(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}
private boolean isOperator(String s) {
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
}
private int getPriority(String s) {
if (s.equals("*") || s.equals("/")) return 2;
return 1;
}
}
```
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Java Algorithm Problems

### 前戏
To host Java Solutions to algorithm problems from LintCode, LeetCode...etc.
I Will try to revise the solutions once new problem or new testing case occurs.
**Mid 2016** I realize that people may want to contribute to this repo, and make it better by contributing fixes, better solutions ... etc. Free free to send pull request. Once verified, I'm happy to merge in!
CALM DOWN AND CODE ON! Fellows!

### News
2017年1月17日, 陪我征战多年的 2014 MackBookPro i7 3.xGHz 被一杯清水结束了生命,在这里深切缅怀悼念。这个Git Repo是小M陪我一字一句打出来的,有过蹉跎,也有过辉煌,陪我从Day1刷题一直刷到了Day1之中。直至今日,小M记录的代码还在给广大coder带来福利。为了延续小M无私奉献的精神,我将重新在这个repo活跃起来,重整已有的问题,也会尝试总结一些System Design方面的想法,将小M还没有能够达成的梦想实现。

| Squence | Problem | Level | Language | Video Tutorial|
|:-------:|:--------------|:------:|:---------:|:-------------:|
|0|[2 Sum II - Input array is sorted.java](https://github.com/shawnfan/LintCode/blob/master/Java/2%20Sum%20II%20-%20Input%20array%20is%20sorted.java)|Medium|Java||
Expand Down