package Others;import java.util.Arrays;import java.util.Scanner;/*** To find triplet equals to given sum in complexity O(n*log(n))** <p>Array must be sorted** @author Ujjawal Joshi* @date 2020年05月18日* <p>Test Cases: Input: 6 //Length of array 12 3 4 1 6 9 target=24 Output:3 9 12 Explanation:* There is a triplet (12, 3 and 9) present in the array whose sum is 24.*/class ThreeSum {public static void main(String args[]) {Scanner sc = new Scanner(System.in);int n = sc.nextInt(); // Length of an arrayint a[] = new int[n];for (int i = 0; i < n; i++) {a[i] = sc.nextInt();}System.out.println("Target");int n_find = sc.nextInt();Arrays.sort(a); // Sort the array if array is not sortedfor (int i = 0; i < n; i++) {int l = i + 1, r = n - 1;while (l < r) {if (a[i] + a[l] + a[r] == n_find) {System.out.println(a[i] + " " + a[l] + " " + a[r]);break;} // if you want all the triplets write l++;r--; insted of break;else if (a[i] + a[l] + a[r] < n_find) l++;else r--;}}sc.close();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。