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 ...
随机推荐
- logback kafkaAppender输出日志到kafka
官网地址https://github.com/danielwegener/logback-kafka-appender 本文以spring boot项目为基础,更多的信息,请参考官网 https:// ...
- Container and Injection in Java
一.Container 1.为什么使用Container 通常,瘦客户端多层应用程序很难编写,因为它们涉及处理事务和状态管理.多线程.资源池和其他复杂的低级细节的复杂代码行.基于组件和独立于平台的Ja ...
- Windows下javac不可用,java -version可以
https://blog.csdn.net/kobedir/article/details/79709287
- Java框架spring Boot学习笔记(十):传递数据到html页面的例子
新建一个templates文件夹和index.html <!DOCTYPE html> <html> <head lang="en"> < ...
- php+javascript实现的动态显示服务器运行程序进度条功能示例
本文实例讲述了php+javascript实现的动态显示服务器运行程序进度条功能.分享给大家供大家参考,具体如下: 经常有这样的业务要处理,服务器上有较多的业务需要处理,需要分批操作,于是就需要一个提 ...
- Golang学习---常用库
1. 路由库:github.com/julienschmidt/httprouter 2. mysql驱动:github.com/go-sql-driver/mysql
- jmeter在几个固定的字符串中,随机取其中之一的方法
在测试过程中遇到上送字段必需是几个固定值中的一个, 使用读取文件中几个固定值,然后随机在这几个固定值中选择的办法解决问题 __CSVRead() CSV file to get values from ...
- SQL 2008发布与订阅
网的教程很多,大都是不能成功,只有这一篇是成功的! https://www.cnblogs.com/DBArtist/p/5803271.html
- Spring @Value取值为null或@Autowired注入失败
@Value 用于注入.properties文件中定义的内容 @Autowired 用于装配bean 用法都很简单,很直接,但是稍不注意就会出错.下面就来说说我遇到的问题. 前两天在项目中遇到了一个问 ...
- 洛谷1027 Car的旅行路线
原题链接 将每个城市拆成四个点,即四个机场来看,那么这题就是求最短路. 不过建图有些麻烦,先要找出第四个机场的坐标. 设另外三个机场的坐标为\((x_1, y_1), (x_2, y_2), (x_3 ...