LeetCode-1:Two Sum

xiaoxiao2021-02-28  92

在牛客网刷了一些算法题,许多的算法题覆盖面不广,于是转战LeetCode,希望能提高自己的编程能力。

1.Two Sum

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 same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. 题意: 在一个整形数组(无序)中快速找出两个数字,使得两个数字之和等于一个给定的值。假设数组中不会出现两组数据和等于指定数字。 public class Solution { public int[] twoSum(int[] nums, int target) { for(int i=0;i<nums.length-1;i++){ for(int j=i+1;j<nums.length;j++){ if(nums[i]+nums[j]==target){ int result[]={i,j}; return result; } } } return null; } }
转载请注明原文地址: https://www.6miu.com/read-53655.html

最新回复(0)