Leetcode——Two Sum(easy)
题目:Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]
代码:(1.22)
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {//给定两个参数nums,target
var result = new Array(2);//定义result数组存放最后结果,长度为2
for(var i = 0;i < nums.length;i++){ //横向遍历nums,如(第一遍得到i = 0,nums[0])
for(var j =i + 1;j < nums.length;j++){
//纵向遍历nums,如(上一行代码已经得到nums[0],此行代码第一遍得到nums[i + 1]即nums[1])
if (nums[i] + nums[j] == target){//在第二层循环里判断nums[i] + nums[j]是否等于target
result[0] = i;//如果等于target,则将i赋值给result[0]
result[1] = j;//将j赋值给result[1];
break;//只需要得到第一对nums[i] + nums[j]==tarfget,所以得到后跳出循环
}
}
}
return result;//返回result数组
};
Leetcode——Two Sum(easy)的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- LeetCode--Array--Two sum (Easy)
1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【Leetcode】【Easy】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode: 371 Sum of Two Integers(easy)
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- shell脚本修改文本中匹配行之前的行的方法
原创文件,欢迎阅读,禁止转载. 例子中是把 finish 前一行的 "yes" 改成 "YES"有一个方法就是利用sed+awk一起来完成. zjk@zjk:~ ...
- 字母导航跳转react核心代码
componentDidMount() { this.move(); } skipToDep(e) { let dom = document.getElementById(e); // 获取要跳至的字 ...
- python回归分析
假设原函数由一个三角函数和一个线性项组成 import numpy as np import matplotlib.pyplot as plt %matplotlib inline def f(x): ...
- Linux命令:dirs
语法 dirs [-clpv] [+N | -N] 说明 打印目录栈内容. 不带任何参数,在一行里打印.空白分隔. /home/code/dir/crypto /home/code/a/b /home ...
- 颜色空间之CIE2000色差公式
CIEDE2000色差公式 为了进一步改善工业色差评价的视觉一致性,CIE专门成立了工业色差评价的色相和明度相关修正技术委员会TC1-47(Hue and Lightness Dependent ...
- Android Studio添加aar依赖
将 implementation fileTree(dir: 'libs', include: ['*.jar']) 改为implementation fileTree(dir: 'libs', in ...
- 知识点---<input>、<textarea>
一.在pc端的input是一个大的知识点 [1]文本框 <input type="text"> [2] 密码框 <input type="passwor ...
- 使用Dockerfile自定义一个包含centos,tomcat的镜像
1.首先建立一个专用的dockerfile目录,方便统一存放将要创建的Dockerfile文件及相关资源, 例如:mkdir mydockerself 2.定位到mydockerself路径下,下载l ...
- day44 mysql高级部分内容
复习 1.多表查询 2.navicat 3.pymysql 1.视图 ***(是一个虚拟表,非真实存在的) 引子 select * from emp left join dep on emp.dep_ ...
- C语言的三目运算符(x=a?b:c):条件运算符
三目运算符使用是为了有条件判断的选择赋值 x = a ? b : c 先计算 a表达式 是否为真.若为真,x 的值便是 b表达式的值,否则 x的值便是 c表达式的值. 条件运算符是右结合的. 如:a ...