1:两数之和(新增)
This commit is contained in:
parent
78a264eeb3
commit
9f1ecd6435
@ -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) {
|
||||||
|
// int start = 0;
|
||||||
|
// int end = nums.length - 1;
|
||||||
|
// int[] result = new int[2];
|
||||||
|
// if (nums.length == 2) {
|
||||||
|
// result[0] = start;
|
||||||
|
// result[1] = end;
|
||||||
|
// } else {
|
||||||
|
// List<Integer> list = new ArrayList();
|
||||||
|
// for (int i = 0; i < nums.length; 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) {
|
public int[] twoSum(int[] nums, int target) {
|
||||||
|
int[] arrs = new int[2];
|
||||||
int start = 0;
|
int start = 0;
|
||||||
int end = nums.length - 1;
|
int end = nums.length - 1;
|
||||||
int[] result = new int[2];
|
List<Integer> list = new ArrayList<>();
|
||||||
if (nums.length == 2) {
|
for (int num : nums) {
|
||||||
result[0] = start;
|
list.add(num);
|
||||||
result[1] = end;
|
|
||||||
} else {
|
|
||||||
List<Integer> list = new ArrayList();
|
|
||||||
for (int i = 0; i < nums.length; i++) {
|
|
||||||
list.add(nums[i]);
|
|
||||||
}
|
}
|
||||||
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]) {
|
return arrs;
|
||||||
int temp = result[0];
|
|
||||||
result[0] = result[1];
|
|
||||||
result[1] = temp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
Loading…
Reference in New Issue
Block a user