leet code Two Sum

/*
 * Example:
 * Given nums = [2, 7, 11, 15], target = 9,

    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].

 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */

function twoSum(nums, target) {
  var len = nums.length;
  var indexnumber = [];
  for( var i = 0; i < len; i ++ ){
    var sub = target - nums[i]
    for( var j = 0; j < len; j ++ ){
      if( j != i && nums[j] == sub ){
        indexnumber.push(i,j)
      }
    }
  }
  var newnumbers = Array.from(new Set(indexnumber))
  var result = [];
  for( var i = 0 ; i < newnumbers.length ; i += 2 ){
    result.push(newnumbers.slice( i, i + 2 ))
  }
  return result
};
时间: 2024-12-31 05:46:04

leet code Two Sum的相关文章

#Leet Code# Root to leaf

语言:Python 描述:使用递归实现 1 def getList(self, node): 2 if node is None: 3 return [] 4 5 if node.left is None and node.right is None: 6 return [[node.val]] 7 8 result = [] 9 for item in self.getList(node.left): 10 result.append([node.val] + item) 11 12 for

#Leet Code# Divide Two Integers

描述:不使用 * / % 完成除法操作.O(n)复杂度会超时,需要O(lg(n))复杂度. 代码: 1 class Solution: 2 # @return an integer 3 def dividePositive(self, dividend, divisor): 4 if dividend < divisor: 5 return 0 6 7 sum = divisor 8 count = 1 9 while sum + sum < dividend: 10 sum += sum 1

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

leet code Sort List

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leet code Sort List 对链表使用快慢指针归并排序 Sort List Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-lin

#Leet Code# Sqrt

描述:log(n) 代码: 1 class Solution: 2 # @param x, an integer 3 # @return an integer 4 def getVal(self, begin, end, x): 5 if end == begin : 6 return begin 7 if end == begin + 1: 8 return begin 9 10 while True: 11 mid = (begin + end) / 2 12 tmp = mid * mid

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

#Leet Code# Gray Code

描述: 要求相邻数2进制差一位 先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了 代码: 1 class Solution: 2 # @return a list of integers 3 def grayCode(self, n): 4 if n == 0: return [0] 5 6 s1 = self.grayCode(n - 1) 7 s2 = [item