博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 1 Two Sum
阅读量:7255 次
发布时间:2019-06-29

本文共 1111 字,大约阅读时间需要 3 分钟。

Problem:

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].

Summary:

给出数组nums和目标数target,返回数组中相加可得到target的索引值。

Solution:

1 vector
twoSum(vector
& nums, int target) { 2 vector
result; 3 int flag = 0; 4 5 for(int i = 0; i < nums.size(); i++) 6 { 7 for(int j = i + 1; j < nums.size(); j++) 8 { 9 if(nums[i] + nums[j] == target)10 {11 result.push_back(i);12 result.push_back(j);13 flag = 1;14 break;15 }16 17 if(flag)18 {19 break;20 }21 }22 }23 24 return result;25 }

 

转载于:https://www.cnblogs.com/VickyWang/p/6228219.html

你可能感兴趣的文章
串行通信------字符串发送和十六进制发送
查看>>
Linux_Command
查看>>
安全DNS
查看>>
Android应用程序窗口(Activity)的视图对象(View)的创建过程分析
查看>>
android开发 java与c# 兼容AES加密
查看>>
VMware Fusion DHCP方式下如何指定虚拟机IP地址
查看>>
【XSS】延长 XSS 生命期
查看>>
Java知多少(4)J2SE、J2EE、J2ME的区别
查看>>
HDR 拍照模式的原理,实现及应用
查看>>
tiny4412 串口驱动分析九 --- shell终端
查看>>
BootStrap网格布局
查看>>
xmanager远程登录
查看>>
**Git分支管理策略
查看>>
ACdream: Sum
查看>>
Centos5, 6下更改系统时间和时区
查看>>
SDUT2165:Crack Mathmen(快速幂)
查看>>
jsonp详解
查看>>
Windows8 正式版最简单的去除桌面水印方法
查看>>
间隔定时器
查看>>
ORCFILE,ParquetFile,CubeFile使用场景区别
查看>>