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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值