388.比特位数 | 121.最佳股票买卖时机 | 283.移动零 | 543.二叉树的直径 | 21.合并两个有序链表 |20.有效括号 | 617.合并二叉树

class Solution {
    public int[] countBits(int n) {
        
        int[] res = new int[n+1];
        res[0] = 0;
        for(int i=1;i<=n ;i++){
            int num = i;
            while(num>0){
                int temp = num%2;
                res[i] = res[i]+temp;
                num = num/2;
            }
        }
        return res;
    }
}
class Solution {
    public int maxProfit(int[] prices) {
        int res = 0 ;
        int min = prices[0];
        if(prices.length <= 1){
            return res;
        }
        for(int i = 1;i<prices.length;i++){
            min = Math.min(prices[i],min);
            res = Math.max(prices[i] - min,res);
        }
        return  res;
    }
}
class Solution {
    public void moveZeroes(int[] nums) {
        int j=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i] != 0){
                nums[j] = nums[i];
                j++;
            }
        }
        for(int i = j;i<nums.length;i++){
            nums[i] = 0;
        }

    }
}
/**
 * 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 {
    private int dia = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        calculateHeight(root);
        return dia;
    }
    private int calculateHeight(TreeNode node){
        if(node == null){
            return 0;
        }
        int left = calculateHeight(node.left);
        int right = calculateHeight(node.right);
        dia = Math.max(dia,left +right);
        return Math.max(left,right)+1;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode res = new ListNode(0);
        ListNode head = res;
        while(list1!= null && list2 != null){
            if(list1.val < list2.val){
                head.next = list1;
                list1 = list1.next;
            }else{
                head.next = list2;
                list2 = list2.next;
            }
            head = head.next;
        }
        if(list1!=null){
            head.next = list1;
        }else{
            head.next = list2;
        }
        return res.next;
    }
}
class Solution {
    public boolean isValid(String s) {
        Stack<Character> temp = new Stack<>();
        char[] charArray = s.toCharArray();
        for(char a : charArray){
            if(a=='('||a=='['||a=='{'){
                temp.push(a);
            }else{
                if(temp.isEmpty()){
                    return false;
                }
                char top = temp.peek();
                if((a == ')' && top!='(')||
                    (a == ']' && top!='[')||
                    (a == '}' && top!='{')){
                return false;
            }
            temp.pop();
            }
    }
    return temp.isEmpty();
}
}
/**
 * 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 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null){
            return root2;
        }
        if(root2 == null){
            return root1;
        }
        TreeNode root = new TreeNode(root1.val + root2.val);
        root.left = mergeTrees(root1.left,root2.left);
        root.right = mergeTrees(root1.right,root2.right);
        return root;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值