摘要: 相对于4重循环,改成两个二重循环O(n2) 使用HashMap存储前两个数组的和,再在另外两个数组的循环中找值 1 class Solution { 2 public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums 阅读全文
posted @ 2024-06-13 21:20 清川1 阅读(20) 评论(0) 推荐(0)
摘要: 我只能说 直接双循环 1 class Solution { 2 public int[] twoSum(int[] nums, int target) { 3 int[] ans=new int[2]; 4 for(int i=0;i<nums.length;i++){ 5 for(int j=i+ 阅读全文
posted @ 2024-06-13 20:42 清川1 阅读(18) 评论(0) 推荐(0)
摘要: 主要是查看HashMap中是否存储n,如果存储就说明非快乐数 各位的数平方相加的方法 1 class Solution { 2 public boolean isHappy(int n) { 3 HashMap<Integer,Integer> map=new HashMap<>(); 4 5 wh 阅读全文
posted @ 2024-06-13 20:27 清川1 阅读(18) 评论(0) 推荐(0)
摘要: 使用hashmap记录数字个数,如果nums1中重复数字多,遍历2时则不需要取少 如果2中重复数字多,则每次取到就-1,直至map内无值 1 class Solution { 2 public int[] intersect(int[] nums1, int[] nums2) { 3 HashMap 阅读全文
posted @ 2024-06-13 13:26 清川1 阅读(18) 评论(0) 推荐(0)
摘要: 1 class Solution { 2 public int[] intersection(int[] nums1, int[] nums2) { 3 int[] ans1=new int[1002]; 4 int[] ans2=new int[1002]; 5 for(int i:nums1){ 阅读全文
posted @ 2024-06-13 13:07 清川1 阅读(19) 评论(0) 推荐(0)
摘要: 难 好好看看 1 class Solution { 2 public List<List<String>> groupAnagrams(String[] strs) { 3 if (strs == null || strs.length == 0) 4 return new ArrayList<>( 阅读全文
posted @ 2024-06-10 22:36 清川1 阅读(16) 评论(0) 推荐(0)
摘要: 同Q23 只需要先将随机字符串挨个存入hashmap中, 然后循环遍历给定字符串,只要最后hashmap中size为0,即可返回true 1 class Solution { 2 public boolean canConstruct(String ransomNote, String magazi 阅读全文
posted @ 2024-06-10 20:11 清川1 阅读(15) 评论(0) 推荐(0)
摘要: 1.先进行简单的字符长度判断,不相等直接返回false; 2.containsKey()的使用 3.在减减循环14-17行里判别key的value是否为0,要不然会报错 1 class Solution { 2 public boolean isAnagram(String s, String t) 阅读全文
posted @ 2024-06-10 20:06 清川1 阅读(14) 评论(0) 推荐(0)
摘要: 1.使用快慢指针 2.快指针向后移动两个位置,慢指针向后移动一个位置 3.若快慢指针能相等,则有环 4.将一个指针指向head,一个指针指向fast,挨个向后遍历,相等即进环点 1 public class Solution { 2 public ListNode detectCycle(ListN 阅读全文
posted @ 2024-06-09 22:59 清川1 阅读(21) 评论(0) 推荐(0)
摘要: 1.求出两链表长度 2.分情况进行长链表头结点后移 3.移至相同长度,两头结点一起后移,找到公共节点 1 public class Solution { 2 public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 3 阅读全文
posted @ 2024-06-09 22:42 清川1 阅读(21) 评论(0) 推荐(0)