- 兩數之和
-
題目:
給定一個整數數組和一個目標值,找出數組中和爲目標值的兩個數。
你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。
示例:
給定 nums = [2, 7, 11, 15], target = 9
因爲 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
思路:
方法一:可以用暴力法進行破解,用雙層循環遍歷nums。一層遍歷第一個加數,add2=target-nums[i], 從i之後進行遍歷,如果存在add2,就返回這兩個加數的索引值。
代碼
public int[] twoSum(int[] nums, int target) {
int[] result=new int[2];
for(int i=0;i<nums.length;i++)
{
int add2=target-nums[i];
for(int j=i+1;j<nums.length;j++)
{
if(nums[j]==add2)
{
result[0]=i;
result[1]=j;
return result;
}
}
}
return result;
}
方法二:可以藉助HashMap,將nums加入到HashMap表中,其中nums中的數作爲key,索引作爲value, 判斷Hashmap中存在target-x且這個數的索引值不等於x自身即可。
代碼
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
- 三數之和
題目
給定一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。
注意:答案中不可以包含重複的三元組。
例如, 給定數組 nums = [-1, 0, 1, 2, -1, -4],
滿足要求的三元組集合爲:
[
[-1, 0, 1],
[-1, -1, 2]
]
思路:
跟兩數之和不同的是,三數之後要輸出的是不同三元組的集合。因此,我們考慮先將nums進行排序,將nums[i]作爲第一個加數,從i+1到nums.length-1之間初始化兩個指針left,right,爲了避免有重複的情況,當nums[i]==nums[i-1],說明有重複的情況,開始下一個循環。如果num[i]+num[left]+num[right]>0,說明加多了,讓right–,如果num[i]+num[left]+num[right]<0,說明加少了,讓left++,如果等於0,說明符合條件,將這一組解加到集合中,這是也應該避免第二個加數和第三個加數重複的情況。
代碼
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] + nums[i] > 0) {
right--;
} else if (nums[left] + nums[right] + nums[i] < 0) {
left++;
} else {
list.add(Arrays.asList(nums[left], nums[right], nums[i]));
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right + 1]) {
right--;
}
}
}
}
return list;
}
- 四數之和
題目
給定一個包含 n 個整數的數組 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。
注意:
答案中不可以包含重複的四元組。
示例:
給定數組 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
滿足要求的四元組集合爲:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
思路
解法和三數之和一樣,但是多加了一層循環。
代碼
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
left++;
} else if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
right--;
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left],
nums[right]));
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right + 1]) {
right--;
}
}
}
}
}
return result;
}