15. 三数之和

  1. class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. List<List<Integer>> list = new ArrayList<>();
  4. if(nums==null || nums.length==0){
  5. return list;
  6. }
  7. Arrays.sort(nums);
  8. // 固定一个指针 i
  9. for(int i=0;i<nums.length;i++){
  10. if(i>0 && nums[i]==nums[i-1]){
  11. continue;
  12. }
  13. int left = i+1;
  14. int right = nums.length-1;
  15. // 更新指针 left和 right:[-1,0,1,2,-1,-4]
  16. while (left<right){
  17. int sum = nums[i]+nums[left]+nums[right];
  18. if(sum==0){
  19. list.add(Arrays.asList(nums[i],nums[left],nums[right]));
  20. while (left<right && nums[left]==nums[left+1]){
  21. left++;
  22. }
  23. while (left<right && nums[right]==nums[right-1]){
  24. right--;
  25. }
  26. left++;
  27. right--;
  28. }else if(sum>0){
  29. right--;
  30. }else if(sum<0){
  31. left++;
  32. }
  33. }
  34. }
  35. return list;
  36. }
  37. }

912. 排序数组

  1. class Solution {
  2. public int[] sortArray(int[] nums) {
  3. if(nums==null || nums.length==0){
  4. return new int[0];
  5. }
  6. int left = 0;
  7. int right = nums.length-1;
  8. quickSort(nums,left,right);
  9. return nums;
  10. }
  11. private void quickSort(int[] nums, int left, int right) {
  12. if(left>right){
  13. return;
  14. }
  15. int index = getIndex(nums,left,right);
  16. quickSort(nums,left,index-1);
  17. quickSort(nums,index+1,right);
  18. }
  19. private int getIndex(int[] nums, int left, int right) {
  20. int temp = nums[left];
  21. while (left<right){
  22. while (left<right && nums[right]>=temp){
  23. right--;
  24. }
  25. nums[left]=nums[right];
  26. while (left<right && nums[left]<=temp){
  27. left++;
  28. }
  29. nums[right] = nums[left];
  30. }
  31. nums[left] = temp;
  32. return left;
  33. }
  34. }

53. 最大子数组和

参考题解:https://leetcode.cn/problems/maximum-subarray/solution/dong-tai-gui-hua-fen-zhi-fa-python-dai-ma-java-dai/

  1. class Solution {
  2. public int maxSubArray(int[] nums) {
  3. if(nums==null || nums.length==0){
  4. return 0;
  5. }
  6. int[] dp = new int[nums.length];
  7. dp[0] = nums[0];
  8. int max = nums[0];
  9. for(int i=1;i<nums.length;i++){
  10. // 如果dp[i-1]>0,那么加上后以dp[i] 结尾的连续子数组之和会变大,否则会变小舍弃
  11. if(dp[i-1]>0){
  12. dp[i] = dp[i-1]+nums[i];
  13. }else {
  14. dp[i] = nums[i];
  15. }
  16. max = Math.max(dp[i],max);
  17. }
  18. return max;
  19. }
  20. }

21. 合并两个有序链表

  1. class Solution {
  2. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  3. ListNode dummy = new ListNode(0);
  4. ListNode cur = dummy;
  5. while (list1!=null && list2!=null){
  6. if(list1.val<=list2.val){
  7. cur.next = list1;
  8. list1 = list1.next;
  9. cur = cur.next;
  10. }else{
  11. cur.next = list2;
  12. list2 = list2.next;
  13. cur = cur.next;
  14. }
  15. }
  16. if(list1!=null){
  17. cur.next = list1;
  18. }
  19. if(list2!=null){
  20. cur.next = list2;
  21. }
  22. return dummy.next;
  23. }
  24. }

1. 两数之和

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. if(nums==null || nums.length==0){
  4. return new int[]{-1,-1};
  5. }
  6. HashMap<Integer,Integer> map = new HashMap<>();
  7. for(int i=0;i<nums.length;i++){
  8. if(map.containsKey(target-nums[i])){
  9. return new int[]{map.get(target-nums[i]),i};
  10. }
  11. map.put(nums[i],i);
  12. }
  13. return new int[]{-1,-1};
  14. }
  15. }