
LeetCode
LeetCode刷题,记录自己的解题思路和借鉴他人的解题技巧。
我的梦境传说
这个作者很懒,什么都没留下…
展开
-
反转链表的两种方法
反转链表的两种方法原创 2023-08-04 17:39:59 · 231 阅读 · 0 评论 -
LeetCode 的383. 赎金信,Java代码实现,使用hashmap计数来比较。
/*解题思路分别使用两个hashmap来计数(每个字符出现的次数),如果要组成的字符串中字符出现的次数大于提供的字符数量,就返回false,否则为true。*///代码class Solution { public boolean canConstruct(String ransomNote, String magazine) { HashMap<Character,Integer> map1 = new HashMap<>(); .原创 2020-07-17 09:21:57 · 220 阅读 · 0 评论 -
LeetCode 的217. 存在重复元素,Java代码使用set实现。
class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int i=0;i <nums.length;i++){ if(set.contains(nums[i])){ return true; }else{.原创 2020-07-14 23:46:38 · 212 阅读 · 0 评论 -
141. 环形链表,Java代码实现,使用双指针
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) {.原创 2020-07-14 23:28:03 · 198 阅读 · 0 评论 -
LeetCode 的 88. 合并两个有序数组,Java代码实现。
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] result = new int[m+n]; int index = 0; int i=0,j=0; for(;i < m && j < n; ){ if(nums1[i] <= nums2[j]){ .原创 2020-07-14 23:16:22 · 128 阅读 · 0 评论 -
两数之和II,输入有序数组,Java代码使用双指针法来查找。
import java.util.Arrays;//167. 两数之和 II - 输入有序数组public class TwoSum { /** * 思路是使用二分搜索,因为是升序的数组,如果low+high得到的值大于target, * 则high--,等于target则退出,否则是low++ */ public static int[] twoSum(int[] arr, int target){ int[] result = {-1,.原创 2020-07-12 15:33:05 · 183 阅读 · 0 评论 -
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。归并排序,Java实现
/** * 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。 */public class SortList { //排序链表 public static ListNode sortList(ListNode list){ if(list == null || list.next == null){ return list; } //获取中间节点 ListNode mid.原创 2020-06-26 13:40:05 · 674 阅读 · 0 评论