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 ...
随机推荐
- Pytorch_01 Tensor,Autograd,构建网络
Tensor Tensor是PyTorch中的重要数据结构,可认为是一个高维数组,Tensor与numpy的ndarrays类似,但Tensor可以使用GPU加速 import torch as t# ...
- python练习题_03
1.写函数: 根据范围获取其中3和7整除的所有数的和,并返回调用者: 符合条件的数字个数以及符合条件的数字的总和,如:def func(start,end): def func(start,end): ...
- Python 学习笔记02篇
之前很庆幸早点报名课程 可以早点看到视频讲解,不至于后期太赶 不得不说原本21天压缩到14天再压缩到7天,如果可以完整且独立地完成至少三个作业,那水平应该真的很不错吧
- MySQL多实例的环境下,服务器端本地连接到指定实例的问题(sock方式连接)
涉及到sock连接的问题. 为了测试MySQL的某些个特性,在一个机器上安装了多个MySQL的实例,如下截图,有两个实例,一个端口是8000,一个端口是8001.在使用mysql -uroot -p ...
- Oracle使用学习笔记(二)_Sql语句
一.Sql语句的分类 数据操作语言,简称DML(data manipulation language),如增加,删除,修改,查询数据等 数据定义语言,简称DDL(data defination lan ...
- Django中的auth模块
from django.contrib import auth authenticate() 提供了用户认证功能,即验证用户名以及密码是否正确,一般需要username .password两个关键字参 ...
- angluar1时间控件,在浏览器里是没有问题的,但是真机时间报错NAN
因为是老项目并且用的angluar1有时也会很头疼没法直接打包和手机联调,所以浏览器上测试的多但是真机和浏览器还是不一样的,废话不说了,看图吧 我的时间插件代码是这样的 后台返回的数据格式是这样的 , ...
- 第二周博客作业 <西北师范大学| 周安伟>
一,本周助教小结 逐步开始适应助教工作,对学生发布的博客进行点评,查看学生对软件工程前期的准备情况. 二,助教本人博客 https://home.cnblogs.com/u/zaw-315/ 三,学生 ...
- 数组引用:C++ 数组做参数 深入分析
转载:https://blog.csdn.net/jiangxinyu/article/details/7767065 在 C++中,数组永远不会按值传递,它是传递第一个元素,准确地说是第 0个 的指 ...
- UnitZ Battlegrounds beta5 - Unity吃鸡类型游戏模版 源码 仿绝地求生
Requires Unity 2018.2.6 or higher.The first battle royale game starter kit on Asset Store, all syste ...