【leetcode】1. Two Sum

在一个数组中找到和等于target的两个数的位置

按样例输入是乱序的,只能用n^2解法

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length - 1; ++i) {
            int left = nums[i];
            for (int j = i + 1; j < nums.length; ++j) {
                int right = nums[j];
                if (left + right == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }
}

 

posted @ 2016-04-10 00:16  -六月飞雪-  阅读(147)  评论(0编辑  收藏  举报