-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from anxinJ/master
Master
- Loading branch information
Showing
29 changed files
with
1,496 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
99 changes: 99 additions & 0 deletions
99
group23/1028361767/data-structure/src/com/coding/basic/stack/StackUtil.java
This file contains 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,99 @@ | ||
package com.coding.basic.stack; | ||
|
||
public class StackUtil { | ||
|
||
|
||
/** | ||
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 | ||
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
*/ | ||
public static void reverse(Stack s) { | ||
Stack tmp = new Stack(); | ||
Stack tmp1 = new Stack(); | ||
while(!s.isEmpty()) | ||
tmp.push(s.pop()); | ||
while(!tmp.isEmpty()) | ||
tmp1.push(tmp.pop()); | ||
while(!tmp1.isEmpty()) | ||
s.push(tmp1.pop()); | ||
} | ||
|
||
/** | ||
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* | ||
* @param o | ||
*/ | ||
public static void remove(Stack s,Object o) { | ||
Stack tmp = new Stack(); | ||
boolean found = false; | ||
while(!s.isEmpty() && !found){ | ||
Object obj = s.pop(); | ||
if(obj == o){ | ||
found = true; | ||
}else{ | ||
tmp.push(obj); | ||
} | ||
} | ||
while(!tmp.isEmpty()) | ||
s.push(tmp.pop()); | ||
} | ||
|
||
/** | ||
* 从栈顶取得len个元素, 原来的栈中元素保持不变 | ||
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* @param len | ||
* @return | ||
*/ | ||
public static Object[] getTop(Stack s,int len) { | ||
Object[] objs = new Object[len]; | ||
if(s.isEmpty() || len == 0) | ||
return objs; | ||
int i = 0; | ||
while(i < len && !s.isEmpty()){ | ||
objs[i++] = s.pop(); | ||
} | ||
for(i=objs.length-1;i>=0;i--) | ||
s.push(objs[i]); | ||
return objs; | ||
} | ||
/** | ||
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz | ||
* 使用堆栈检查字符串s中的括号是不是成对出现的。 | ||
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true | ||
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; | ||
* | ||
* ( 40 ) 41 [ 91 ] 93 {123 }125 | ||
* @param s | ||
* @return | ||
*/ | ||
public static boolean isValidPairs(String s){ | ||
if(s == null || s.length() == 0) | ||
return true; | ||
Stack stack = new Stack(); | ||
char[] chars = s.toCharArray(); | ||
for(int i=0;i<chars.length;i++){ | ||
char tmp = chars[i]; | ||
if(tmp == 40 || tmp == 91 || tmp == 123){ | ||
stack.push(tmp); | ||
continue; | ||
}else if(tmp == 41){ | ||
if(tmp-1 == (char)stack.peek()){ | ||
stack.pop(); | ||
}else{ | ||
return false; | ||
} | ||
}else if(tmp == 93 || tmp == 125){ | ||
if(tmp-2 == (char)stack.peek()){ | ||
stack.pop(); | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} | ||
System.out.println(stack.size()); | ||
if(!stack.isEmpty()) return false; | ||
return true; | ||
} | ||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
group23/1028361767/data-structure/src/com/coding/basic/stack/expr/InfixExpr.java
This file contains 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,101 @@ | ||
package com.coding.basic.stack.expr; | ||
|
||
import com.coding.basic.stack.Stack; | ||
|
||
public class InfixExpr { | ||
String expr = null; | ||
|
||
public InfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
if(expr == null || "".equals(expr)){ | ||
return 0; | ||
} | ||
Stack numStack = new Stack(); | ||
Stack opStack = new Stack(); | ||
String[] ss = expr.split(""); | ||
boolean needOper = false;// 需要运算,遇到* / 后置为true | ||
boolean foundOper = true;// 遇到运算符 | ||
String ops = "+-*/"; | ||
for (int i=0;i<ss.length;i++) { | ||
String s = ss[i]; | ||
if(ops.indexOf(s) != -1){ | ||
if(needOper){ | ||
doMulOrDivOp(numStack, (String)opStack.pop()); | ||
needOper = false; | ||
} | ||
opStack.push(s); | ||
foundOper = true; | ||
if("+-".indexOf(s) != -1){ | ||
|
||
}else{ | ||
needOper = true; | ||
} | ||
}else if(!foundOper){// 未找到运算符前,字符串一直追加 | ||
String tmp = ""; | ||
Object obj = numStack.pop(); | ||
if(obj != null){ | ||
tmp = (String)obj; | ||
} | ||
numStack.push(tmp + s); | ||
}else if("1234567890".indexOf(s) != -1){ | ||
numStack.push(s); | ||
foundOper = false; | ||
}else{ | ||
throw new RuntimeException("InfixExpr not support " + s + " !"); | ||
} | ||
if((i == ss.length - 1) && needOper){ | ||
doMulOrDivOp(numStack, (String)opStack.pop()); | ||
} | ||
} | ||
if(numStack.size() > 1){ | ||
doAddOrSubOps(numStack, opStack); | ||
} | ||
return new Float((String)numStack.pop()); | ||
} | ||
|
||
/** | ||
* 多个加减 | ||
* @param numStack | ||
* @param opStack | ||
*/ | ||
private void doAddOrSubOps(Stack numStack, Stack opStack) { | ||
Stack calStack = new Stack(); | ||
while(numStack.size() > 1){ | ||
calStack.push(numStack.pop()); | ||
calStack.push(opStack.pop()); | ||
} | ||
float num1 = new Float((String)numStack.pop()); | ||
float num2; | ||
while(calStack.size() > 0){ | ||
String op = (String)calStack.pop(); | ||
num2 = new Float((String)calStack.pop()); | ||
if("+".equals(op)){ | ||
num1 += num2; | ||
}else{ | ||
num1 -= num2; | ||
} | ||
} | ||
numStack.push(num1 + ""); | ||
} | ||
|
||
/** | ||
* 单个乘除 | ||
* @param numStack | ||
* @param op | ||
*/ | ||
private void doMulOrDivOp(Stack numStack, String op){ | ||
float num2 = new Float((String)numStack.pop()); | ||
float num1 = new Float((String)numStack.pop()); | ||
String result = null; | ||
if("*".equals(op)){ | ||
result = (num1 * num2) + ""; | ||
}else{ | ||
result = (num1 / num2) + ""; | ||
} | ||
numStack.push(result); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
group23/1028361767/data-structure/src/com/coding/basic/stack/expr/InfixExprTest.java
This file contains 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,48 @@ | ||
package com.coding.basic.stack.expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
public class InfixExprTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testEvaluate() { | ||
//InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); | ||
{ | ||
InfixExpr expr = new InfixExpr("2+3*4+5"); | ||
Assert.assertEquals(19.0, expr.evaluate(), 0.001f); | ||
} | ||
{ | ||
InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); | ||
Assert.assertEquals(100.0, expr.evaluate(), 0.001f); | ||
} | ||
|
||
{ | ||
InfixExpr expr = new InfixExpr("3*20/2"); | ||
Assert.assertEquals(30, expr.evaluate(), 0.001f); | ||
} | ||
|
||
{ | ||
InfixExpr expr = new InfixExpr("20/2*3"); | ||
Assert.assertEquals(30, expr.evaluate(), 0.001f); | ||
} | ||
|
||
{ | ||
InfixExpr expr = new InfixExpr("10-30+50"); | ||
Assert.assertEquals(30, expr.evaluate(), 0.001f); | ||
} | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
group23/1028361767/data-structure/src/com/coding/me/book/algorithms/BasicBinarySearch.java
This file contains 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,12 @@ | ||
package com.coding.me.book.algorithms; | ||
|
||
public class BasicBinarySearch { | ||
|
||
public int findIndex(int[] nums, int target){ | ||
if(nums.length == 0) return -1; | ||
|
||
int mid = nums.length / 2; | ||
return 0; | ||
} | ||
|
||
} |
Oops, something went wrong.