Java每日一练(20230409)

目录

1. 多数元素  

2. 反转链表 II  

3. 日期之间的遍历 

每日一练刷题专栏 

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 多数元素

给定一个大小为 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

示例 1:

输入:[3,2,3]
输出:3

示例 2:

输入:[2,2,1,1,1,2,2]
输出:2

进阶:

  • 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

出处:

https://edu.csdn.net/practice/25116177

代码:

import java.util.*;
public class majorityElement {
    public static class Solution {
        public int majorityElement(int[] nums) {
            int res = nums[0];
            int count = 0;
            for (int i = 0; i < nums.length; i++) {
                if (res == nums[i]) {
                    count++;
                } else {
                    count--;
                }
                if (count == 0) {
                    res = nums[i + 1];
                }
            }
            return res;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = {3,2,3};
        System.out.println(s.majorityElement(nums));
        int[] nums2 = {2,2,1,1,1,2,2};
        System.out.println(s.majorityElement(nums2));    }
}

输出:

3
2


2. 反转链表 II

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

出处:

https://edu.csdn.net/practice/25116178

代码:

public class reverseBetween {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }   
    public static ListNode createLinkedList(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        ListNode head = new ListNode(nums[0]);
        ListNode cur = head;
        for (int i = 1; i < nums.length; i++) {
            cur.next = new ListNode(nums[i]);
            cur = cur.next;
        }
        return head;
    }
    public static void printLinkedList(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("null");
    }
    public static class Solution {
        public ListNode reverseBetween(ListNode head, int left, int right) {
            if (left == 1) {
                return reverseN(head, right);
            }
            head.next = reverseBetween(head.next, left - 1, right - 1);
            return head;
        }
        ListNode successor = null; // 后驱节点
        // 反转以 head 为起点的 n 个节点,返回新的头结点
        ListNode reverseN(ListNode head, int n) {
            if (n == 1) {
                successor = head.next;
                return head;
            }
            ListNode last = reverseN(head.next, n - 1);
            head.next.next = head;
            head.next = successor;
            return last;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = {1,2,3,4,5};
        ListNode head = createLinkedList(nums);
        printLinkedList(head);
        head = s.reverseBetween(head, 2, 4);
        printLinkedList(head);
 
        int[] nums2 = {5};
        head = createLinkedList(nums2);
        printLinkedList(head);
        head = s.reverseBetween(head, 1, 1);
        printLinkedList(head);
   }
}

输出:

1->2->3->4->5->null
1->4->3->2->5->null
5->null
5->null


3. 日期之间的遍历

对给定的两个日期之间的日期进行遍历,比如startTime 是 2014-07-11;endTime 是 2014-08-11 如何把他们之间的日期获取并遍历出来。

出处:

https://edu.csdn.net/practice/25116179

代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SplitTime {
    private static List dateSplit(Date startDate, Date endDate) throws Exception {
        if (!startDate.before(endDate))
            throw new Exception("开始时间应该在结束时间之后");
        Long spi = endDate.getTime() - startDate.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);
        List dateList = new ArrayList();
        dateList.add(endDate);
        for (int i = 1; i <= step; i++) {
            dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));
        }
        return dateList;
    }
    public static void main(String[] args) throws ParseException {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start = sdf.parse("2015-4-20");
            Date end = sdf.parse("2015-5-2");
            List lists = dateSplit(start, end);
            if (!lists.isEmpty()) {
                for (Date date : lists) {
                    System.out.println(sdf.format(date));
                }
            }
        } catch (Exception e) {
        }
    }
}

输出:

2015-05-02
2015-05-01
2015-04-30
2015-04-29
2015-04-28
2015-04-27
2015-04-26
2015-04-25
2015-04-24
2015-04-23
2015-04-22
2015-04-21
2015-04-20


每日一练刷题专栏 

持续,努力奋斗做强刷题搬运工!

点赞,你的认可是我坚持的动力! 

 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

你可能感兴趣的:(#,Java每日一练,刷题专栏,java)