17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified value.

这道题实际上跟LeetCode上的Two Sum很类似,但是不同的是,那道题限定了只有一组解,而这道题说可以有很多组符合要求的解,那么我们先来看一种使用哈希表的解法,这种解法的时间复杂度是O(n),空间复杂度是O(1),思路是用哈希表建立每个数字和其下标之间的映射,遍历整个数字,如果target减去当前数字的值在哈希表中存在,那么返回这一对结果,然后更新当前数字在哈希表中的映射值,参见代码如下:

解法一:

void find_pairs(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = ; i < nums.size(); ++i) {
if (m.count(target - nums[i])) {
cout << nums[i] << " " << nums[m[target - nums[i]]] << endl;
}
m[nums[i]] = i;
}
}

下面这种方法是利用了双指针的思路,我们首先要把数组排个序,然后左右两个指针向中间移动,如果当前两个指针指的数加起来正好等于target,则找到了一对结果,如果大于target,那么我们将右指针左移一位,这样值能减小一些,如果和小于target,则把左指针右移一位,这样和能增大一些,参见代码如下:

解法二:

void find_pairs(vector<int> nums, int target) {
sort(nums.begin(), nums.end());
int left = , right = nums.size() - ;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
cout << nums[left] << " " << nums[right] << endl;
++left; --right;
} else if (sum > target) {
--right;
} else {
++left;
}
}
}

CareerCup All in One 题目汇总

[CareerCup] 17.12 Sum to Specific Value 和为特定数的更多相关文章

  1. careercup-中等难度 17.12

    17.12 设计一个算法,找出数组中两数之和为指定值的所有整数对. 解答 时间复杂度O(n)的解法 我们可以用一个哈希表或数组或bitmap(后两者要求数组中的整数非负)来保存sum-x的值, 这样我 ...

  2. CodeBlocks(17.12) 代码调试基础方法&快捷方式

    转载:CodeBlocks(17.12) 代码调试基础方法&快捷方式: https://www.cnblogs.com/DCD112358/p/8998053.html

  3. DPDK安装方法 17.12.13

    DPDK安装方法 17.12.13 Ubuntu: $ git clone https://github.com/DPDK/dpdk.git $ cd dpdk/ $ export RTE_ARCH= ...

  4. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定

    springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定 标签: springmvc springmvc学习笔记12-springmvc注解开发之包装类型參数绑定 需求 实现方 ...

  6. [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  7. [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

    18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...

  8. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  9. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

随机推荐

  1. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  2. Android打包的那些事

    使用gradle打包apk已经成为当前主流趋势,我也在这个过程中经历了各种需求,并不断结合gradle新的支持,一一改进.在此,把这些相关的东西记录,做一总结. 1. 替换AndroidManifes ...

  3. SpringMVC解析3-DispatcherServlet组件初始化

    在spring中,ContextLoaderListener只是辅助功能,用于创建WebApplicationContext类型实例,而真正的逻辑实现其实是在DispatcherServlet中进行的 ...

  4. iOS开发-由浅至深学习block

    关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想到在C函数中,我们可以定义一个指向函数的指针并且调用 ...

  5. Java List与数组之间的转换

    http://blog.csdn.net/kingzone_2008/article/details/8444678

  6. Oracle 查询性能优化实践

      1.索引使用原则   不要对索引使用全模糊,但是 LIKE 'asdf%'是可以的,即不要Contains,可用StartWith 不要对索引进行函数,表达式操作,或者使用is null判断,否则 ...

  7. Zookeeper 分布式环境搭建

    一.前期环境 安装概览 IP Host Name     Software     192.168.23.128     ae01 JDK 1.7 192.168.23.129 ae02 JDK 1. ...

  8. java基础-变量

    浏览以下内容前,请点击并阅读 声明 java中的变量分为四种: 实例变量(非静态字段):一个java类中没有static关键词修饰的字段 类变量(静态字段):一个java类中带有static关键词修饰 ...

  9. Linux下统计高速网络中的流量

    netpps.sh统计每秒数据量,包含接收(RX)或发送(TX) netpps.sh eth0 #!/bin/bash INTERVAL=" # update interval in sec ...

  10. Haskell 笔记 ①

    ①一切都是函数,包括常量.表达式,格式:名字 参数1 参数2.. =函数内容 ②if语句(else绝对不可以省略) F=if (..) then x else y ③没有数组,只有列表[1,2,3,4 ...