题解 | #不重复打印排序数组中相加和为给定值的所有二元组#

不重复打印排序数组中相加和为给定值的所有二元组

http://www.nowcoder.com/practice/1ff1a607c81748baa1823ffa687d74c4

public class Main {

public static void printUniquePair(int[] arr, int k) {
	if (arr == null || arr.length < 2) {
		return;
	}
	int left = 0;
	int right = arr.length - 1;
	while (left < right) {
		if (arr[left] + arr[right] < k) {
			left++;
		} else if (arr[left] + arr[right] > k) {
			right--;
		} else {
			if (left == 0 || arr[left - 1] != arr[left]) {
				System.out.println(arr[left] + "," + arr[right]);
			}
			left++;
			right--;
		}
	}
}


public static void main(String[] args) {
	int sum = 10;
	int[] arr1 = { -8, -4, -3, 0, 1, 2, 4, 5, 8, 9 };
	printArray(arr1);
	System.out.println("====");
	printUniquePair(arr1, sum);

}

}

全部评论

相关推荐

07-09 18:28
门头沟学院 Java
写着提前批,结果还要实习4个月以上???
程序员牛肉:这种不用看,直接投了,面试的时候问对应的HR就行。有可能他们是直接复制的暑期实习的模板。
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
07-09 15:55
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务