Last updated on 2023年6月7日 晚上
142.环形链表II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head;
while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ ListNode index1 = fast; ListNode index2 = head; while(index1 != index2){ index1 = index1.next; index2 = index2.next; } return index2; } } return null; } }
|
<<<<<<< HEAD
##
242.有效的字母异位词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public boolean isAnagram(String s, String t) { //设置record数组的长度大小为26(26个小写字母) int[] record = new int[26];
for(int i = 0; i< s.length();i++){ record[s.charAt(i) - 'a'] ++; }
for(int i = 0;i < t.length();i++){ record[t.charAt(i) - 'a'] }
for(int i = 0; i < record.length;i++){ if(record[i] != 0){ return false; } }
return true; } }
|
383.赎金信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] record = new int[26]; for(int i = 0; i < ransomNote.length();i++){ record[ransomNote.charAt(i) - 'a'] ++; }
for(int i = 0;i < magazine.length();i++){ record[magazine.charAt(i) - 'a'] }
for(int i = 0; i < record.length;i++){ if(record[i] > 0){ return false; } } return true; } }
|
349.两个数组的交集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { //数组作为map int[] record = new int[1001]; for(int i = 0; i < nums1.length;i++){ //map只记录是否有,并不需要记录每个key值对应的values数量 record[nums1[i]] = 2; }
for(int i = 0; i < nums2.length;i++){ if(record[nums2[i]] == 2){ record[nums2[i]] = 3; } }
List<Integer> tmpList = new ArrayList<>(); for(int i = 0;i < record.length;i++){ if(record[i] == 3){ tmpList.add(i); } }
int[] result = new int[tmpList.size()]; int index = 0; for(int num: tmpList){ result[index] = tmpList.get(index); index++; } return result; } }
|
0faf98f2fe074769adf459ed307fb014a35a9876
my-leetcode-logs-20230531
https://thewangyang.github.io/2023/05/31/leetcode-notes-20230531/