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"); }
leetcode第321题 LeetCode解题
文件列表
LeetCode-master.zip
(预估有个15文件)
LeetCode-master
.gitignore
22B
src
Q1TwoSum.java
1KB
Q14LongestCommonPrefix.java
831B
Q3LongestSubstringWithoutRepeatingCharacters.java
1KB
Q9PalindromeNumber.java
1KB
Q7ReserveInteger.java
755B
Q4MedianofTwoSortedArrays.java
3KB
Q2AddTwoNumbers.java
2KB
暂无评论