首页 > 其他分享 >二刷Leetcode-Days04

二刷Leetcode-Days04

时间:2023-05-17 09:46:07浏览次数:42  
标签:index return cur int next 二刷 Days04 Leetcode size

数组:

    /**
     * 27. 移除元素
     * @param nums
     * @param val
     * @return 很多考察数组、链表、字符串等操作的面试题,都使用双指针法。
     */
    public int removeElement(int[] nums, int val) {
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[left] = nums[i];
                left++;
            }
        }
        return left;
    }

链表:

    /**
     * 707. 设计链表
     */
    class MyLinkedList {
        private ListNode head;
        //size存储链表元素的个数
        private int size;
        public MyLinkedList() {
            size = 0;
            head = new ListNode(-1);
        }

        public int get(int index) {
            //如果index非法,返回-1
            if (index < 0 || index >= size) {
                return -1;
            }
            ListNode cur = head;
            //包含一个虚拟头节点,所以查找第 index+1 个节点
            for (int i = 0; i <= index; i++) {
                cur = cur.next;
            }
            return cur.val;
        }

        //在链表最前面插入一个节点,等价于在第0个元素前添加
        public void addAtHead(int val) {
            addAtIndex(0, val);
        }

        //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
        public void addAtTail(int val) {
            addAtIndex(size, val);
        }

        // 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
        // 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
        // 如果 index 大于链表的长度,则返回空
        public void addAtIndex(int index, int val) {
            if (index > size) {
                return;
            }
            if (index == 0) {
                index = 0;
            }
            size++;
            //找到要插入节点的前驱
            ListNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            ListNode addNode = new ListNode(val);
            addNode.next = cur.next;
            cur.next = addNode;

        }

        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            size--;
            // 除了头结点需要单独考虑,其他不用
            if (index == 0) {
                head = head.next;
                return;
            }
            ListNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }
    }

哈希表:

    /**
     * 349. 两个数组的交集
     * @param nums1
     * @param nums2
     * @return 给定两个数组nums1和nums2,返回它们的交集。输出结果中的每个元素一定是唯一的。我们可以不考虑输出结果的顺序 。
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> tempSet = new HashSet<>();
        Set<Integer> res = new HashSet<>();
        // 使用set集合的去重功能
        for (int i = 0; i < nums1.length; i++) {
            tempSet.add(nums1[i]);
        }
        for (int num : nums2) {
            if (tempSet.contains(num)) {
                res.add(num);
            }
        }
        return res.stream().mapToInt(x -> x).toArray();
    }

// end -> 快排、堆排序、希尔排序、归并排序

标签:index,return,cur,int,next,二刷,Days04,Leetcode,size
From: https://www.cnblogs.com/LinxhzZ/p/17407588.html

相关文章

  • LeetCode 257. 二叉树的所有路径
    题目链接:LeetCode257.二叉树的所有路径题意:给你一个二叉树的根节点root,按任意顺序,返回所有从根节点到叶子节点的路径。解题思路:递归法采用递归法,就是一个dfs的过程,在遍历过程中,记录上路径即可。完整代码如下:varres[]stringvarpath[]intfuncbinaryTreePaths(......
  • LeetCode 111. 二叉树的最小深度
    题目链接:LeetCode111.二叉树的最小深度题意:给定一个二叉树,找出其最小深度。给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。解题思路:1.递归法与求最大深度类似,采用先序或者后序都是可以的,但是这里要注意一个问题:最小深度是从......
  • [LeetCode] 1343. Number of Sub-arrays of Size K and Average Greater than or Equa
    Givenanarrayofintegers arr andtwointegers k and threshold,return *thenumberofsub-arraysofsize k andaveragegreaterthanorequalto *threshold.Example1:Input:arr=[2,2,2,2,5,5,5,8],k=3,threshold=4Output:3Explanation:Sub-a......
  • 图解LeetCode——234. 回文链表
    一、题目给你一个单链表的头节点head,请你判断该链表是否为回文链表。如果是,返回true;否则,返回false。二、示例2.1>示例1:【输入】head=[1,2,2,1]【输出】true2.2>示例2:【输入】head=[1,2]【输出】false提示:链表中节点数目在范围[1,10^5]内0<=Node.v......
  • LeetCode 101. 对称二叉树
    题目链接:LeetCode101.对称二叉树题意:给你一个二叉树的根节点root,检查它是否轴对称。解题思路:1.递归法采用递归法思路就比较简单,因为要比较二叉树是否是轴对称的,因此就是比较左右子树是否轴对称,因此在遍历的过程中,就是比较左边的左子树与右边的右子树是否相等,以及左......
  • LeetCode 周赛 345(2023/05/14)体验一题多解的算法之美
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。往期回顾:LeetCode双周赛第104场·流水的动态规划,铁打的结构化思考周赛概览T1.找出转圈游戏输家(Easy)标签:模拟、计数T2.相邻值的按位异或(Medium)标签:模拟、数学、构造T3.矩阵中移动的最......
  • LeetCode 226. 翻转二叉树
    题目链接:LeetCode226.翻转二叉树题意:给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。解题思路:对于每一个节点,只需要考虑反转当前节点的左右子树即可,因此只需要考虑遍历顺序,本题中,采用前序和后序遍历都是可以的,但是中序遍历不行,如果采用中序,会将某些节点反转两......
  • LeetCode 94. 二叉树的中序遍历
    题目链接:LeetCode94.二叉树的中序遍历题意:给定一个二叉树的根节点root,返回它的中序遍历。解题思路:对于二叉树的遍历,有递归和非递归两种遍历方式,1.递归遍历**根据“左->根->右”的顺序,直接模拟即可。注意按照递归三部曲(递归的参数和返回值、递归的终止条件、单层......
  • 二刷Leetcode-Days02
    栈和队列:/***232.用栈实现队列*队列的先进先出可以使用两个栈stackIn和stackOut来实现;*出队列前,如果stackOut不为空,表示队列当前值在上一轮已进入stackOut中,还没有被消费掉*若stackOut为空,也就是队列当前值还在stackIn中,为了确保先进先......
  • 2023-05-15 leetcode周赛题
    找出转圈游戏输家mysolution100%passclassSolution:defcircularGameLosers(self,n:int,k:int)->List[int]:seen=set()now_num=1step=1seen.add(1)while1:stepSum=step*ktotal=now_num+stepSumnow_num=tot......