1:两数之和(新增)

This commit is contained in:
huangge1199 2021-10-13 12:09:28 +08:00
parent 78a264eeb3
commit 9f1ecd6435
2 changed files with 57 additions and 32 deletions

View File

@ -45,54 +45,79 @@
package leetcode.editor.cn; package leetcode.editor.cn;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
//1:两数之和 //1:两数之和
public class TwoSum{ public class TwoSum {
public static void main(String[] args) { public static void main(String[] args) {
//测试代码 //测试代码
Solution solution = new TwoSum().new Solution(); Solution solution = new TwoSum().new Solution();
} }
//力扣代码 //力扣代码
//leetcode submit region begin(Prohibit modification and deletion) //leetcode submit region begin(Prohibit modification and deletion)
class Solution { class Solution {
public int[] twoSum(int[] nums, int target) { // public int[] twoSum(int[] nums, int target) {
int start = 0; // int start = 0;
int end = nums.length - 1; // int end = nums.length - 1;
int[] result = new int[2]; // int[] result = new int[2];
if (nums.length == 2) { // if (nums.length == 2) {
result[0] = start; // result[0] = start;
result[1] = end; // result[1] = end;
} else { // } else {
List<Integer> list = new ArrayList(); // List<Integer> list = new ArrayList();
for (int i = 0; i < nums.length; i++) { // for (int i = 0; i < nums.length; i++) {
list.add(nums[i]); // list.add(nums[i]);
// }
// Arrays.sort(nums);
// while (nums[start] + nums[end] != target) {
// if (nums[start] + nums[end] > target) {
// end--;
// } else {
// start++;
// }
// }
// result[0] = list.indexOf(nums[start]);
// list.remove(result[0]);
// result[1] = list.indexOf(nums[end]);
// if (result[1] >= result[0]) {
// result[1]++;
// }
// if (result[0] > result[1]) {
// int temp = result[0];
// result[0] = result[1];
// result[1] = temp;
// }
// }
// return result;
// }
public int[] twoSum(int[] nums, int target) {
int[] arrs = new int[2];
int start = 0;
int end = nums.length - 1;
List<Integer> list = new ArrayList<>();
for (int num : nums) {
list.add(num);
} }
Arrays.sort(nums); Arrays.sort(nums);
while (nums[start] + nums[end] != target) { while (start < end) {
if (nums[start] + nums[end] > target) { if (nums[start] + nums[end] < target) {
start++;
} else if (nums[start] + nums[end] > target) {
end--; end--;
} else { } else {
start++; break;
} }
} }
result[0] = list.indexOf(nums[start]); arrs[0] = list.indexOf(nums[start]);
list.remove(result[0]); list.remove(arrs[0]);
result[1] = list.indexOf(nums[end]); arrs[1] = list.indexOf(nums[end]);
if (result[1] >= result[0]) { if (arrs[1] >= arrs[0]) {
result[1]++; arrs[1]++;
}
if (result[0] > result[1]) {
int temp = result[0];
result[0] = result[1];
result[1] = temp;
} }
return arrs;
} }
return result;
} }
}
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)
} }

File diff suppressed because one or more lines are too long