自己烂代码烂思路的最好体现

这代码功力真的是烂到发指 好好反思一下
990. Satisfiability of Equality Equations

Input: [“a==b”,“b!=a”]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.

class Solution {
    public boolean equationsPossible(String[] equations) {
        if (equations == null || equation.length <= 1) return true;
        List<HashSet<Character>> list = new ArrayList<>();
        for(String equation: equations) {
            char n1 = equation.charAt(0);
            char n2 = equation.charAt(3);
            char sign = equation.charAt(1);
            if (sign == '=') {
                //first we need to check if those node are new or old
                int numOld = 0;
                boolean sameSet = true;
                int[] index new int[]{-1, -1};
                int k = 0;
                
                for (int i = 0; i < list.size(); i++) {
                    Hashset<Character> set = list.get(i);
                    if (set.contains(n1) && set.contains(n2)) {
                        //do nothing
                        break;
                    } else (set.contains(n1) || set.contains(n2)) {
                        numOld++;
                        sameSet = false;
                        index[k++] = i;
                    }
                }
                if (numOld == 2) {
                    if (sameSet) {
                        //do nothing
                    } else {
                        list.get(index[0]).addAll(list.get(index[1]));
                        list.remove(index[1]);
                        //merge
                        //but we need index of those two set to locate them and merge and delete
                    }
                } else if (numOld == 0) {
                    HashSet<Character> set = new HashSet<>();
                    set.add(n1);
                    set.add(n2);
                    list.add(set);
                } else {
                    //we also need the index of that old node's set index, just to put the new node in this set
                    list.get(index[0]).add(n1);
                    list.get(index[0]).add(n2); //we don't know which one is the new one, so we add them all
                }
            }
            
        }
        for (String equation: equations) {
            char n1 = equation.charAt(0);
            char n2 = equation.charAt(3);
            char sign = equation.charAt(1);
            
            if (sign == '!') {
                int numOld = 0;
            }
        }
    }
}
posted @ 2020-11-29 00:18  EvanMeetTheWorld  阅读(18)  评论(0)    收藏  举报