Skip to content

Commit

Permalink
Merge pull request #33 from anxinJ/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
Mr-ChangK authored Apr 11, 2017
2 parents a295c28 + 5312870 commit 88e5cda
Show file tree
Hide file tree
Showing 29 changed files with 1,496 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.coding.basic;
package com.coding.basic.stack;

import java.util.EmptyStackException;

Expand All @@ -13,12 +13,12 @@ public void push(Object o) {

public Object pop() {
checkBound();
return elementData.get(size() - 1);
return elementData.remove(size() - 1);
}

public Object peek() {
checkBound();
return elementData.remove(size() - 1);
return elementData.get(size() - 1);
}

public boolean isEmpty() {
Expand All @@ -34,4 +34,14 @@ private void checkBound() {
throw new EmptyStackException();
}
}

public String toString(){
StringBuilder sb = new StringBuilder();
for(int i=0;i<elementData.size();i++){
sb.append(elementData.get(i));
if(i < elementData.size() - 1)
sb.append(",");
}
return sb.toString();
}
}
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;
}


}
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);
}

}
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);
}

}

}
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;
}

}
Loading

0 comments on commit 88e5cda

Please sign in to comment.