leetcode #1 Two Sum
来源:互联网 发布:软件使用许可证模板 编辑:程序博客网 时间:2024/06/11 19:06
URL: https://leetcode.com/problems/two-sum/
题目描述:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
第一次做leetcode上的题,虽然这是第一道,难度也只是Medium,但能一次性通过,可谓“出师告捷”,挺高兴的!题目本来是很简单的(要是不卡时间的话),看到这题,第一反应当然是两层循环,可这样时间复杂度就 n2 了。这道题是要想办法把复杂度降到 n 的,于是,我牺牲了很大的数组空间,用于记录每个数据的下标,本来还担心卡空间呢,结果顺利通过了,真是个惊喜!代码如下:
Language:cpp Run Time:19 ms
#define MAX_INT 0x7FFF#define MIN_INT 0x8000class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { int max = MIN_INT, min = MAX_INT; for(int i = 0; i < numbers.size(); i++){ max = max > numbers[i] ? max : numbers[i]; min = min < numbers[i] ? min : numbers[i]; } max = max > (target - min) ? max : (target - min); min = min < (target - max) ? min : (target - max); int index_lenth = max - min + 1; int index_table[index_lenth]; memset(index_table, 0xff, sizeof(index_table)); int first_index = -1, second_index = 0; while(second_index < numbers.size()){ first_index = index_table[target - numbers[second_index] - min]; if(first_index != -1) break; index_table[numbers[second_index] - min] = second_index; second_index ++; } vector<int> anser; anser.push_back(first_index + 1); anser.push_back(second_index + 1); return anser; }};
- LeetCode 1 - Two Sum
- leetcode 1 Two Sum
- Leetcode【1】:Two Sum
- [leetcode 1] Two Sum
- 【leetcode-1】Two Sum
- [leetcode 1] Two Sum
- [Leetcode] 1 - Two Sum
- LeetCode (1) Two Sum
- LeetCode 1:《Two Sum》
- LeetCode | #1 Two Sum
- leetcode-1 Two Sum
- Two Sum | LeetCode(1)
- [Leetcode]1Two Sum
- leetcode 1 Two Sum
- leetcode #1 Two Sum
- leetcode 1:Two Sum
- LeetCode 1 Two Sum
- leetcode #1 two sum
- 利用WebSphere Edge Server构建冷轧系统负载均衡
- UVa 806 - Spatial Structures(DFS)
- Delphi IDHTTP用法详解
- OpenCL-- 统计给定单词在文本中出现次数
- C语言 编写程序数一下1到100的所有整数中出现多少次数字9
- leetcode #1 Two Sum
- 转载04:CSS盒子模型
- Delphi 的绘图功能
- 典型的ELF可重定位目标文件
- hdu 4508 湫湫系列故事——减肥记I 完全背包。
- 机房收费系统(个人重构)——存储过程
- ListView 可以滑动的上下间距
- 使用Eclipse 编译含有多个main函数的项目
- 黑马程序员--java基础--Object类中的几个方法、包的概念