Leetcode 1 two sum 难度:0
https://leetcode.com/problems/two-sum/
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- vector<int> index(2,0);
- vector<int> n2;
- int numsSize = nums.size();
- int f = -1;
- for(int i = 0;i < numsSize;i++){
- n2.push_back(nums[i]);
- if(nums[i] == target/2 && target%2 ==0){
- if(f != -1){
- index[0] = min(f,i + 1);
- index[1] = max(f,i + 1);
- return index;
- }
- else f = i + 1;
- }
- }
- sort(nums.begin(),nums.end());
- for(int i = 0;i < numsSize;i++){
- int j = lower_bound(nums.begin(),nums.end(),target - nums[i]) - nums.begin();
- int a,b;
- if(nums[i] + nums[j] == target && j < numsSize){
- for(int k = 0;k < numsSize;k++){
- if(nums[i] == n2[k]) a = k + 1;
- if(nums[j] == n2[k]) b = k + 1;
- }
- index[0] = min(a,b);
- index[1] = max(a,b);
- }
- }
- return index;
- }
- };
Leetcode 1 two sum 难度:0的更多相关文章
- [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
随机推荐
- 使用 Redis 实现排行榜功能 (转载 https://segmentfault.com/a/1190000002694239)
排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如"用户积分榜".如果没有实效性一直按照总榜来排,可能榜 ...
- web工程目录结构
/WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...
- EventBus使用详解(一)——初步使用EventBus
一.概述 EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间 ...
- css 打字动画
使用 css 将文字逐字打出 <h1>css is awesome</h1> 要使<h1>标签里的文字逐字打出,对应的样式如下: h1{ width: 14ch;/ ...
- nginx 配置文件备份
1. /etc/nginx/sites-enabled/default 的原始文件 # You may add here your # server { # ... # } # statements ...
- stoneniqiu 理想就是自己喜欢做,并对社会和他人都有意义的事情!
H5嵌入原生开发小结----兼容安卓与ios的填坑之路 2016-11-19 08:29 by stoneniqiu, 1038 阅读, 8 评论, 收藏, 编辑 一开始听说开发H5,以为就是做适配现 ...
- android 修改framework下资源文件后如何编译
在framework/base/core/res/res 下添加资源文件后需要先编译资源 然后编译framework 才可正常引用 进入项目根目录 cd frameworks/base/core/re ...
- php 面向对象中的魔术方法
1.__construct() 实例化对象是被自动调用.当__construct和以类名为函数名的函数 同时存在时调用__construct,另一个不背调用. 类名为函数名的函数为老版的构造函数. 2 ...
- 事务码 ListSchema:查看Cube星型结构Schema
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- aws在线技术峰会笔记-电商解决方案
Redshift PB级别的数据仓库