Coder Social home page Coder Social logo

某大厂面试题 about jcsprout HOT 3 CLOSED

crossoverjie avatar crossoverjie commented on May 18, 2024
某大厂面试题

from jcsprout.

Comments (3)

CyC2018 avatar CyC2018 commented on May 18, 2024 6

无并发的情况

public class Solution {

    private Trie trie = new Trie();

    public void addWord(String s) {
        trie.insert(s);
    }

    public boolean search(String s) {
        return trie.search(s);
    }

    private class Trie {

        private class Node {
            Node[] childs = new Node[26];
            boolean isLeaf;
        }

        private Node root = new Node();

        void insert(String word) {
            insert(word, root);
        }

        private void insert(String word, Node node) {
            if (node == null) return;
            if (word.length() == 0) {
                node.isLeaf = true;
                return;
            }
            int index = indexForChar(word.charAt(0));
            if (node.childs[index] == null) {
                node.childs[index] = new Node();
            }
            insert(word.substring(1), node.childs[index]);
        }

        boolean search(String word) {
            return search(word, root);
        }

        private boolean search(String word, Node node) {
            if (node == null) return false;
            if (word.length() == 0) return node.isLeaf;
            char c = word.charAt(0);
            String next = word.substring(1);
            if (c == '.') {
                for (Node child : node.childs) {
                    if (search(next, child)) return true;
                }
                return false;
            } else {
                int index = indexForChar(c);
                return search(next, node.childs[index]);
            }
        }

        private int indexForChar(char c) {
            return c - 'a';
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.addWord("bad");
        solution.addWord("dad");
        solution.addWord("mad");
        System.out.println(solution.search("pad"));
        System.out.println(solution.search("bad"));
        System.out.println(solution.search(".ad"));
        System.out.println(solution.search("..d"));
    }
}

from jcsprout.

CyC2018 avatar CyC2018 commented on May 18, 2024 2

要支持高并发的话,只要锁住要修改的节点即可。

        void insert(String word) {
            insert(word, root);
        }

        private void insert(String word, Node node) {
            if (node == null) return;
            if (word.length() == 0) {
                synchronized (node) {
                    node.isLeaf = true;
                }
                return;
            }
            int index = indexForChar(word.charAt(0));
            if (node.childs[index] == null) {
                synchronized (node) {
                    node.childs[index] = new Node();
                }
            }
            insert(word.substring(1), node.childs[index]);
        }

from jcsprout.

crossoverJie avatar crossoverJie commented on May 18, 2024
public class Search {

    private static Map<String,String> ALL_MAP = new ConcurrentHashMap<>(50000) ;


    /**
     * 换成 ascii码 更省事
     */
    private static final char[] dictionary = {'a','b','c','d','m','p'} ;

    public static void main(String[] args) throws InterruptedException {


        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    addWord(i + "ad");
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    addWord(i + "bd");
                }
            }
        });
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    addWord(i + "cd");
                }
            }
        });
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    addWord(i + "dd");
                }
            }
        });
        Thread t5 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    addWord(i + "ed");
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        t5.join();
        System.out.println(ALL_MAP.size());
        Assert.assertEquals(50000,ALL_MAP.size());
 
        addWord("bad");
        addWord("dad");
        addWord("mad");
        boolean pad = search("pad");
        System.out.println(pad);
        Assert.assertFalse(pad);

        boolean bad = search("bad");
        System.out.println(bad);
        Assert.assertTrue(bad);


        boolean ad = search(".ad");
        System.out.println(ad);
        Assert.assertTrue(ad);


        boolean bsearch = search("b..");
        System.out.println(bsearch);
        Assert.assertTrue(bsearch);

        boolean asearch = search(".a.");
        System.out.println(asearch);


        boolean search = search(".af");
        System.out.println(search);


        boolean search1 = search(null);
        System.out.println(search1);

    }

    public static boolean search(String keyWord){
        boolean result = false ;
        if (null ==  keyWord || keyWord.trim().equals("")){
            return result ;
        }

        //做一次完整匹配
        String whole = ALL_MAP.get(keyWord) ;
        if (whole != null){
            return true ;
        }

        char[] wordChars = keyWord.toCharArray() ;

        for (int i = 0; i < wordChars.length; i++) {
            char wordChar = wordChars[i] ;

            if (46 != (int)wordChar){
                continue ;
            }

            for (char dic : dictionary) {
                wordChars[i] = dic ;
                boolean search = search(String.valueOf(wordChars));

                if (search){
                    return search ;
                }

                String value = ALL_MAP.get(String.valueOf(wordChars));
                if (value != null){
                    return true ;
                }
            }

        }


        return result ;
    }


    public static void addWord(String word){
        ALL_MAP.put(word,word) ;
    }
}

from jcsprout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.