目录
1. 定义一个类Generator 🌟🌟
2. 螺旋矩阵 II 🌟🌟
3. 删除排序链表中的重复元素 II 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
1. 定义一个类Generator
定义一个类 Generator(生成器类),它可以在每次调用其 next()方法时,产生由你 最喜欢的电影(如 Snow White 或 Star Wars)的字符构成的名字(作为 String 对象)。在字 符列表中的电影名用完之后,循环到这个字符列表的开始处。使用这个生成器来填充数组、 ArrayList、LinkedList、HashSet,然后打印每一个容器。
出处:
https://edu.csdn.net/practice/27452672
代码:
import java.util.*;
public class MovieGenerator {private String[] movies = new String[] { "SS", "DD", "HH", "FF", "XX", "ZZ" };private int i = 0;public String next() {return movies[i++ % movies.length];}public String[] getMovies() {return movies;}public Collection<String> fill(Collection<String> collection) {for (int i = 0; i < 8; i++) {collection.add(next());}return collection;}public static void main(String[] args) {MovieGenerator generator = new MovieGenerator();System.out.println(Arrays.toString(generator.getMovies()));System.out.println(generator.fill(new ArrayList<String>()));System.out.println(generator.fill(new LinkedList<String>()));System.out.println(generator.fill(new HashSet<String>()));}
}
输出:
略
2. 螺旋矩阵 II
给你一个正整数 n
,生成一个包含 1
到 n2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
示例 1:
输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1 输出:[[1]]
提示:
1 <= n <= 20
出处:
https://edu.csdn.net/practice/27452673
代码:
import java.util.*;
public class GenerateMatrix {public int[][] generateMatrix(int n) {int[][] res = new int[n][n];if (n == 0) {return res;}int left = 0;int right = n - 1;int up = 0;int down = n - 1;int i = 1;while (i <= n * n) {for (int col = left; col <= right; col++) {res[up][col] = i;i++;}up++;if (i <= n * n) {for (int j = up; j <= down; j++) {res[j][right] = i;i++;}right--;}if (i <= n * n) {for (int j = right; j >= left; j--) {res[down][j] = i;i++;}down--;}if (i <= n * n) {for (int j = down; j >= up; j--) {res[j][left] = i;i++;}left++;}}return res;}public static void main(String[] args) {GenerateMatrix a = new GenerateMatrix();System.out.println(Arrays.deepToString(a.generateMatrix(3)));}
}
输出:
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
3. 删除排序链表中的重复元素 II
存在一个按升序排列的链表,给你这个链表的头节点 head
,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。
返回同样按升序排列的结果链表。
示例 1:
输入:head = [1,2,3,3,4,4,5] 输出:[1,2,5]
示例 2:
输入:head = [1,1,1,2,3] 输出:[2,3]
提示:
- 链表中节点数目在范围
[0, 300]
内 -100 <= Node.val <= 100
- 题目数据保证链表已经按升序排列
出处:
https://edu.csdn.net/practice/27452674
代码:
import java.util.*;
public class deleteDuplicates {public static class ListNode {int val;ListNode next;ListNode(int x) { val = x; }}public static class Solution {public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode next = head.next;if (head.val == next.val) {while (next != null && head.val == next.val) {next = next.next;}head = deleteDuplicates(next);} else {head.next = deleteDuplicates(next);}return head;}}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 void main(String[] args) {Solution s = new Solution();int[] nums = {1,2,3,3,4,4,5};ListNode head = createLinkedList(nums);printLinkedList(head);head = s.deleteDuplicates(head);printLinkedList(head);int[] nums2 = {1,1,1,2,3};head = createLinkedList(nums2);printLinkedList(head);head = s.deleteDuplicates(head);printLinkedList(head);}
}
输出:
1->2->3->3->4->4->5->null
1->2->5->null
1->1->1->2->3->null
2->3->null
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Golang每日一练 专栏 | |
Python每日一练 专栏 | |
C/C++每日一练 专栏 | |
Java每日一练 专栏 |