my java solution


  • 0
    T

    A major problem I came across was that relate to String object, = and .equals() is totally different!

    class Solution {
    	public int calPoints(String[] ops) {
    		int sum=0;
    		int len=ops.length;
    		boolean valid[] = new boolean[len];
    		int point[] = new int[len];
    		
            for (int i = 0; i < len; i++) {
                valid[i]=true;
    			point[i]=0;
            }
    
    		for (int i = 0; i < len; i++) {
    			if (isdigit(ops[i])) { 
                    point[i] = Integer.parseInt(ops[i]);
    				sum += point[i];
    			}
    			else if (ops[i].equals("+")) {
    				int j=i-1;
    				while (j>=0) {
    					if (valid[j]) {
    						point[i] += point[j];
    						break;
    					}
    					j--;
    				}
    				j=j-1;
    				while (j>=0) {
    					if (valid[j]) {
    						point[i] += point[j];
    						break;
    					}
    					j--;
    				}
    				sum+=point[i];
    			}
    			else if (ops[i].equals("D")) {
    				int j=i-1;
    				while (j>=0) {
    					if (valid[j]) {
    						point[i] += 2*point[j];
                            sum+=point[i];
    						break;
    					}
    					j--;
    				}
    			}
    			else if (ops[i].equals("C")) {
    				valid[i]=false;
    				int j=i-1;
    				while (j>=0) {
    					if (valid[j]) {
    						valid[j] = false;
    						sum-=point[j];
    						break;
    					}
    					j--;
    				}
    			}
    		}
    		return sum;
    	}
    	public boolean isdigit(String str) {
    		int len = str.length();
    		char [] a;
    		a = str.toCharArray();
            
    		for (int i = 0; i < a.length; i++) {
    			if (!(a[i]>='0'&&a[i]<='9')) {
    				return false;
    			}
    		}
    		return true;
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.