[CareerCup] 17.12 Sum to Specific Value 和为特定数
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] 17.12 Sum to Specific Value 和为特定数的更多相关文章
- careercup-中等难度 17.12
17.12 设计一个算法,找出数组中两数之和为指定值的所有整数对. 解答 时间复杂度O(n)的解法 我们可以用一个哈希表或数组或bitmap(后两者要求数组中的整数非负)来保存sum-x的值, 这样我 ...
- CodeBlocks(17.12) 代码调试基础方法&快捷方式
转载:CodeBlocks(17.12) 代码调试基础方法&快捷方式: https://www.cnblogs.com/DCD112358/p/8998053.html
- DPDK安装方法 17.12.13
DPDK安装方法 17.12.13 Ubuntu: $ git clone https://github.com/DPDK/dpdk.git $ cd dpdk/ $ export RTE_ARCH= ...
- [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 ...
- springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定
springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定 标签: springmvc springmvc学习笔记12-springmvc注解开发之包装类型參数绑定 需求 实现方 ...
- [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 ...
- [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 ...
- [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 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
随机推荐
- PMP 第十二章 项目采购管理
1规划采购 2实施采购 3管理采购 4结束采购 1.合同的不同叫法?卖方的不同叫法? 2.规划采购管理的目的是什么?合同类型有哪些?不同的特点是什么?何种情况下应选择何种合同?自制或外购分析计算和合同 ...
- 如何通过阅读C标准来解决C语言语法问题
有时候必须非常专注地阅读ANSI C标准才能找到某个问题的答案.一位销售工程师把下面这段代码作为测试用例发给Sun的编译小组. foo(const char **p) {} int main(int ...
- JAVA安装,环境变量配置
JAVA环境变量设置 PATH %JAVA_HOME%\bin JAVA_HOME D:\ProgramFiles\Java\jdk1.6.0_10 CLASSPATH .;%JAVA_HOME%\l ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- JQuery解析json数据
<script> var data ="{" + "root:[" + "{name:'1',value:'0'}," + &q ...
- Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...
- POJ 2785 HASH
题目链接:http://poj.org/problem?id=2785 题意:给定n行数字,每行4个数分别是a,b,c,d,现在要求能有多少个(a,b,c,d)组合并且和为0 思路:n^2统计所有(a ...
- iOS UIImageView设置为圆形
UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...
- 实战Hadoop中遇到的几个类、接口说明
1. Configuration :public 类型接口,这个接口包含的多数方法是进行与数据属性<key,value>有关的操作. 几个方法: 1)addProperty(String ...
- 【SAP Business Objects】Universe中的@prompt语法
@Prompt 函数的语法: @Prompt('message','type',[lov],Mono|Multi,free|constrained|primary_key,persistent|not ...