Summary of the fourth week of September

1. Write a function pow common error correct wording and writing their own recursive

Correct wording:

n is odd: return pow (a * a, n / 2) * a;  
n is even: return pow (a * a, n / 2);
 
Error writing:
return pow(pow(a,2), n/2);
return pow(pow(a, n/2), 2);
There may fall into an infinite loop, suggesting Segmentation fault
2. 127.0.0.1 What is that? Why use it?
127.0.0.1 is the loopback address, referring to the local machine, usually used for testing. Loopback address (127.xxx) is the local loopback address (Loopback Address), ie the IP address of the internal IP of the host stack, is mainly used for inter-network software testing as well as the local machine interprocess communication, no matter what the program, by using the loopback address to send data, protocol software immediately returned, without any network transmission. (To be added...)
3. About String and char []
An object type is String, String String definition can not be changed
 
If you want to operate character by character, you can use
char[] ch =  str.toCharArray();

 Str into the character array to operate.

In addition, an array of characters and String operation above, there are different.

 

4. java inside how to find the minimum number of three?

Using nested form:

Math.min(a, Math.min(b,c))

 

5. linux 0.11 inside the assembly code for Chinese unfriendly, do not write Chinese comments, otherwise it will error.

6. The simple distinction between DFS and BFS

DFS is a depth-first search (Depth First Search), go on a road, hit a wall and then return until nowhere to go, no turning back, the search is completed.

BFS is a breadth first search (Breadth First Search), outdiffusion of a ring, the used queue stored value of each node outdiffusion.

7. Always keep in mind the Java parameter passing references are passed, if you want to add an object to a collection, you can not just pass in the reference, you should first create a (new) an object based on the current data, and then send it in. This is the time to pay special attention in the cycle, if the statement or create objects on the statement out of the loop, the error occurs.

For example: leetcode 95 title  https://leetcode-cn.com/problems/unique-binary-search-trees-ii/

class Solution {
    public List<TreeNode> generateTrees(int n) {
        if(0 == n){
            return new ArrayList<>();
        }
        return helper(1,n);
    }
    
    public List<TreeNode> helper(int start, int end){
        List<TreeNode> res = new ArrayList<>();
        if(start > end){
            res.add(null);
            return res;
        }    
        if(start == end){       //有一个元素
            res.add(new TreeNode(start));
            return res;
        }
        for(int i = start; i <= end; i++){
            List<TreeNode> leftTrees = helper(start, i - 1);
            List<TreeNode> rightTrees = helper(i+1, end);
//            TreeNode root = new TreeNode(i);放在这里就会出现问题
            for(TreeNode leftTree : leftTrees){
                for(TreeNode rightTree : rightTrees){
                    TreeNode root = new TreeNode(i);    // put here no problem 
                    root.right = righttree; 
                    root.left = leftTree; 
                    res.add (the root); 
                } 
            }     
        } 
        
        return RES; 
    } 
}

leetcode 127 title

import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> wordSet = new HashSet<>(wordList.size());
        wordSet.addAll(wordList);
        if(!wordSet.contains(endWord)){
            return 0;
        }
        Set<String> s1 = new HashSet<>();
        Set<String> s2 = new HashSet<>(); 
        S1.add (beginWord); 
        s2.add (endWord); 
        int len = beginWord.length ();
         int STEP = 0 ;
         the while (!! S1.isEmpty () && s2.isEmpty ()) { 
            STEP + + ;
             IF (s1.size ()> s2.size ()) { 
                the Set <String> TEMP = S1; 
                S1 = S2; 
                S2 = TEMP; 
            } 
            the Set <String> = S new new HashSet <> (); // with storing the current node to all nodes reachable 
            for(String Word: s1) { 
          // can not be placed here! ! ! !
for ( int I = 0; I <len; I ++ ) { char [] str = word.toCharArray () ; // if placed below for the first, str will lead after the first round, and has not been initialized carried out the next wave action. for ( char C = 'A'; C <= 'Z'; C ++ ) { STR [I] = C; String TEMP = new new String (STR); IF (s2.contains (TEMP)) { return STEP +. 1 ; } IF(! wordSet.contains (the TEMP)) { // If the dictionary does not contain the word words (or have already been processed), then a no-brainer, this road leads nowhere! Continue ; } wordSet.remove (TEMP); s.add (TEMP); // the node may save up } } } S1 = S; } return 0 ; } }

8. Java run-time calculation program

Long startTime = System.nanoTime ();    // acquisition start time   
doSomething (); // test code segment   
Long endTime = System.nanoTime (); // Gets the end time   
System.out.println ( "program run time:" + (endTime-startTime) + " ns");

9. With respect to binary search tree

Binary search tree (Binary See), (Also: a binary search tree, binary sort tree) which is either an empty tree or a binary tree with the following properties: if its left subtree is not empty, then left the child values ​​of all tree nodes is less than the value of the root node; if its right subtree is not empty, then the value of the right subtree of all nodes are greater than the value of the root; its left, They were also right subtree binary sort tree.

leetcode 95 https://leetcode-cn.com/problems/unique-binary-search-trees/solution/hua-jie-suan-fa-96-bu-tong-de-er-cha-sou-suo-shu-b/

10. Analyzing string is not a palindrome substring, a boolean array reference. 5 leetcode  https://leetcode-cn.com/problems/longest-palindromic-substring/

leetcode 131     https://leetcode-cn.com/problems/palindrome-partitioning/

131 solution to a problem

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    int n;
    String s;
    boolean[][] dp;
    List<List<String>> res = new ArrayList<>();
    public List<List<String>> partition(String s) {
        int n = s.length();
        this.s = s;
        this.n = n;
        dp = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                if (s.charAt(i) == s.charAt(j) && (i - j < 2 || dp[j + 1][i - 1])) dp[j][i] = true;
            }
        }
        System.out.println(Arrays.deepToString(dp));
        dfs(0,new ArrayList<String>());
        return res;


    }
    private void dfs(int i,ArrayList<String> tmp) {
        if (i == n) res.add(new ArrayList<>(tmp));  //Again traversed, s completion has been segmented, and each portion of the palindromic sequence are sliced 
        for ( int J = I; J <n-; J ++ ) {
             IF (DP [I] [J]) { 
                tmp.add ( s.substring (I, J +. 1));      // find a solution 
                DFS (J +. 1, tmp);   // continue to search 
                tmp.remove (tmp.size () -. 1);   // back 
            } 
        } 
    } 
} 
/ * 
S = AAB 
DP. 1 2 0 
0 TTF 
. 1 FTF 
2 FFT 
* /

 

 

 

Guess you like

Origin www.cnblogs.com/zhaijiayu/p/11571996.html