欢迎使用CSDN-markdown编辑器

来源:互联网 发布:仿卷皮源码 编辑:程序博客网 时间:2024/06/08 17:24

Leetcode No.1 Two Sum

-Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

给定一个数组,和一个target,求这个数组中 和为target的两个数的位置。


代码块

代码块语法遵循标准markdown代码,例如:

class Solution {public:    vector<int> twoSum(vector<int>& nums, int target) {        unordered_map<int, int> map;        int n = (int)nums.size();        for (int i = 0; i < n; i++) {            auto p = map.find(target-nums[i]);            if (p!=map.end()) {                return {p->second+1, i+1};            }            map[nums[i]]=i;        }    }};
0 0
原创粉丝点击