力扣---2020.6.8

990. 等式方程的可满足性

class Solution {
    public boolean equationsPossible(String[] equations) {
        int length = equations.length;
        int[] parent = new int[26];
        for (int i = 0; i < 26; i++) {
            parent[i] = i;
        }
        for (String str : equations) {
            if (str.charAt(1) == '=') {
                int index1 = str.charAt(0) - 'a';
                int index2 = str.charAt(3) - 'a';
                union(parent, index1, index2);
            }
        }
        for (String str : equations) {
            if (str.charAt(1) == '!') {
                int index1 = str.charAt(0) - 'a';
                int index2 = str.charAt(3) - 'a';
                if (find(parent, index1) == find(parent, index2)) {
                    return false;
                }
            }
        }
        return true;
    }

    public void union(int[] parent, int index1, int index2) {
        parent[find(parent, index1)] = find(parent, index2);
    }

    public int find(int[] parent, int index) {
        //这段代码的作用是分组
        while (parent[index] != index) {
            parent[index] = parent[parent[index]];
            index = parent[index];
        }
        return index;
    }
}
class Solution {
    int[] parent = new int[26];
    public boolean equationsPossible(String[] equations) {
        for(int i=0;i<parent.length;i++) parent[i] = i;
        for(String s: equations){  //先根据==的字符串构造并查集
            if(s.charAt(1)=='='){
                merge(s.charAt(0),s.charAt(3));
            }
        }
        for(String s: equations){  //对于!=的字符串,查看两个变量是否在同一个集合中
            if(s.charAt(1)=='!'){
                if(find(s.charAt(0))==find(s.charAt(3))) return false;
            }
        }
        return true;
    }
    public void merge(char x,char y){
        parent[find(x)] = find(y);
    }
    public int find(char x){
        int n = x-'a';
        while(n!=parent[n]) n = parent[n];
        return n;
    }
}

面试题 17.17. 多次搜索

class Solution {
    public int[][] multiSearch(String big, String[] smalls) {
        Trie tree = new Trie(smalls);
        for (int i = 0; i < smalls.length; i++) {
            tree.insert(smalls[i], i);
        }
        char[] chars = big.toCharArray();
        int len = chars.length;
        for (int i = 0; i < len; i++) {
            tree.update(big.substring(i, len), i);
        }

        int[][] ans = new int[smalls.length][];
        for (int i = 0; i < ans.length; i++) {
            List<Integer> list = tree.lists[i];
            ans[i] = new int[list.size()];
            for (int j = 0; j < list.size(); j++) {
                ans[i][j] = list.get(j);
            }
        }
        return ans;

    }

    class Trie {
        class Node {
            int id;
            boolean flag;
            Node[] children;

            public Node() {
                id = -1;
                flag = false;
                children = new Node[26];
            }
        }

        Node root;
        List<Integer>[] lists;

        public Trie(String[] strings) {
            root = new Node();
            int len = strings.length;
            lists = new List[len];
            for (int i = 0; i < len; i++) {
                lists[i] = new ArrayList<>();
            }
        }

        public void insert(String word, int id) {
            Node p = root;
            char[] chars = word.toCharArray();
            int len = chars.length;
            for (int i = 0; i < len; i++) {
                int c = chars[i] - 97;
                if (p.children[c] == null) {
                    p.children[c] = new Node();
                }
                p = p.children[c];
            }
            p.flag = true;
            p.id = id;
        }

        public void update(String word, int offset) {
            Node p = root;
            char[] chars = word.toCharArray();
            int len = chars.length;
            for (int i = 0; i < len; i++) {
                int c = chars[i] - 97;

                if (p.children[c] == null) {
                    return;
                }
                p = p.children[c];
                if (p.flag) {
                    lists[p.id].add(offset);
                }
            }
        }
    }
}
class TrieNode{
    TrieNode[] childs;
    boolean isWord;
    public TrieNode(){
        childs = new TrieNode[26];
    }
}

class Solution {
    public int[][] multiSearch(String big, String[] smalls) {
        TrieNode root = new TrieNode();
        Map<String,List<Integer>> map = new HashMap<>();
        //构建字典树
        for (String small : smalls) {
            map.put(small,new ArrayList<>());
            insert(root,small);
        }
        //寻找所有单词在big中的起始位置,并存放在map中
        for(int i=0;i<big.length();i++){
             search(root, i, big,map);
        }
        //输出结果
        int[][] ans = new int[smalls.length][];
        for (int i = 0; i < smalls.length; i++) {
            ans[i] = map.get(smalls[i]).stream().mapToInt(Integer::valueOf).toArray();
        }
        return ans;
    }

    //构建后缀树
    public void insert(TrieNode root,String word){
        TrieNode node = root;
        for (int i = word.length()-1; i >=0; i--) {
            int idx = word.charAt(i)-'a';
            if(node.childs[idx]==null){
                node.childs[idx] = new TrieNode();
            }
            node = node.childs[idx];
        }
        node.isWord = true; //表示单词的结尾
    }

    //寻找以endPos结尾的所有单词的起始位置
    public void search(TrieNode root,int endPos,String sentence,Map<String,List<Integer>> map){
        TrieNode node = root;
        StringBuilder builder = new StringBuilder(); //单词作为key
        for(int i=endPos;i>=0;i--){
            int idx = sentence.charAt(i)-'a';
            if(node.childs[idx]==null){
                break;
            }
            //由于字典树存的是后缀,故要倒着插入
            builder.insert(0,sentence.charAt(i));
            node = node.childs[idx];//递归寻找
            //找到某个单词,就把起始位置添加到map中
            if(node.isWord){
                map.get(builder.toString()).add(i);
            }
        }
    }

}

面试题 04.05. 合法二叉搜索树

class Solution {
    Integer  preInt = null;
    public boolean isValidBST(TreeNode root) {
        if (root==null) return true;
//      //中序遍历二叉树
        boolean left = isValidBST(root.left);
       if (preInt!=null&&root.val<=preInt) return  false;
        preInt = root.val;
        boolean right = isValidBST(root.right);
        return left&&right;
    }
}
class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root==null) return true;
        return isValidBSThelp(root.left,Long.MIN_VALUE,root.val)&&isValidBSThelp(root.right,root.val,Long.MAX_VALUE);
    }
    private boolean isValidBSThelp(TreeNode root,long min,long max){
        if(root==null) return true;
        if(root.val<=min||root.val>=max) return false;
        boolean left = isValidBSThelp(root.left,min,root.val);
        boolean right = isValidBSThelp(root.right,root.val,max);
        return left && right;
    }
}
class Solution {
    public boolean isValidBST(TreeNode root) {
    	return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

	private boolean isValidBST(TreeNode root, long min, long max) {
		return root == null || (root.val > min && root.val < max && isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max));
	}
}

你知道的越多,你不知道的越多。

你可能感兴趣的:(数据结构与算法)