阿里编程逆波兰表达式有人ac吗

只有40%哪里有问题啊
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {

        ArrayList<Integer> inputs = new ArrayList<Integer>();
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        if(line != null && !line.isEmpty()) {
            int res = resolve(line.trim());
            System.out.println(String.valueOf(res));
        }
    }

    // write your code here
    public static int resolve(String expr) {
    	StringBuffer sb = new StringBuffer();
    	for(int i=0;i<expr.length();i++){
    		if(expr.charAt(i)==' '){
    			continue;
    		}else{
    			sb.append(expr.charAt(i));
    			//sb.append(" ");
    		}
    	}
    	String test = sb.toString();
    	//String[] sp=test.split(" ");
    	char[] chas = test.toCharArray();
    	Stack<Integer> stack = new Stack<Integer>();
    	for(int i=0;i<chas.length;i++){
    		if(chas[i]=='*'){
    			if(stack.size()<2){
    				return -1;
    			}
    			int m1=stack.pop();
    			int m2=stack.pop();
    			int m = m1*m2;
    			stack.push(m);
    		}else if(chas[i]=='+'){
    			if(stack.size()<2){
    				return -1;
    			}
    			int m1=stack.pop();
    			int m2=stack.pop();
    			int m = m1+m2;
    			stack.push(m);
    		}else if(chas[i]=='^'){
    			int x = stack.pop();
    			x=x+1;
    			stack.push(x);
    		}else{
    			String cs = chas[i]+"";
    			int value = Integer.valueOf(cs);
    			if(stack.size()==16){
    				return -2;
    			}
    			stack.push(value);
    		
    		}
    	}
       return stack.peek();
    }
}

#阿里巴巴#
全部评论
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; public class Main { private static String pattern = "^-?[0-9]+"; public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if (line != null && !line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { int[] stack = new int[16]; int top = -1; String[] datas = expr.split(" "); for (int i = 0; i < datas.length; i++) { if (Pattern.matches(pattern, datas[i])) { if (top == 15) { return -2; } top++; stack[top] = Integer.parseInt(datas[i]); } else { switch (datas[i]) { case "^": if (top == -1) { return -1; } stack[top]++; break; case "+": if (top < 1) { return -1; } int a = stack[top]; top--; stack[top] += a; break; case "*": if (top < 1) { return -1; } int b = stack[top]; top--; stack[top] *= b; break; } } } if (top < 0) { return -1; } return stack[top]; } } 我这个AC了,字符串匹配的没AC,求大神代码
点赞 回复 分享
发布于 2017-04-26 21:15
你这个代码把123当作1,2,3来处理了,123是一个整体
点赞 回复 分享
发布于 2017-04-26 21:18
import java.util.ArrayList; import java.util.Stack; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line != null && !line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { String[] array = expr.split("\\s+"); Stack stack = new Stack(); int count = 0; for (int i = 0; i < array.length; i++){ if (array[i].equals("+")){ count--; int temp1 = 0; if (count >= 0){ temp1 = (int)stack.pop(); } else { return -1; } count--; int temp2 = 0; if (count >= 0){ temp2 = (int)stack.pop(); }else { return -1; } count++; stack.push(temp1+temp2); }else if (array[i].equals("*")){ count--; int temp1 = 0; if (count >= 0){ temp1 = (int)stack.pop(); } else { return -1; } count--; int temp2 = 0; if (count >= 0){ temp2 = (int)stack.pop(); }else { return -1; } count++; stack.push(temp1*temp2); }else if (array[i].equals("^")){ count--; int temp = 0; if (count >= 0){ temp = (int)stack.pop(); } else { return -1; } temp += 1; count++; stack.push(temp); }else { int temp = Integer.parseInt(array[i]); count++; if (count > 16){ return -2; }else { stack.push(temp); } } } return (int)stack.pop(); } } 我这样写ac了
点赞 回复 分享
发布于 2017-04-26 21:16
第三个elseif你没有判断是否栈内有一个元素
点赞 回复 分享
发布于 2017-04-26 21:17
我也40%
点赞 回复 分享
发布于 2017-04-26 21:17
同40%
点赞 回复 分享
发布于 2017-04-26 21:18
表示没看到空格,做了也是40%
点赞 回复 分享
发布于 2017-04-26 21:21
之前没分割空格40%,分割后ac
点赞 回复 分享
发布于 2017-04-26 21:21
测试用例和自己的用例都能过。为什么百分之零
点赞 回复 分享
发布于 2017-04-26 21:22
import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main3 { public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line != null && !line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { String[] strArr=expr.split(" "); Stack<Integer> stack=new Stack<Integer>(); for(int i=0;i<strArr.length;i++){ if(!strArr[i].equals("*")&&!strArr[i].equals("^")&&!strArr[i].equals("+")){ stack.push(Integer.parseInt(strArr[i])); } if(stack.size()>16){ return -2; } if(strArr[i].equals("*")){ if(stack.size()<2){ return -1; } int n1=stack.pop(); int n2=stack.pop(); int res=n1*n2; stack.push(res); } if(strArr[i].equals("^")){ if(stack.size()<1){ return -1; } int n1=stack.pop(); int res=n1+1; stack.push(res); } if(strArr[i].equals("+")){ if(stack.size()<2){ return -1; } int n1=stack.pop(); int n2=stack.pop(); int res=n1+n2; stack.push(res); } } return stack.pop(); } } 这样ac出来了
点赞 回复 分享
发布于 2017-04-26 21:24
应该存在遇到*时候,栈顶前两个元素是数字才可以,如果不是数字直接-1,这种情况应该存在
点赞 回复 分享
发布于 2017-04-26 21:31
// write your code here public static int resolve(String expr) { String[] tokens = expr.split(" "); int length = tokens.length; Stack<Integer> stack = new Stack<>(); int a , b , c; for (int i = 0; i < length; i++) { if( !isOperator(tokens[i]) ) { stack.push(Integer.parseInt(tokens[i])); } else { if(!isSelfIncress(tokens[i])) { try { a = stack.pop(); b = stack.pop(); c = getRS(b, a, tokens[i]) ; if(stack.size() > 16) return -2; stack.push(c); } catch (EmptyStackException e) { return -1; } } if(isSelfIncress(tokens[i])) { try { a = 0; b = stack.pop(); c = getRS(b, a, tokens[i]) ; if(stack.size() > 16) return -2; stack.push(c); } catch (EmptyStackException e) { return 1; } } } } return stack.peek(); } public static boolean isOperator(String string) { if("+".equals(string) || "*".equals(string) || "^".equals(string) ) { return true; } else return false; } public static boolean isSelfIncress(String string) { if("^".equals(string) ) { return true; } else { return false; } } public static int getRS(int a ,int b , String operator){ if("+".equals(operator)) { return a + b; } else if("*".equals(operator)) { return a * b; } else if("^".equals(operator)) { return ++a; } return 0; } 我是通过catch异常和判断是否大于16来判断上下溢出的。
点赞 回复 分享
发布于 2017-04-26 21:33
import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args){ ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line!=null && !line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } public static int resolve(String expr) { String[] exprs = expr.split(" "); Stack<Integer> stack = new Stack<>(); for(int i=0;i<exprs.length;i++){ String temp = exprs[i]; if(temp.equals("")){ continue; } if(!(temp.contains("*")||temp.contains("^")||temp.contains("+"))){ if(stack.size()==16){ return -2; } else { stack.push(Integer.valueOf(temp)); } } else { String posibleNumber=""; boolean isNum = false; for(int j=0;j<temp.length();j++){ if(temp.codePointAt(j)>=48&&temp.codePointAt(j)<=57){ posibleNumber+=temp.charAt(j); isNum=true; } else { if(isNum) { if(stack.size()==16){ return -2; } else { stack.push(Integer.valueOf(posibleNumber)); posibleNumber=""; isNum=false; j--; } } else { if(temp.charAt(j) == '^'){ if(stack.size()==0){ return -1; } else { int tempNum = stack.pop()+1; stack.push(tempNum); } } else if(temp.charAt(j) == '+') { if(stack.size()<2){ return -1; } else { int tempA = stack.pop(); int tempB = stack.pop(); stack.push(tempA+tempB); } } else if(temp.charAt(j) == '*'){ if(stack.size()<2){ return -1; } else { int tempA = stack.pop(); int tempB = stack.pop(); stack.push(tempA*tempB); } } } } } if(isNum){ if(stack.size()==16){ return -2; } else { stack.push(Integer.valueOf(posibleNumber)); posibleNumber=""; isNum=false; } } } } if(stack.size()>=1) return stack.pop(); else return -1; } } 通过100%
点赞 回复 分享
发布于 2017-04-26 21:40
AC import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line != null && ! line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { Stack<Integer> stack = new Stack<>(); String[] split = expr.split("\\s+"); for (int i = 0; i < split.length; i ++) { if(split[i].equals("^")) { if(stack.size() < 1) return - 1; stack.push(stack.pop() + 1); } else if(split[i].equals("+")) { if(stack.size() < 2) return - 1; stack.push(stack.pop() + stack.pop()); } else if(split[i].equals("*")) { if(stack.size() < 2) return - 1; stack.push(stack.pop() * stack.pop()); } else { if(stack.size() > 16) return - 2; stack.push(Integer.parseInt(split[i])); } } return stack.pop(); } }
点赞 回复 分享
发布于 2017-04-26 23:13

相关推荐

联洲 嵌入式软件开发 总包48w(sp+3档)
点赞 评论 收藏
分享
2024-11-29 11:43
河南科技大学 Java
铁锈不腻玩家:下面那个袁先生删了,问他怎么回事,头像都换不明白
点赞 评论 收藏
分享
评论
点赞
2
分享
牛客网
牛客企业服务