Skip to content

Latest commit

 

History

History
37 lines (30 loc) · 802 Bytes

2019-10-02-LeetCode-TwoSum.md

File metadata and controls

37 lines (30 loc) · 802 Bytes
layout title gh-repo gh-badge tags
post
LeetCode 1. 两数之和
youcoding98/youcoding98.github.io
star
fork
follow
leetcode

题目

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

题解之暴力法:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int l = nums.length;
        int[] a = new int[2];
        for (int i = 0; i < l; i++) {
            for(int j = i+1;j < l;j++)
            {
                if(nums[i] + nums[j] == target)
                {
                    a[0] = i; a[1] = j;
                    break;
                }
            }
        }
        return a;


    }
}

解题思路