Leetcode 线性表 Two Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Two Sum

Total Accepted: 19206 Total
Submissions: 103959

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

题意:给定一组数和另一个数,在这组数中找两个数,使它们的和等于给定的数

思路1: --> 错,因为题目要求返回下标。

1.排序

2.两个下标,一个指向头,一个指向尾

3.如果下标指向的两个元素相加大于给定的数,尾下标减一

如果小于,头下标加一

思路2: hash

1.用hash存储每个数的下标

2.数组,看hash[target-num[i]]是否存在

复杂度:时间O(n), 空间O(n)

class Solution {
public:
    vector<int> twoSum(vector<int> &num, int target){
    	map<int, int> m;
    	vector<int> result;
    	for(int i = 0; i < num.size(); i++) m[num[i]] = i;
    	for(int i = 0; i < num.size(); i++){
    		int j = m[target - num[i]];
    		if(j && j != i){
    			result.push_back(i + 1);
    			result.push_back(j + 1);
    			return result;
    		}
    	}
    }
};

Leetcode 线性表 Two Sum

时间: 2024-10-20 07:47:40

Leetcode 线性表 Two Sum的相关文章

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be change

Leetcode 线性表 Linked List Cycle

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle Total Accepted: 17041 Total Submissions: 48975 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否有环 思路:快慢

Leetcode 线性表 数 Add Two Numbers

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Two Numbers Total Accepted: 13127 Total Submissions: 58280 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes

Leetcode 线性表 Remove Nth Node From End of List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Nth Node From End of List Total Accepted: 12160 Total Submissions: 41280 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th