题目: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)的更多相关文章

  1. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. 【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 ...

  7. 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 -. ...

  8. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  9. 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 ...

随机推荐

  1. [TensorFlow]TensorFlow安装方法

    下载*.whl文件方法安装: 方法:http://www.python36.com/install-tensorflow-using-official-pip-pacakage/ 在线安装: 方法:h ...

  2. sql 日志文件截断收缩

    use mydb ALTER DATABASE mydb SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE mydb SET RECOVERY SIMPL ...

  3. hbase的常用的shell命令&hbase的DDL操作&hbase的DML操作

    前言 笔者在分类中的hbase栏目之前已经分享了hbase的安装以及一些常用的shell命令的使用,这里不仅仅重新复习一下shell命令,还会介绍hbase的DDL以及DML的相关操作. hbase的 ...

  4. linux7 安装 zlib依赖库 与安装python 3.6

    Linux 安装zlib依赖库 进入src: cd /usr/local/src 下载zlib库: wget http://www.zlib.net/zlib-1.2.11.tar.gz 解压下载的t ...

  5. Linux命令:let

    语法 let  expr [expr ...] 说明 计算c的算术表达式.详细说明请参考<Bash参考指南-6.5 shell算术运算>

  6. 机械革命 x7ti-s 1周年使用报告

    2017年11月19日在京东入手一台机械革命x7ti-s(https://item.jd.com/5048818.html)当时各种优惠返现什么的最终8399.5到手(是1060显卡的机械版).前几周 ...

  7. Taro开发之城市选择器(带坐标)

    要写个城市选择器能返回对应的城市(这里只定义到了地级市),同时返回坐标系,参考了网上资料,下面就看看具体代码吧 import Taro, { Component } from '@tarojs/tar ...

  8. List接口特有功能

    List 有序的 有整数索引 允许重复使用 特有功能: void add(int index, E element)   //指定位置添加元素 E get()int index)            ...

  9. 认识一下margin

    标签(空格分隔): margin <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  10. echarts简单的折线图

    加jar包 <script src="<%=path %>/js/echarts.min.js"></script> 首先 在jsp页面中 选好 ...