Arts Week 13 (6/10 - 6/16)

What ARTS yes?
Mathimatics-Numerical algorithms : at least a week to do a leetcode algorithm problem;
Review : Read and review at least an English technical articles;
Tip : Learn at least one technical skills;
Share : Share ideas and think there is a technical article.


Algorithm

LeetCode 126. Word Ladder II

Thinking analysis
of this question is a classic search problem, the difficulty lies optimization. Questions asked to find all the possible solutions, we can put this question into a undirected graph , each word is represented by a node on the graph, to ensure that each word of the word neighbor with only one letter of the word is different if with a depth-first search violence, we check in on the map starting node and end nodes, taking into account all the possible paths from the beginning to the end, doing so would greatly increase the time consumed by the search, then how to optimize it? And before we increase the memory of the often mentioned difference is that here we consider the use of notation, because at the time of the search, we want to ensure that our current search direction is to go towards the end, what does that mean, that is to say, the next we're going to end node distance than the current node where we are closer to the distance from the end , we can carry out such a search based on two ideas, just mark the first time, the second is the search for possible answers . Here are labeled using breadth-first search, why not use a depth-first search? Because there may be a ring, the depth-first search in this figure can not guarantee the accuracy of marking . Search for answers when we can use depth-first search, it can also breadth, and here I use a deep search.


Reference Code

public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
    if (beginWord.equals(endWord) || !wordList.contains(endWord)) {
        return new ArrayList<>();
    }
    
    List<List<String>> results = new ArrayList<>();
    
    // bfs mark
    Map<String, Integer> memo = new HashMap<>();
    for (String word : wordList) {
        memo.put(word, -1);
    }
    
    memo.put(beginWord, -1);
    memo.put(endWord, 0);
    
    bfsMark(endWord, memo);
    System.out.println(memo);
    
    // dfs find path
    List<String> path = new ArrayList<String>();
    path.add(beginWord);
    
    dfs(memo, beginWord, endWord, wordList, path, results, memo.get(beginWord));
    
    return results;
}

private void dfs(Map<String, Integer> memo, 
                 String curWord,
                 String endWord,
                 List<String> wordList,
                 List<String> path,
                 List<List<String>> results,
                 int step) {
    if (curWord.equals(endWord)) {
        results.add(new ArrayList<String>(path));
    }
    
    if (step <= 0) {
        return;
    }
    
    for (String word : wordList) {            
        if (memo.get(word) == step - 1 && checkDiff(word, curWord)) {
            path.add(word);
            
            dfs(memo, word, endWord, wordList, path, results, step - 1);
                
            path.remove(path.size() - 1);
        }
    }
}

private boolean checkDiff(String a, String b) {
    int count = 0;
    for (int i = 0; i < a.length(); ++i) {
        if (a.charAt(i) != b.charAt(i)) {
            count++;
        }
        
        if (count > 1) {
            return false;
        }
    }
    
    return count == 1;
}

private void bfsMark(String endWord, Map<String, Integer> memo) {
    Queue<String> queue = new LinkedList<>();
    queue.offer(endWord);
    
    int stepCount = 0;
    while (!queue.isEmpty()) {
        int size = queue.size();
        stepCount++;
        
        for (int i = 0; i < size; ++i) {
            String curWord = queue.poll();
            
            char[] curWordArr = curWord.toCharArray();
            for (char r = 'a'; r <= 'z'; ++r) {
                for (int j = 0; j < curWordArr.length; ++j) {
                    char tmp = curWordArr[j];
                    curWordArr[j] = r;
                    
                    String newWord = String.valueOf(curWordArr);
                    
                    if (memo.containsKey(newWord) && memo.get(newWord) == -1) {
                        queue.offer(newWord);
                        memo.put(newWord, stepCount);
                    }
                    
                    curWordArr[j] = tmp;
                }
            }
        }
    }
}
复制代码

Review

A technical problem of how to ask someone the article:

How To Ask Questions The Smart Way

Asking questions is a science, especially on the Internet to ask questions, read the article to understand is not very good, but still understood, learned a lot, to sum up the key elements:

  • Before asking questions of others, we must be understanding of your problem and try to solve, you can Google, or to manually test simulation possible, and then, or ask knowledgeable friends around you, if you try a lot of method, but still no solution in to ask questions. They have found the problem and solve the problem on their own to help grow the biggest, so you can ensure you ask the question is relatively quality, thoughtful questions after the more able to attract the attention of experts, believe that no program experts willing to answer questions like " Java how to print out "such a problem.
  • POST problems on a network problem when the attention of the title is clear, direct and easy to understand . Author gives a " Object - Error performance " template, the object is to say something which is specific to the wrong place, where the more specific the better, the best plus a version number, the specific open source modules and so on; error performance means this thing what happened, where do is concise, straightforward explanation symptoms. What title is not too long but have to explain general, to ensure people will not read it time-consuming, but it can immediately react to this problem is
  • Mentioned above title POST problems, say that the body of the description of the problem, to note here is not to add some of their own guess, people need to take the time to read these speculations do not say, but also to understand why you think so confusing audio-visual, cause people do not want to look down , a better approach is to describe environmental problems, and then how do you do cause this problem, there is little that you have to write something that shows that you try to solve this problem but for various reasons, they failed to do so for two reasons:
    • Let others understand the problem is really a problem for you, you're want to get help, people will feel that his help will be valuable here
    • Dispel those possible solutions, sometimes we ask questions'll get some answers, but those answers you've tried before reading the find themselves too, but the problem is not resolved, then we have to go ask others to continue to pursue the question, this is very inefficient do not say, other people look at the next encounter the same problem when this post will read a lot of irrelevant information, it is inefficient
  • We asked the question, let someone else be able to quickly give solution, the best solution is an indicator that tells us how to do it. Can not say that to call someone to write a long list of codes, such help time-consuming, I believe not many people willing to help. Do not let other people see your code to help you debug, you want to ask, it is best to shorten the code length, easy to others, but also to facilitate their own
  • Preventing the purpose is not clear or broad questions, each question has an ultimate purpose, whether it wants to find bug, or is found to solve the bug, or re-design of the above suggestions how you want to point, we need to let others know what do you want. The other is not the problem with "urgent", "fast" such words, so there is a feeling someone else's urging, leading people do not want to see your question

Tip

This week contacted openresty, record it and install it on your Mac's boot

Installation (use homebrew):

>$ brew tap openresty/brew
>$ brew install openresty
复制代码

start up:

>$ openresty -p `...` -c ...
复制代码

stop:

>$ openresty -s quit -p `...` -c ...
复制代码

Again before recording a few more obscure, but now understand the basics of Python points:

  • Python among shallow copy of the object is not to say only copy references, but creates a new object, but the new object inside the child object or objects inside before the child object , the following code well explain this:
import copy

a = [[1,2],2,3,4]
b = copy.copy(a)

b.append(1)
b[0].append(3)

print(a)
print(b)

-----------------------------
[[1, 2, 3], 2, 3, 4]
[[1, 2, 3], 2, 3, 4, 1]
复制代码
  • == defined operators, Python and Java is very different here compared Python not address, but value. Python is used in the comparison address is operator, by the way Python with id to distinguish objects that represent, in fact, can represent the address of an object, is the operator of the computing speed will be much faster than the == operator, because is not is overloaded, it is only comparing two object id

Share

This week finally to talk algorithm, topological sorting

Problem analysis and topological sorting principle

Reproduced in: https: //juejin.im/post/5d02a8c1f265da1b971a706e

Guess you like

Origin blog.csdn.net/weixin_33834679/article/details/93180058