1 两数之和

  1. ### 解题思路
  2. 亮点1:利用map建立映射关系,储存映射关系,相比遍历,减少了大量的无用比较
  3. 亮点2A+B=C 我通过判断C-B是否存在map中,这一步直接就判定了A B是否存在
  4. ### 代码
  5. java
  6. class Solution {
  7. public int[] twoSum(int[] nums, int target) {
  8. //Map<int,int> map = new HashMap(); 不能为基础类型,但可以是引用类型。所以不能为int,但可以是Integer
  9. Map<Integer,Integer> map = new HashMap();
  10. for(int i = 0;i < nums.length;i++) {
  11. if(map.containsKey(target-nums[i])) {//亮点2 A+B=C 我通过判断C-B是否存在map中,这一步直接就判定了A B是否存在
  12. return new int[]{i,map.get(target-nums[i])};
  13. }
  14. map.put(nums[i], i);// 亮点1:利用map建立映射关系,储存映射关系,相比遍历,减少了大量的无用比较 **
  15. }
  16. return new int[0];
  17. }
  18. }

2 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

滑动窗口:
image.png
image.png

  1. // "abcabcbb";
  2. class Solution {
  3. public int lengthOfLongestSubstring(String s) {
  4. int result = 0;
  5. int start = 0;
  6. Map<Character,Integer> map = new HashMap<>();
  7. char[] chas = s.toCharArray();
  8. for(int i = 0; i < chas.length;i++) {
  9. char var = chas[i];
  10. //map.put(var, map.get(var) == null ? 0 : map.get(var) + 1);
  11. map.put(var, map.get(var) == null ? 1 : map.get(var) + 1);
  12. // if( map.get(i) > 1) {
  13. // start++;
  14. // }
  15. while(map.get(var) > 1) {
  16. map.put(chas[start], map.get(chas[start]) -1);
  17. //关键点:到达"abcabcbb"中abcb重复段时,a,b全部由1变为0了,
  18. 此时ab不参与重复计算了
  19. start++;
  20. }
  21. result = Math.max(i - start +1, result);
  22. }
  23. return result;
  24. }
  25. }
  26. 作者:andy-7
  27. 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/by-andy-7-9ykm/
  28. 来源:力扣(LeetCode
  29. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3 快速排序

  1. class Solution {
  2. public int[] sortArray(int[] nums) {
  3. quickSort(nums,0,nums.length-1);
  4. return nums;
  5. }
  6. public void quickSort (int[] nums, int low, int high) {
  7. if (low < high) {
  8. int index = partition(nums,low,high);
  9. quickSort(nums,low,index-1);
  10. quickSort(nums,index+1,high);
  11. }
  12. }
  13. public int partition (int[] nums, int low, int high) {
  14. int pivot = nums[low];
  15. while (low < high) {
  16. //移动high指针
  17. while (low < high && nums[high] >= pivot) {
  18. hight--;
  19. }
  20. //填坑
  21. if (low < high) nums[low] = nums[high];
  22. while (low < high && nums[low] <= pivot) {
  23. low++;
  24. }
  25. //填坑
  26. if (low < high) nums[high] = nums[low];
  27. }
  28. //基准数放到合适的位置
  29. nums[low] = pivot;
  30. return low;
  31. }
  32. }
  33. 作者:chefyuan
  34. 链接:https://leetcode-cn.com/problems/sort-an-array/solution/dong-hua-mo-ni-yi-ge-kuai-su-pai-xu-wo-x-7n7g/
  35. 来源:力扣(LeetCode
  36. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

4 归并排序

5 回文数

  1. package TIMU3;
  2. public class tumu3 {
  3. public static boolean isPalindrome(int x) {
  4. if (x < 0 || (x % 10 == 0 && x != 0))
  5. return false;
  6. int rev = 0;
  7. while (rev < x) {
  8. rev = x % 10 + rev * 10;
  9. x /= 10;
  10. }
  11. return (rev == x || rev/10 == x);
  12. }
  13. public static void main(String[] args) {
  14. System.out.println(isPalindrome(1221));
  15. }
  16. }

6 两数相加

先累加

再进位

在考虑细节:链表一个长一个短,首位进位

为什么merge不能直接对head1操作,而要对l1操作???????

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  13. ListNode head1 = l1;
  14. ListNode head2 = l2;
  15. while(head1 != null) {
  16. if(head2 != null) {
  17. // 第一步 把链表1 和链表2 直接相加
  18. head1.val += head2.val;
  19. head2 = head2.next;
  20. }
  21. //如果链表2比链表1长 把链表2多的部分直接赋给链表1即可 因为我们是以链表1为基础算的。
  22. if(head1.next == null && head2 != null) {
  23. head1.next = head2;
  24. break;
  25. }
  26. head1 = head1.next;
  27. }
  28. merge(l1);
  29. //遇坑!!!如果这样 merge(head1); 结果会错误!
  30. return l1;
  31. }
  32. public void merge(ListNode head) {
  33. while(head != null) {
  34. if(head.val >= 10) {
  35. //第二步 判断第一步相加结果 如果有大于10的需要进一位
  36. head.val = head.val%10;
  37. // 如果第一位(反转后的实际数组比如 781 这里指的是7不是1)需要进位 那么需要新建个位置
  38. if(head.next == null) {
  39. head.next = new ListNode(0);
  40. }
  41. head.next.val += 1;
  42. }
  43. head = head.next;
  44. }
  45. }
  46. }
  47. 作者:andy-7
  48. 链接:https://leetcode-cn.com/problems/add-two-numbers/solution/by-andy-7-jiyp/
  49. 来源:力扣(LeetCode
  50. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

7 只出现一次的数字

image.png

  1. class Solution {
  2. public int singleNumber(int[] nums) {
  3. int single = 0;
  4. for(int num : nums) {
  5. single ^= num;
  6. }
  7. return single;
  8. }
  9. }

如果允许引入其他容器 也可以这么解题
使用集合存储数组中出现的所有数字,并计算数组中的元素之和。由于集合保证元素无重复,因此计算集合中的所有元素之和的两倍,即为每个元素出现两次的情况下的元素之和。由于数组中只有一个元素出现一次,其余元素都出现两次,因此用集合中的元素之和的两倍减去数组中的元素之和,剩下的数就是数组中只出现一次的数字。

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/single-number/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。