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);
        }
    }
}
目录
相关文章
|
6天前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
16 1
|
8天前
|
存储 人工智能 算法
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
这篇文章详细介绍了Dijkstra和Floyd算法,这两种算法分别用于解决单源和多源最短路径问题,并且提供了Java语言的实现代码。
34 3
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
|
10天前
|
算法
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
39 1
|
10天前
|
算法 索引
❤️算法笔记❤️-(每日一刷-141、环形链表)
❤️算法笔记❤️-(每日一刷-141、环形链表)
23 0
|
10天前
|
算法
【❤️算法笔记❤️】-(每日一刷-876、单链表的中点)
【❤️算法笔记❤️】-(每日一刷-876、单链表的中点)
29 0
|
10天前
|
算法
【❤️算法笔记❤️】-每日一刷-23、合并 K 个升序链表
【❤️算法笔记❤️】-每日一刷-23、合并 K 个升序链表
25 0
|
10天前
|
存储 算法
【❤️算法笔记❤️】-每日一刷-21、合并两个有序链表
【❤️算法笔记❤️】-每日一刷-21、合并两个有序链表
37 0
|
5天前
|
算法
每日一道算法题(Leetcode 20)
每日一道算法题(Leetcode 20)
15 2
|
9天前
|
存储 缓存 算法
经典算法之链表篇(三)
经典算法之链表篇(三)
|
9天前
|
算法
经典算法之链表篇(二)
经典算法之链表篇(二)