leetcode第321题解题争取每天做一题LeetCode上的题,坚持就是胜利!题目:给定一个数组和一个目标数值,求数组内两数相加之和等于目标数值的下标。假定必有一组解。示例:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]。解题思路如下:遍历数组,判断是否有nums[j] = target - nums[i]。public int[] twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[j] == target - nums[i]) { return new int[]{i, j}; } } } throw new IllegalArgumentException("No two sum solution"); }