• LintCode-两数之和


    题目描述:

      给一个整数数组,找到两个数使得他们的和等于一个给定的数target。

      你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是1到n,不是以0开头。

     注意事项

      你可以假设只有一组答案。

    样例

      numbers=[2, 7, 11, 15],  target=9

      return [1, 2]

     1 public class Solution {
     2     /*
     3      * @param numbers : An array of Integer
     4      * @param target : target = numbers[index1] + numbers[index2]
     5      * @return : [index1 + 1, index2 + 1] (index1 < index2)
     6      */
     7     public int[] twoSum(int[] numbers, int target) {
     8        int[] res = new int[2];
     9          for(int i=0;i<numbers.length;i++){
    10              for(int j=i+1;j<numbers.length;j++){
    11                  if(numbers[i]+numbers[j]==target){
    12                      res[0] = i+1;
    13                      res[1] = j+1;
    14                      return res;
    15                  }
    16              }
    17          }
    18         return res;
    19     }
    20 }
  • 相关阅读:
    spring
    ajax
    jquary
    Java web 部分
    长跑马拉松
    面试的标准
    数据预处理——剔除异常值,平滑,归一化
    概率分布
    养生
    平滑的作用
  • 原文地址:https://www.cnblogs.com/xiaocainiao2hao/p/5364702.html
Copyright © 2020-2023  润新知