欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > 每日5题Day18 - LeetCode 86 - 90

每日5题Day18 - LeetCode 86 - 90

2025/3/24 12:37:47 来源:https://blog.csdn.net/alimamaalala/article/details/139507021  浏览:    关键词:每日5题Day18 - LeetCode 86 - 90

每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!

第一题:86. 分隔链表 - 力扣(LeetCode)

/*** 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 partition(ListNode head, int x) {ListNode beforehead = new ListNode(-1);ListNode afterhead = new ListNode(-1);ListNode before = beforehead;ListNode after = afterhead;ListNode current = head;while(current != null){if(current.val < x){before.next = new ListNode(current.val);before = before.next;}else{after.next = new ListNode(current.val);after = after.next;}current = current.next;}before.next = afterhead.next;after.next = null;return beforehead.next;}
}

第二题:87. 扰乱字符串 - 力扣(LeetCode)

感觉没读懂,一开始我觉得是下面这个做法

class Solution {public boolean isScramble(String s1, String s2) {//相当于同频异位的判断if(s1.length() != s2.length()){return false;}int[] arr = new int[26];for(int i = 0; i < s1.length(); i++){arr[s1.charAt(i) - 'a']++;arr[s2.charAt(i) - 'a']--;}for(int i = 0; i < 26; i++){if(arr[i] != 0){return false;}}return true;}
}

第三题:88. 合并两个有序数组 - 力扣(LeetCode)

class Solution {public void merge(int[] nums1, int m, int[] nums2, int n) {int current = m + n - 1;int l = m - 1, r = n - 1;while(current > -1){if(l > -1 && r > -1){nums1[current] = nums1[l] >= nums2[r] ? nums1[l--] : nums2[r--];}else if(l > -1){nums1[current] = nums1[l--];}else{nums1[current] = nums2[r--];}current--;}return;}
}

第四题:89. 格雷编码 - 力扣(LeetCode)

class Solution {public List<Integer> grayCode(int n) {// 创建一个列表来存储格雷编码序列List<Integer> res = new ArrayList<Integer>() {{ add(0); }};// 初始化 head 为 1,用于生成格雷编码序列int head = 1;// 循环遍历每一位的格雷编码for (int i = 0; i < n; i++) {// 从列表末尾向前遍历// 每次,将 head 加上当前值的逆序添加到列表中for (int j = res.size() - 1; j >= 0; j--)res.add(head + res.get(j));// 将 head 左移一位head <<= 1;}// 返回生成的格雷编码序列return res;}
}
AA

 第五题:90. 子集 II - 力扣(LeetCode)

class Solution {public List<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> res = new ArrayList<>();List<Integer> path = new ArrayList<>();Arrays.sort(nums);traversal(0, res, path, nums);return res;}private static void traversal(int start, List<List<Integer>> res, List<Integer> path, int[] nums){res.add(new ArrayList<>(path));for(int i = start; i < nums.length; i++){//组合,不是排列//注意去重if(i > start && nums[i] == nums[i - 1]){continue;}path.add(nums[i]);traversal(i + 1, res, path, nums);path.removeLast();}return;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词