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. 第二十一篇:SOUI中的控件注册机制

    Win32编程中,用户需要一个新控件时,需要向系统注册一个新的控件类型.注册以后,调用::CreateWindow时才能根据标识控件类型的字符串创建出一个新的控件窗口对象. 为了能够从XML描述的字符 ...

  2. Vector[C++]

    //    vector<int> vec; //    for(int i = 0; i < 10; i++) //    { //        vec.push_back(5) ...

  3. 在Salesforce中对Object实现Trigger的绑定

    Trigger的相关属性详细解读请看如下链接: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_c ...

  4. Vue#计算属性

    在模板中表达式非常便利,但是它们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护.这就是为什么 Vue.js 将绑定表达式限制为一个表达式.如果需要多于一 ...

  5. SQL SERVER数据库的表中修改字段的数据类型后,不能保存

      在数据库里面建了一个表,可是由于对SQL SERVER的建表功能不熟悉,不知道把主键设成什么是好,就先设置了个TEXT类型,可是后来朋友们告诉我说,TEXT类型容易让数据文件变得很大,还 是改成一 ...

  6. Liferay 6.2 改造系列之十五:修改默认可用语言

    在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Specify the locales that are enabled ...

  7. HTTP 请求方式: GET和POST的比较(转)

    GET和POST是HTTP的两个常用方法.   什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 ...

  8. asp中 grideview 更新 无法获取值 解决办法

    string str1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim(); 来 ...

  9. 对NLP的一些新认识

    其实这是老板让上交的一份总结,贴出来,欢迎朋友们批评指正. 最近看了一部分关于NLP的几篇论文,其中大部分为神经网络实现, 从基本的HMM算法实现,到LSTM实现,有很多方法可以用来处理NLP任务中的 ...

  10. js-错误处理与调试,JSON

    错误处理与调试: 1.try-catch try{ window.someNoneXistentFunction(); }catch(error){ alert(error.message) } 2. ...