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

    1. Two Sum

    Question

      Given an array of integers, return indices of the two numbers such that they add up to a specific target.

    You may assume that each input would have exactly one solution, and you may not use the sameelement twice.

    Example:

    Given nums = [2, 7, 11, 15], target = 9,
    
    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].
    

    Solution

    1. /** 
    2.  * @ClassName Solution 
    3.  * @Description TODO 
    4.  * @Author wangwenjie 
    5.  * @Date 2019/1/23 14:02 
    6.  **/  
    7. public class Solution {  
    8.   
    9.     public  int[] twoSum(int[] nums, int target) {  
    10.         int [] res = new int[2];  
    11.         int num = nums.length;  
    12.         for (int i=0;i<num-1;i++){  
    13.             for (int j = i+1;j<num;j++){  
    14.                 if (nums[i]+nums[j]== target){  
    15.                     res[0] = i;  
    16.                     res[1] = j;  
    17.                     break;  
    18.                 }  
    19.             }  
    20.         }  
    21.   
    22.         return res;  
    23.     }  
    24. }  
  • 相关阅读:
    Spring boot 整合mybatis
    验证码图片生成器
    Guava
    高并发环境下生成唯一流水号
    BP神经网络
    c# 利用反射获取属性名和值
    第一天开博,试试发个贴
    更改UISearchBar系统背景色方法
    IOS开发之UISearchBar应用
    textview根据文字行数自动变化大小
  • 原文地址:https://www.cnblogs.com/wwjldm/p/10336697.html
Copyright © 2011-2022 走看看