九月第四周总结

1. 用递归自己写pow函数常见的正确写法和错误写法

正确写法:

n为奇数:return pow(a*a, n/2) * a;  
n为偶数:return pow(a*a, n/2);
 
错误写法:
return pow(pow(a,2), n/2);
return pow(pow(a, n/2), 2);
有可能会陷入死循环,提示Segmentation fault
2. 127.0.0.1是什么?为什么使用它?
127.0.0.1是回送地址,指本地机,一般用来测试使用。回送地址(127.x.x.x)是本机回送地址(Loopback Address),即主机IP堆栈内部的IP地址,主要用于网络软件测试以及本地机进程间通信,无论什么程序,一旦使用回送地址发送数据,协议软件立即返回,不进行任何网络传输。(待补充。。。)
3. 关于String和char[]
String是一个对象类型,String定义的字符串是不能改变的
 
如果想要逐个字符地操作,可以使用
char[] ch =  str.toCharArray();

 将str转化为字符数组来进行操作。

另外,字符数组和String在操作上面也有不同。

4. java里面怎么找出三个数的最小值?

使用嵌套的形式:

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

5. linux 0.11里面的汇编代码对中文不友好,不要写中文注释,否则会报错。

6. 简单地区分DFS和BFS

DFS是深度优先搜索(Depth First Search),一条路下去,碰壁了再返回,直到无处可走,无路可退,搜索完毕。

BFS是广度优先搜索(Breadth First Search),一环一环地向外扩散,用队列保存每次向外扩散的节点值。

7. 要始终牢记Java传参传的都是引用,如果想要向一个集合中添加一个对象,不能只是把引用传进去,应该先根据当前的数据创建(new)一个对象,然后再传进去。这一点在循环的时候要特别注意,如果把创建或声明对象的语句放在了循环的外面,会出现错误。

比如:leetcode 95题 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);    //放在这里就没问题
                    root.right = rightTree;
                    root.left = leftTree;
                    res.add(root);
                }
            }    
        }
        
        return res;
    }
}

leetcode 127题

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;
        while(!s1.isEmpty() && !s2.isEmpty()){
            step++;
            if(s1.size() > s2.size()){
                Set<String> temp = s1;
                s1 = s2;
                s2 = temp;
            }
            Set<String> s = new HashSet<>();//用于存放当前结点可到达的所有结点
            for(String word : s1){
          //不能放在这里!!!!
for(int i = 0; i < len; i++){ char[] str = word.toCharArray();//如果放在第一个for下面,会导致str在第一轮过后,并没有经过初始化就进行了下一波操作。 for(char c = 'a'; c <= 'z'; c++){ str[i] = c; String temp = new String(str); if(s2.contains(temp)){ return step + 1; } if(!wordSet.contains(temp)){ //如果词典中不包含这个单词的话(或者已经处理过),那么想都不用想,这条路走不通! continue; } wordSet.remove(temp); s.add(temp);//把可能的结点存起来 } } } s1 = s; } return 0; } }

8. Java计算程序运行时间

long startTime=System.nanoTime();   //获取开始时间  
doSomeThing(); //测试的代码段  
long endTime=System.nanoTime(); //获取结束时间  
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");

9. 关于二叉搜索树

二叉查找树(Binary See),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。

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. 判断一个字符串是不是回文子串,可以使用布尔型数组,参考leetcode 5 https://leetcode-cn.com/problems/longest-palindromic-substring/

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

131题解

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));  //遍历一遍,s已经被切分完毕,并且切分的每一部分都是回文串
        for (int j = i; j < n; j++) {
            if (dp[i][j]) {
                tmp.add(s.substring(i, j + 1));     //找到一种解
                dfs(j + 1, tmp);  //继续寻找
                tmp.remove(tmp.size() - 1);  //回溯
            }
        }
    }
}
/*
s = aab
dp  0  1  2
0   t  t  f
1   f  t  f
2   f  f  t
*/

猜你喜欢

转载自www.cnblogs.com/zhaijiayu/p/11571996.html
今日推荐