算法例子:求出数组中所有组合(两个数之和等于指定数)(Java)

这里介绍三种方法

方法1

思路:
双重for循环,每一个数和所有其他的数进行相加,如果等于目标值则打印出
时间复杂度 :
O(N2)
空间复杂度 :
O(1)

public static void main(String[] args) {
        int[] nums = new int[]{1, 8, 9, 6, 4, 3, 7};
        int target = 10;
        method1(nums, target);
    }
public static void method1(int[] nums, int target) {
        for (int i = 0, size = nums.length; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (nums[i] + nums[j] == target) {
                    System.out.println(nums[i] + "+" + nums[j] + "=" + target);
                    break;
                }
            }
        }
    }

结果:

1+9=10
9+1=10
6+4=10
4+6=10
3+7=10
7+3=10

方法2

思路:
1.先将数组进行排序
2.从两边进行遍历数组(left,right指针)
2.1:num[left]+num[right]=target,则打印出,则left++,right–
2.2:num[left]+num[right]<target,则left++
2.3:num[left]+num[right]>target,right–
时间复杂度:
排序算法复杂度+O(N)
空间复杂度:
排序算法复杂度+O(1)

public static void main(String[] args) {
        int[] nums = new int[]{1, 8, 9, 6, 4, 3, 7};
        int target = 10;
        method2(nums, target);
    }
public static void method2(int[] nums, int target) {
        //1.先排序(这里使用的是快速排序){1,3,4,6,7,8,9};
        quickSort(nums, 0, nums.length - 1);
        //2.次数num是已经排序好的数组,从两边遍历已排序的数组
        for (int left = 0, right = nums.length - 1; left < right; ) {
            //2.1.相等的话,直接输出即可
            if (nums[left] + nums[right] == target) {
                System.out.println(nums[left] + "+" + nums[right] + "=" + target);
                left++;
                right--;
            } else if (nums[left] + nums[right] < target) {
                //2.2.相加小于目标值,则left++
                left++;
            } else {
                //2.3.相加小于目标值,则 right--
                right--;
            }
        }
    }

快排方法

/**
     * 快排
     *
     * @param nums
     * @param startIndex
     * @param endIndex
     */
    private static void quickSort(int[] nums, int startIndex, int endIndex) {
        if (startIndex < endIndex) {
            //第一次划分
            int partition = partition(nums, startIndex, endIndex);
            //递归调用左侧排序
            quickSort(nums, startIndex, partition - 1);
            //递归调用右侧排序
            quickSort(nums, partition + 1, endIndex);
        }
    }

快排中的一次划分


    /**
     * @param nums       待排序数组
     * @param startIndex 开始位置
     * @param endIndex   结束位置
     * @return 排序后:关键字的索引
     */
    private static int partition(int[] nums, int startIndex, int endIndex) {
        int key = nums[startIndex];
        while (startIndex < endIndex) {
            //从右往左
            while (startIndex < endIndex && nums[endIndex] > key) {
                endIndex--;
            }
            if (startIndex < endIndex) {
                nums[startIndex] = nums[endIndex];
                startIndex++;
            }
            //从左往右
            while (startIndex < endIndex && nums[startIndex] < key) {
                startIndex++;
            }
            if (startIndex < endIndex) {
                nums[endIndex] = nums[startIndex];
                endIndex--;
            }
        }
        //等到左右重合有,相当于此位置已经分出了三个区域
        //[小于flagNum][flagnum][大于flagNum]
        nums[startIndex] = key;
        return startIndex;
    }

结果:

1+9=10
3+7=10
4+6=10

方法3

思路:
限制条件:1.数组中的数字不会很大.2.前提是知道素组中的最大值(否则的话还要遍历一遍找出最大值,时间复杂度就会加O(N))
因为要创建一个数组中最大值长度的数组.就是将遍历数组的值对应的就是temp的角标, temp的值就是其是否遍历过.
1.准备好一个数组temp[maxnum]
2.遍历待排序数组:int sub=target-num[i];
3.如果temp[sub-1]不为0,则说明找到了一对,输出即可.将temp[sub]=0,后面若有重复的数据,可再次存储.
4.否则:将temp[nums[i]-1]++;
时间复杂度:
O(N)
空间复杂度:
O(N)—>其实是:O(maxNum)

public static void main(String[] args) {
        int[] nums = new int[]{1, 8, 9, 6, 4, 3, 7};
        int target = 10;
        method3(nums, target,9);
    }
public static void method3(int[] nums, int target, int maxNum) {
        int[] temp = new int[maxNum];
        int sub;
        for (int i = 0, size = nums.length; i < size; i++) {
            sub = target - nums[i];
            if (temp[sub - 1] > 0) {
                System.out.println(sub + "+" + nums[i] + "=" + target);
                //然后清除此位置的数据
                temp[sub - 1] = 0;
            } else {
                temp[nums[i] - 1]++;
            }
        }
    }

结果:

1+9=10
6+4=10
3+7=10

总结:

方法1是最先想到(时间复杂度比较大)
方法2是相对比较好的方法,(时间复杂度和空间复杂度相对比较适中)这个就得看排序算法选择的是否合适.
其他的排序算法:
冒泡排序,快速排序,堆排序
箱排序,基数排序
方法3是对数组的元素有一定的限制,最大值合理,是空间换时间.

如果大家有什么好的方法,希望大家可以提出来.共同学习.

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值