What's wrong with my dp solution in Java?


  • 0
    G
    public class Solution {
        public List<Integer> diffWaysToCompute(String input) {
    		List<Integer> num = new ArrayList<Integer>();
    		List<Character> symbol = new ArrayList<Character>();
    		decode(input,num,symbol);
    		int numlen = num.size();
    		if(numlen==1)return num;
    		int symbollen = symbol.size();
    		List<List<Integer>> result = new ArrayList<List<Integer>>();
    		for(int dis=1;dis<numlen;dis++){
    			List<List<Integer>> all = new ArrayList<List<Integer>>();
    			if(dis==1){
    				for(int i=0;i<symbollen;i++){
    					List<Integer> temp = new ArrayList<Integer>();
    					temp.add(compute(num.get(i), symbol.get(i), num.get(i+1)));
    					all.add(temp);
    				}
    				result = all;
    				continue;
    			}
    			for(int beg=0;beg<numlen-1;beg++){
    				for(int i=0;i<result.size();i++){
    					List<Integer> part = new ArrayList<Integer>();
    					for(int j=0;j<result.get(0).size()-1;j++){
    						int partres1 = compute(result.get(i).get(j), symbol.get(beg+dis-1), num.get(beg+dis));
    						int partres2 = compute(num.get(beg), symbol.get(beg+dis-1), result.get(i+1).get(j));
    						part.add(partres1);
    						part.add(partres2);
    					}
    					all.add(part);
    				}
    			}
    			result = all;
    		}
    		return result.get(0);
    	}
    	
    	private int compute(int input1,char c,int input2){
    		switch(c){
    		case '-':return input1-input2;
    		case '+':return input1+input2;
    		case '*':return input1*input2;
    		default: return 0;
    		}
    	}
    	
    	private void decode(String input,List<Integer> nums,List<Character> symbol){
    		int len = input.length();
    		for(int i=0;i<len;i++){
    			int temp =0;
    			char digit = '0';
    			while(i<len){
    				digit = input.charAt(i);
    				if(digit<'0'||digit>'9'){
    					symbol.add(digit);
    					break;
    				}
    				i++;
    				temp = 10*temp+digit-'0';
    			}
    			nums.add(temp);
    		}
    	}
    }

Log in to reply
 

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