Leetcode80 删除有序数组中的重复项Ⅱ
给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
示例 1:
输入:nums = [1,1,1,2,2,3]
输出:5, nums = [1,1,2,2,3]
解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
昨天刚做了个双指针专场,今天就又是一道双指针题目,删除重复项,使每项元素最多出现两次,可以用一个指针向前走查找元素,另一个指针输入
class Solution {
public int removeDuplicates(int[] nums) {
int len = nums.length;
int i = 0, j = 0;
int remain;
while(i<len){
remain = 2;
while(i+1<len && nums[i+1]==nums[i]){
i++;
remain--;
if(remain>0){
nums[j++] = nums[i];
}
}
nums[j++] = nums[i++];
}
return j;
}
}
接下来是几道链表题目
剑指offer6 从尾到头打印链表
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
这一题解法很多,可以使用栈依次保存输出,也可以直接遍历两遍,第一遍获得长度length,第二遍填入元素,我就用了简单的方法。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
int len = 0;
ListNode now = head;
while(now != null){
len++;
now = now.next;
}
int[] res = new int[len--];
now = head;
while(now != null){
res[len--] = now.val;
now = now.next;
}
return res;
}
}
剑指offer18 删除链表的元素
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
这是一道和数据结构有关的题目,删除链表中的元素,只需要让pre.next = now.next即可
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val){
return head.next;
}
ListNode pre = head;
ListNode now = head.next;
while(now!=null && now.val!=val){
pre = now;
now = now.next;
}
pre.next = now.next;
return head;
}
}
剑指offer22 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。
示例:
给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
这算是一类很经典的题目,标准解法就是使用快慢指针,使快指针在慢指针前k的位置,当快指针指向null,则慢指针指向的结点为倒数第k个结点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode now = head;
ListNode pre = head;
for(int i = 0; i < k; i++){
now = now.next;
}
while(now!=null){
pre = pre.next;
now = now.next;
}
return pre;
}
}
剑指offer24 反转链表
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
这一题也是有很多种解法,可以使用栈来依次保存各个结点,然后反向链接,但是这样的空间复杂度为O(n)。可以利用数据结构的知识,使用三个指针pre,now,next,令now.next = pre,即可实现。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return head;
}
ListNode pre = null;
ListNode now = head;
ListNode next = now.next;
while(next!=null){
now.next = pre;
pre = now;
now = next;
next = next.next;
}
now.next = pre;
return now;
}
}
剑指offer25 合并两个排序的链表
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
与合并两个有序数组一样,使用双指针即可。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode index1 = l1, index2 = l2;
if(l1 == null){
return l2;
}else if(l2 == null){
return l1;
}
ListNode head;
if(index1.val<index2.val){
head = index1;
index1 = index1.next;
}else{
head = index2;
index2 = index2.next;
}
ListNode pre = head;
while(index1!=null && index2!=null){
if(index1.val<index2.val){
pre.next = index1;
pre = index1;
index1 = index1.next;
}else{
pre.next = index2;
pre = index2;
index2 = index2.next;
}
}
if(index1!=null){
pre.next = index1;
}else{
pre.next = index2;
}
return head;
}
}
剑指offer35 复杂链表的复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
示例 1:
输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
这一题,第一想法就是使用hash表,存储下来已经创立的结点,这样做的时间复杂度O(n),那么有没有时间复杂度为O(1)的解法呢,答案是有的,这里给大家提供一种。
第一步,在每个节点的后面插入复制的节点。
第二步,对复制节点的 random 链接进行赋值。
第三步,拆分
public RandomListNode Clone(RandomListNode pHead) {
if (pHead == null)
return null;
// 插入新节点
RandomListNode cur = pHead;
while (cur != null) {
RandomListNode clone = new RandomListNode(cur.label);
clone.next = cur.next;
cur.next = clone;
cur = clone.next;
}
// 建立 random 链接
cur = pHead;
while (cur != null) {
RandomListNode clone = cur.next;
if (cur.random != null)
clone.random = cur.random.next;
cur = clone.next;
}
// 拆分
cur = pHead;
RandomListNode pCloneHead = pHead.next;
while (cur.next != null) {
RandomListNode next = cur.next;
cur.next = next.next;
cur = next;
}
return pCloneHead;
}
剑指offer52 两个链表的第一个公共结点
输入两个链表,找出它们的第一个公共节点。
如下面的两个链表:
在节点 c1 开始相交。
这是一道虐狗题,我们使用两个指针 node1,node2 分别指向两个链表 headA,headB 的头结点,然后同时分别逐结点遍历,当 node1 到达链表 headA 的末尾时,重新定位到链表 headB 的头结点;当 node2 到达链表 headB 的末尾时,重新定位到链表 headA 的头结点。
这样,当它们相遇时,所指向的结点就是第一个公共结点。
参考亮评:
你变成我,走过我走过的路。
我变成你,走过你走过的路。
然后我们便相遇了…
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode l1 = headA;
ListNode l2 = headB;
if(l1 == null || l2 == null){
return null;
}
while(l1.next != null){
l1 = l1.next;
}
while(l2.next != null){
l2 = l2.next;
}
if(l1 != l2){
return null;
}
l1 = headA;
l2 = headB;
while(l1!=l2){
l1 = l1.next == null ? headB : l1.next;
l2 = l2.next == null ? headA : l2.next;
}
return l1;
}
}