My Java Solution, code is bad, but works


  • 0
    W

    My idea is to add operators between two digits.
    But here, I used not only '+' , '-', '*', but also a whitespace.
    The whitespace is used to make two adjacent digits combined together. After the dfs, then I traverse the the result and eliminate the whitespace.
    My code style is really bad, could someone give me some advice?

    public class Solution {
            public List<String> addOperators(String num, int target) {
                List<String> res = new ArrayList<String>();
                if (num == null || num.length() == 0) return res;
                List<String> temp = new ArrayList<String>();
                char[] operator = {'+', '-', '*', ' '};
                allCombine(temp, new StringBuilder(), num.toCharArray(), operator, 0);
                for (String str : temp) {
                    if (calculate(str) == target) {
                        res.add(str);
                    }
                }
                return res;
            }
            public void allCombine(List<String> res, StringBuilder sb, char[] array, char[] operator, int position) {
                if (position == array.length-1) {
                    sb.append(array[array.length-1]);
                    StringBuilder sb2 = new StringBuilder();
                    for (int i = 0; i < sb.length(); i++) {
                        if (sb.charAt(i) != ' ') sb2.append(sb.charAt(i));
                    }
                    res.add(sb2.toString());
                    return ;
                }
                sb.append(array[position]);
                for (int j = 0; j < operator.length; j++) {
                    sb.append(operator[j]);
                    allCombine(res, sb, array, operator, position+1);
                    sb.setLength(position*2+1);
                }
            }
            public int calculate(String str) {
                if (str == null || str.length() == 0) return 0;
                LinkedList<Integer> list = new LinkedList<Integer>();
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    if (Character.isDigit(c)) {
                        int num = c - '0';
                        int j = i + 1;
                        while (j < str.length() && Character.isDigit(str.charAt(j))) {
                            if (num == 0) return -1;
                            if (Integer.MAX_VALUE/num < 10 || 
                                (Integer.MAX_VALUE/num == 10 && Integer.MAX_VALUE % 10 < str.charAt(j)-'0')) return -1;
                            num = num * 10 + (str.charAt(j) - '0');
                            j++;
                        }
                        i = j - 1;
                        if (!list.isEmpty() && list.peek() == 2) {
                            list.pop();
                            int hold = list.pop();
                            hold = hold * num;
                            list.push(hold);
                        } else {
                            list.push(num);
                        }
                    } else {
                        switch (c) {
                            case '+' : list.push(0); break;
                            case '-' : list.push(1); break;
                            case '*' : list.push(2); break;
                            default : return -1;
                        }
                    }
                }
                if (list.isEmpty()) return 0;
                int res = list.pollLast();
                while (!list.isEmpty()) {
                    int operator = list.pollLast();
                    int operand = list.pollLast();
                    if (operator == 0) {
                        res += operand;
                    } else if (operator == 1) {
                        res -= operand;
                    }
                }
                return res;
            } 
        }

Log in to reply
 

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