LeetCode1—Two Sum

我会带着你远行 2022-06-09 04:38 315阅读 0赞

本类型博客中的各算法的时间复杂度分析均为博主自己推算,本类型博客也是博主自己刷LeetCode的自己的一些总结,因此个中错误可能较多,非常欢迎各位大神在博客下方评论,请不吝赐教

#

一、问题

  • 输入:整数数组nums和目标值target
  • 输出:在整数数组中找出两个值,使得其和为target,返回这两个数在数组中的索引
  • 假设条件:①数组中的每个数最多使用一次;②假设每个测试用例都只有唯一的一组索引

二、输入输出示例

  • 输入:nums = [2, 7, 11, 15], target = 9
  • 输出:[0, 1] (nums[0]+nums[1]=9)

三、解法

本题的基本思路就是先选取一个数,计算target跟它的差值,然后在剩下的数中查找有没有跟差值相等的数,有的话则返回两个的索引,没有的话则换一个数继续这个步骤。因此该问题解法的时间复杂度为 遍历次数*搜索次数,遍历次数固定为O(n),因此算法时间复杂度差异都在搜索算法上。以下几种解法都体现了不同的搜索方式。

解法一:暴力搜索

解题思路:针对每个数字,搜索数组中其他数字是否和它的和为目标值,如果是的话则返回结果,否则继续遍历下一个数字。每次处理完一个数字后,就可以确定它肯定不会出现在答案中,因此在处理下个数字时,就不再需要遍历它,以此减少遍历次数。

  1. package com.happy.leetcode.p1;
  2. import java.util.Arrays;
  3. /**
  4. * 29.69%
  5. * @author bird
  6. *
  7. */
  8. public class TwoSumV1 {
  9. public static void main(String[] args) {
  10. int[] nums = {2, 7, 11, 15};
  11. int target = 9;
  12. int[] result = new TwoSumV1().twoSum(nums, target);
  13. System.out.println(Arrays.toString(result));
  14. }
  15. public int[] twoSum(int[] nums, int target) {
  16. for(int i=0; i<nums.length-1; i++) {
  17. // 搜索剩下的数字
  18. for(int j=i+1; j<nums.length; j++) {
  19. if((nums[i]+nums[j])==target) {
  20. return new int[] {i, j};
  21. }
  22. }
  23. }
  24. return null;
  25. }
  26. }

时间复杂度分析:两层循环,所以时间复杂度为 O(n^2)

解法二:快排+二分法搜索

解题思路:该解法的思路与解法一类似,但是在搜索另一个值时采用二分法搜索,由于二分法搜索前需要进行排序,因此先使用快排处理数组

  1. package com.happy.leetcode.p1;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. /**
  5. * 48.34%
  6. * @author bird
  7. *
  8. */
  9. public class TwoSumV2 {
  10. public static void main(String[] args) {
  11. int[] nums = {2, 5, 5, 11};
  12. int target = 10;
  13. int[] result = new TwoSumV2().twoSum(nums, target);
  14. System.out.println(Arrays.toString(result));
  15. }
  16. public int[] twoSum(int[] nums, int target) {
  17. // 存储原数组的索引
  18. Integer[] indexes = new Integer[nums.length];
  19. for(int i=0; i<indexes.length; i++) {
  20. indexes[i] = i;
  21. }
  22. // 快排,获取到按原数组的值升序排序后的索引数组
  23. Arrays.sort(indexes, new Comparator<Integer>() {
  24. public int compare(Integer o1, Integer o2) {
  25. return nums[o1]-nums[o2];
  26. }
  27. });
  28. for(int i=0; i<indexes.length-1; i++) {
  29. int rest = target - nums[indexes[i]];
  30. // 二分法搜索
  31. int index = binarySearch(nums, indexes, i+1, rest);
  32. if(index==-1) {
  33. continue;
  34. }
  35. return new int[] {indexes[i], indexes[index]};
  36. }
  37. return null;
  38. }
  39. /**
  40. * 二分法搜索
  41. * @param nums 原数组
  42. * @param indexes 排序后的索引数组
  43. * @param start 起始位置
  44. * @param target 搜索目标值
  45. * @return
  46. */
  47. private int binarySearch(int[] nums, Integer[] indexes, int start, int target) {
  48. int end = nums.length-1;
  49. while(start<=end) {
  50. int index = (start+end)/2;
  51. if(nums[indexes[index]]==target) {
  52. return index;
  53. }else if(nums[indexes[index]]<target) {
  54. start = index+1;
  55. }else {
  56. end = index-1;
  57. }
  58. }
  59. return -1;
  60. }
  61. }

时间复杂度分析:快排的时间复杂度为 O(nlogn),针对数组中每个数要进行一次二分搜索,时间复杂度为logn,要遍历n个数,所以时间复杂度为O(nlogn),而总的为两者之和,所以时间复杂度为 O(nlogn)

解法三:利用Java中的Map类型来提高搜索效率

解题思路:该解题思路与第一种类似,只是在搜索的那一步将直接从数组中搜索转变为利用Java的Map类型来存储数组,从Map中搜索符合条件的值

  1. package com.happy.leetcode.p1;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. /**
  6. * 63.85%
  7. * @author bird
  8. *
  9. */
  10. public class TwoSumV5 {
  11. public static void main(String[] args) {
  12. int[] nums = {2, 5, 5, 11};
  13. int target = 10;
  14. int[] result = new TwoSumV5().twoSum(nums, target);
  15. System.out.println(Arrays.toString(result));
  16. }
  17. public int[] twoSum(int[] nums, int target) {
  18. Map<Integer, Integer> map = new HashMap<>();
  19. int rest;
  20. for(int i=0; i<nums.length; i++) {
  21. rest = target-nums[i];
  22. if(map.containsKey(rest)) {
  23. return new int[] {map.get(rest), i};
  24. }
  25. map.put(nums[i], i);
  26. }
  27. return null;
  28. }
  29. }

时间复杂度分析:Map的键搜索的时间复杂度为O(1),而数组遍历的时间复杂度为O(n),因此总的时间复杂度为 O(n)

解法四:使用散列的思想存储已经遍历的数组的数

解题思路:该解法思路同解法三,只是在存储时使用自己定义的散列思想将数值和其存储位置进行一一映射,这样在查找时可以大大增加时间复杂度。

  1. package com.happy.leetcode.p1;
  2. import java.util.Arrays;
  3. /**
  4. * 99.73%
  5. * @author bird
  6. *
  7. */
  8. public class TwoSumV4 {
  9. public static void main(String[] args) {
  10. int[] nums = {2, 5, 5, 11};
  11. int target = -14;
  12. int[] result = new TwoSumV4().twoSum(nums, target);
  13. System.out.println(Arrays.toString(result));
  14. }
  15. public int[] twoSum(int[] nums, int target) {
  16. int[] a = new int[16050];
  17. int temp;
  18. for(int i = 0; i < nums.length; ++i) {
  19. temp = target - nums[i] + 5;
  20. if(temp >= 0) {
  21. if(a[temp] > 0) {
  22. return new int[] {a[temp] - 1, i};
  23. } else {
  24. a[nums[i] + 5] = i + 1;
  25. }
  26. }
  27. }
  28. return null;
  29. }
  30. }

时间复杂度分析:遍历次数为n,搜索时间复杂度为O(1),因此总的时间复杂度为 O(n)

【注意】该代码并不适用于通用性解法,因为实际的测试用例的数值范围较小,因此该散列算法方法简单,执行效率高,因此执行时间极短,但是并不适用于通用情况,比如当数值为负时无法给出正确答案

发表评论

表情:
评论列表 (有 0 条评论,315人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode1Two Sum

    本类型博客中的各算法的时间复杂度分析均为博主自己推算,本类型博客也是博主自己刷LeetCode的自己的一些总结,因此个中错误可能较多,非常欢迎各位大神在博客下方评论,请不吝赐教