leetcode算法题解(Java版)-13-经典反转链表

一、简单二分搜索

题目描述

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

思路

  • 题目很简单,二分就能通过

代码

public class Solution {
    public int searchInsert(int[] A, int target) {
        //二分查找
        int left = 0;
        int right = A.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(A[mid]==target){
                return mid;
            }
            else if(A[mid]<target){
                left = mid+1;
            }
            else{
                right = mid-1;
            }
        }
        return left;
    }
}

二、反转链表

题目描述

Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given1->2->3->4->5->NULL, m = 2 and n = 4,
return1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

思路

  • 经典的题目,值得反复推敲。设置两个指针,一个指向m的位置,一个指向m之前的那个位置,不断刷新这两个指针的next,达到反转的效果。
  • 注意,这两个指针本身不变化,变化的是他们的next.
  • 这样做,满足了题意中的:do it in-place and in one-place

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode start = head;
        ListNode prestart = dummy;
        for(int i=1;i<m;i++){
            prestart = start;
            start = start.next;
        }
        
        ListNode tem;
        for(int i=0;i<n-m;i++){
            tem = start.next;
            start.next = tem.next;
            tem.next = prestart.next;
            prestart.next = tem;
        }
        return dummy.next;
    }
}

三、深搜

题目描述

Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.

For example,
If S =[1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

思路

  • 题目要列出所有不重复的,用所给数组中数字组成的非递减数列。
  • 深搜解决问题:注意到不能有重复的数字,之前好像做过类似的题目,今天又碰到了,也就是要让拿的数字不能和已经拿过的一样。if(start>i&&num[i]==num[i-1]

代码

import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
        
        if(num==null||num.length==0){
            return res;
        }
        Arrays.sort(num);
        findAll(num,0,new ArrayList<>());
        return res;
    }
    public void findAll(int [] num,int start,ArrayList<Integer> list){
        res.add(new ArrayList<Integer>(list));
        
        for(int i=start;i<num.length;i++){
            if(i>start&&num[i]==num[i-1]){
                continue;
            }
            list.add(num[i]);
            findAll(num,i+1,list);
            list.remove(list.size()-1);
        }
    }
}
上一篇:leetcode算法题解(Java版)-15-动态规划(斐波那契)


下一篇:【资料下载】开发者如何全面提升职场竞争力系列课程第二讲——开发者如何申请技术专利