45天打卡计划_Day10

题目信息 :二叉树的锯齿形层序遍历

知识点:树、二叉树、广度优先搜索

题目描述:

给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:[[3],[20,9],[15,7]]

示例 2:

输入:root = [1]
输出:[[1]]

示例 3:

输入:root = []
输出:[]

数据范围:

  • 树中节点数目在范围 [0, 2000] 内
  • -100 \leq Node.val \leq 100

解题思路:

层序遍历 维护一个翻转标识记录每层的遍历顺序,隔层反序即可

/**
 * 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 List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i >0;i--){
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            if(res.size() %2 ==1) Collections.reverse(tmp);
            res.add(tmp);
        }
        return res;
    }
}

复杂度分析:

  • 时间复杂度 O(N) : N 为二叉树的节点数量,即 BFS 需循环 N 次,占用 O(N)。共完成 少于 N 个节点的倒序操作,占用 O(N)
  • 空间复杂度 O(N) : 最差情况下,即当树为满二叉树时,最多有 N/2 个树节点同时在 queue 中,使用 O(N) 大小的额外空间。


题目信息 :存在重复元素

知识点:数组、排序、哈希表

题目描述:

给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。

示例 1:

输入:nums = [1,2,3,1]

输出:true

解释:

元素 1 在下标 0 和 3 出现。

示例 2:

输入:nums = [1,2,3,4]

输出:false

解释:

所有元素都不同。

示例 3:

输入:nums = [1,1,1,3,3,4,3,2,4,2]

输出:true

数据范围:

  • 1\leq nums.length \leq 10^{5}
  • -10^{9} \leq nums[i] \leq 10^{9}

解题思路:

对于数组中每个元素,我们将它插入到哈希表中。如果插入一个元素时发现该元素已经存在于哈希表中,则说明存在重复的元素。

写法一

把 nums 中的元素都丢到哈希集合中(去重)。如果哈希集合的大小小于 n,则说明有重复元素。

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int x : nums) {
            set.add(x);
        }
        return set.size() < nums.length;
    }
}

复杂度分析
  • 时间复杂度:O(N),其中 n 是 nums 的长度。
  • 空间复杂度:O(N)

写法二:优化方法一剪枝

遍历数组,一边遍历一边把 nums[i] 加到哈希集合中。如果发现 nums[i] 已经在哈希集合中,直接返回 true。这样可以提前退出循环。

class Solution {
    public boolean containsDuplicate(int[] nums) {
    Set<Integer> set = new HashSet<>();
    for(int x : nums) {
        if(!set.add(x)) {
            return true;
        }
    }
    return false;
    }
}
复杂度分析
  • 时间复杂度:O(N),其中 n 是 nums 的长度。
  • 空间复杂度:O(N)
Vue可以通过使用第三方组件库或自己编写组件来实现日历签到功能。 1. 使用第三方组件库 例如,使用Element UI中的DatePicker组件,可以实现选择日期的功能。在选中日期后,可以将签到状态保存到后端数据库中,再在日历上展示已签到的日期。 2. 自己编写组件 可以使用Vue自带的组件和插件来编写日历签到组件。具体实现方法如下: - 在Vue项目中创建一个Calendar组件; - 使用Vue自带的v-for指令展示日历; - 在每个日期上绑定点击事件,用来切换签到状态; - 将签到状态保存到Vuex或后端数据库中; - 在日历上展示已签到的日期。 下面是一个简单的日历签到组件代码示例: ```html <template> <div class="calendar"> <div class="header">{{ year }}年{{ month }}月</div> <table> <thead> <tr> <th v-for="day in days">{{ day }}</th> </tr> </thead> <tbody> <tr v-for="week in weeks"> <td v-for="date in week" :class="{ active: isSigned(date) }" @click="toggleSign(date)">{{ date }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { year: new Date().getFullYear(), month: new Date().getMonth() + 1, days: ["日", "一", "二", "三", "四", "五", "六"], weeks: [], signedDates: [] }; }, computed: { daysInMonth() { const date = new Date(this.year, this.month, 0); return date.getDate(); } }, mounted() { this.generateCalendar(); }, methods: { generateCalendar() { const firstDay = new Date(this.year, this.month - 1, 1).getDay(); const lastDate = this.daysInMonth; const weeks = []; let week = []; for (let i = 0; i < firstDay; i++) { week.push(""); } for (let i = 1; i <= lastDate; i++) { week.push(i); if (week.length === 7) { weeks.push(week); week = []; } } if (week.length > 0) { for (let i = week.length; i < 7; i++) { week.push(""); } weeks.push(week); } this.weeks = weeks; }, toggleSign(date) { if (this.isSigned(date)) { // 取消签到 const index = this.signedDates.indexOf(date); this.signedDates.splice(index, 1); } else { // 签到 this.signedDates.push(date); } // 将签到状态保存到Vuex或后端数据库中 }, isSigned(date) { return this.signedDates.includes(date); } } }; </script> <style> .calendar { width: 300px; margin: 0 auto; text-align: center; } .header { font-size: 16px; font-weight: bold; margin-bottom: 10px; } table { width: 100%; border-collapse: collapse; } th { height: 30px; line-height: 30px; } td { height: 50px; line-height: 50px; cursor: pointer; } td.active { background-color: #66ccff; color: #fff; } </style> ``` 这个示例中的Calendar组件展示了一个月的日历,并提供了签到功能。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值