Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一题 Two Sum 不符合预期结果 #1

Open
johnsonzheng0824 opened this issue Apr 8, 2018 · 0 comments
Open

第一题 Two Sum 不符合预期结果 #1

johnsonzheng0824 opened this issue Apr 8, 2018 · 0 comments

Comments

@johnsonzheng0824
Copy link

算法本身没问题,改一下比较好

public static int[] twoSum(int[] numbers, int target)
{
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++)
        {
            if (map.containsKey(target - numbers[i]))
            {
                result[1] = i;
                result[0] = map.get(target - numbers[i]);
                return result;
            }
            map.put(numbers[i], i);
        }
        return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant