3/21

217

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> hashset=new HashSet<>();
        for(int i=0;i<nums.length;i++){
            if(hashset.add(nums[i])==false){
                return true;
            }
        }
        return false;
    }
}

230

dfs
感覺我自己的代碼很不錯

 /**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int ans=0,index=0;
    public int kthSmallest(TreeNode root, int k) {
        dfs(root,k);
        return ans;
    }

    void dfs(TreeNode root,int k){
        if(root==null||index==k) return;
        dfs(root.left,k);
        if(++index==k){
            ans=root.val;
            return;
        }
        dfs(root.right,k);
    }
}

231

思路1:2的冪為:12222..
不斷除以2。如果最后結(jié)果(必定是奇數(shù)),是1true,否則false。
坑:位運(yùn)算符判斷奇偶 但是== 和 !=的優(yōu)先級大于&
坑: if(n==0) return false;

class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n==0) return false;//!!
        while((n&1)==0){ //位運(yùn)算符判斷奇偶 但是== 和 !=的優(yōu)先級大于&
            n>>=1;
        }
        if(n==1) return true;
        return false;
    }
}

時間復(fù)雜度o(logn)

思路2:時間復(fù)雜讀o(1)
https://leetcode-cn.com/problems/power-of-two/solution/power-of-two-er-jin-zhi-ji-jian-by-jyd/

ps n可能小于等于0,所以必須n>0才可以 n!=0是不對的,因為n可能是負(fù)數(shù)
可是為什么上面的做法可以不用管n負(fù)數(shù)不負(fù)數(shù)呢?因為負(fù)數(shù)不斷除以二還是負(fù)數(shù),不可能得到1.

class Solution {
    public boolean isPowerOfTwo(int n) {
        return n > 0 && (n & (n - 1)) == 0;
    }
}

只看了這個題解其他沒看

235

思路1:遞歸
很典型的遞歸,不用管內(nèi)部具體是怎么樣的
因為這題是bst
遞歸的思想:如果他們在左邊那我就去找左邊的最近公共祖先
如果他們右邊那我就去找右邊的最近公共祖先

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        if(p.val<root.val && q.val<root.val){
            return lowestCommonAncestor(root.left,p,q);
        }
        else if(p.val>root.val && q.val>root.val){
            return lowestCommonAncestor(root.right,p,q);
        }
        else return root;
    }
}

沒看別人的 但是應(yīng)該看看 人家一個while就搞定了呢

236

不會 寫了很久
思路1是:
分別保存尋找到兩個節(jié)點(diǎn)的路徑,最近的公共祖先就是路徑的分岔點(diǎn)!
在尋找保存節(jié)點(diǎn)的過程中,不是普通的前中后序遍歷直接加入,因為這樣你得到的是遍歷序列!不是找節(jié)點(diǎn)的路徑序列!所以如果在左邊找到了才保存呢!這是代碼邏輯!
20% 90%

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        List<TreeNode> pathp=new ArrayList<>();
        List<TreeNode> pathq=new ArrayList<>();
        find(root,pathp,p);
        find(root,pathq,q);
        TreeNode ans=new TreeNode();
        for(int i=pathp.size()-1,j=pathq.size()-1;i>=0&&j>=0;i--,j--){
            if(pathp.get(i).val!=pathq.get(j).val){
                break;
            }
            ans=pathp.get(i);
        }
        return ans;
    }
    boolean find(TreeNode root,List<TreeNode> path,TreeNode node){
        if(root==null) return false;
        else{
            if(root.val==node.val){
                path.add(root);
                return true;
            }
        }
        if(!find(root.left,path,node)){
            if(find(root.right,path,node)){
                path.add(root);
                return true;
            }
            else{
                return false;
            }
        }
        else{
            path.add(root);
            return true;
        } 
        
    }
}

思路2:

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // LCA 問題
        if (root == null) {
            return root;
        }
        if (root == p || root == q) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) {
            return root;
        } else if (left != null) {
            return left;
        } else if (right != null) {
            return right;
        }
        return null;
    }
}

沒看別人的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容