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, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].

记录第一个LeetCode题, c++的STL库不会用, (╥╯^╰╥)

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

1_Two Sum的更多相关文章

  1. 1_Two Sum --LeetCode

    原题如下: 思路:将nums放到一个map<int,int>中,其中,键是nums中元素,值对应其下标.然后遍历nums,取nums中一个值nums[i],接着用target减去它,最后再 ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. 大话Python类语义

    类 物以类聚,人以群分,就是相同特征的人和事物会自动聚集在一起,核心驱动点就是具有相同特征或相类似的特征,我们把具有相同特征或相似特征的事物放在一起,被称为分类,把分类依据的特征称为类属性 计算机中分 ...

  2. Python 3.9 新特性速览

    国庆假期,Python 社区发布了 3.9 版本的第一个 stable release. 相比于 3.8,Python 3.9 新特性众多,但不少特性与大多数 Python"使用者" ...

  3. mysql字段大小写敏感设置

    mysql中varchar类型的字符集一般设置成utf8,然而mysql默认是对大小写不敏感(不区分),如果想要mysql区分大小写需要设置排序规则,规则详解如下:在mysql中存在着各种排序规则:1 ...

  4. Linux设备驱动中的阻塞和非阻塞I/O <转载>

    Green 博客园 首页 新随笔 联系 订阅 管理 Linux设备驱动中的阻塞和非阻塞I/O   [基本概念] 1.阻塞 阻塞操作是指在执行设备操作时,托不能获得资源,则挂起进程直到满足操作所需的条件 ...

  5. .NET Core+MongoDB集群搭建与实战

    目录 安装 MongoDB apt 直接安装(方法1) apt 仓库安装(方法2) 方法1.2启动 MongoDB 通过二进制包安装(方法3) 安装依赖 deb 安装 MongoDB tgz 安装 M ...

  6. CentOS 8 安装 oniguruma 和 oniguruma-devel

    一,oniguruma是什么? oniguruma是一个处理正则表达式的库,我们之所以需要安装它, 是因为在安装php7.4的过程中,mbstring的正则表达式处理功能对这个包有依赖性, 所以我们要 ...

  7. centos8上安装ImageMagick6.9.10并压缩图片生成webp缩略图

    一,ImageMagick的作用: ImageMagick 是一个用来创建.编辑.合成图片的软件. 它可以读取.转换.写入多种格式的图片. 功能包括:图片切割.颜色替换.各种效果的应用, 图片的旋转. ...

  8. React.Component 和 React.PureComponent 、React.memo 的区别

    一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...

  9. 超简单集成HMS ML Kit文字超分能力,一键提升文本分辨率

    前言 大家有没有遇到过这种情况,在浏览微博或者公众号时看到一段有趣的文字,于是截图发到朋友圈想和好友分享.但是在发布图片时,软件会对图片强制进行压缩,导致图片分辨率下降,文字变得模糊难以阅读.那么有没 ...

  10. 千万不要往 Shell 里粘贴命令!

    对于用惯了 IDE 的程序员来说,在终端里敲命令可能没那么顺手,也记不住那么多复杂的命令.比较偷懒的做法就是网上搜相关的命令,复制到剪贴板往命令行窗口里一贴,完事! 但是这么做有很大的风险,为什么呢? ...