zoukankan      html  css  js  c++  java
  • LeetCode#1 Two Sum

    Problem Definition:

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

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    Solution: HashMap

    1     # @param {integer[]} nums
    2     # @param {integer} target
    3     # @return {integer[]}
    4     def twoSum(nums, target):
    5         dic={}
    6         for i, n in enumerate(nums, 1):
    7             if target-n in dic:
    8                 return [dic[target-n],i]
    9             dic[n]=i

     

  • 相关阅读:
    oracle-PL/SQL1
    ROS之Gazebo
    ROS之urdf 2
    ROS之urdf 1
    ROS 面部识别
    ROS x Arduino
    STM32F0的低功耗模式
    项目进度
    C++函数返回为引用
    STM32F0的多路ADC 无DMA
  • 原文地址:https://www.cnblogs.com/acetseng/p/4680095.html
Copyright © 2011-2022 走看看