面试题(6)之 leetcode-001
1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
实现1:
/**
* @method twoSum
* @param {number[]} nums 数组
* @param {number} target 数字
* @return {number[]} indexes 下标
*/
function twoSum(nums, target) {
for (let i = 0; i < nums.length; i++) {
const findNum = target - nums[i];
const findIndex = nums.indexOf(findNum);
if(findIndex > -1 && findIndex !== i) {
return [i, findIndex];
}
}
};
实现2: for循环 嵌套 for循环
面试题(6)之 leetcode-001的更多相关文章
- [leetCode][001] Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [Leetcode][001] Two Sum (Java)
题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- Leetcode 001. 两数之和(扩展)
1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...
- leetcode 001
1 Two Sum Difficulty: Easy The Link: https://leetcode.com/problems/two-sum/description/ Description ...
- 【JAVA、C++】LeetCode 001 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- two Sum ---- LeetCode 001
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode 001 Two Sun
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介
目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...
- [转]T-SQL_面试题
[转]T-SQL_面试题 2015-05-19 1 创建表插入数据 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,s ...
随机推荐
- 使用loadrunner监控apcahe资源
一般要修改的内容在Httpd.conf文件中已经存在,如果不存在请自行添加相应内容. (1)修改Apache中Httpd.conf文件, (2)添加ExtendedStatus,设置ExtendedS ...
- python2学习------基础语法4(模块)
1.整体结构层次(a.py,b.py) 目标:b.py文件中导入a.py里面定义的class A,并调用A类里面的属性或相关方法. 2.模块导入 <1> __init__.py < ...
- mac下添加环境变量
1.环境变量相关文件说明: a. /etc/profile b. /etc/paths c. ~/.bash_profile d. ~/.bash_login e. ~/.profile f. ~/. ...
- Prometheus Operator 架构【转】
本节讨论 Prometheus Operator 的架构.因为 Prometheus Operator 是基于 Prometheus 的,我们需要先了解一下 Prometheus. Prometheu ...
- k8s解析service地址方式
[root@k8s-master ~]# dig -t A kubernetes.default.svc.cluster.local. @10.96.0.10 ; <<>> D ...
- vs2010编译C++ 静态成员函数的引用
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using names ...
- hello程序的运行过程-从计算机系统角度
hello程序的运行过程-从计算机系统角度 1.gcc编译器驱动程序读取源程序文件hello.c,并将它翻译成一个可执行目标文件hello.翻译过程分为四个阶段:预处理阶段,编译阶段,汇编阶段,链接阶 ...
- Day4 - D - Watchcow POJ - 2230
Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...
- 用ftp命令实现主机文件批量更新
我们的主机环境是windows 2003,平时程序员访问都喜欢用远程桌面.简单快捷直观.不过我比较喜欢在本地用vim和命令行,这样编辑修改不需要受网络影响. 这种情况下,我本地调试的程序,要经常更新到 ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...