Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
注 :给定一个链表,交换每两个节点,返回首指针。要在常数空间内完成算法不能修改链表值,只能改变节点之间的链接。
解:竟然可以输入奇数个,无语。
if (head==)
{return head;}
ListNode *backer, *fronter, *temp;
//游标定位
backer = head;
if (head->next == NULL){ return head; }//针对只输入一个的情况
fronter = head->next;
temp = fronter->next;//可以有,也可以是NULL
//进行交换
head = fronter;
fronter->next = backer;
if (temp != NULL && temp->next!=NULL)
{
backer->next = temp->next;
}
else if (temp != NULL && temp->next == NULL)
{
backer->next = temp;
return head;
}
else { backer->next = NULL; return head; }
while (temp != NULL)
{
//移动游标
backer = temp; fronter = temp->next;
temp = fronter->next;
//进行交换
fronter->next = backer;
if (temp != NULL && temp->next != NULL)
{
backer->next = temp->next;
}
else if (temp != NULL && temp->next == NULL)
{
backer->next = temp;
return head;
}
else { backer->next = NULL; return head; }
}
return head;
26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
注:对于一个排好序的数组,移除其中重复的元素,使得每个元素只出现一次,然后返回新的长度。不能使用额外的空间。这个数组一定是越变越小的,所以只要恰当的使用指针移动数组中的数据应该可以不占用额外的空间。
解:这里新学会了v.erase(iter)的使用
if (nums.size() == )
return ;
vector<int>::iterator nums_i = nums.begin(), nums_j = nums.begin() + ;
while (nums_i != nums.end() && nums_j != nums.end())
{
if (*nums_i == *nums_j)
{
nums_j=nums.erase(nums_j);//删除后者j并返回j后一个元素的指针,如果没有就返回end()指针
}
else{
nums_i++; nums_j++;
}
}
return nums.size();
Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
随机推荐
- requests爬取梨视频主页所有视频
爬取梨视频步骤: 1.爬取梨视频主页,获取主页所有的详情页链接 - url: https://www.pearvideo.com/ - 1) 往url发送请求,获取主页的html文本 - 2) 解析并 ...
- UNITY->(width*height)style Inventory
项目过后对项目功能进行记录,(width*height)风格背包实现细节,包含对物体的存放,装备,替换,对未知装备的鉴定,物体前缀的获取,项目类型为tcg+rpg,背包的作用主要为游戏中的物品的获取存 ...
- DEVOPS技术实践_08:声明式管道语法
简介 前面简单的做了管道的实验,看了一下的它的效果 声明式管道是Groovy语法中的一个更简单和结构化的语法.下面主要学习明式管道语法. 一 声明式管道的基本结构 以上节的代码为例 node { de ...
- tensorflow之tf.train.exponential_decay()指数衰减法
exponential_decay(learning_rate, global_steps, decay_steps, decay_rate, staircase=False, name=None) ...
- .NET Core 3.1之深入源码理解HealthCheck(二)
写在前面 前文讨论了HealthCheck的理论部分,本文将讨论有关HealthCheck的应用内容. 可以监视内存.磁盘和其他物理服务器资源的使用情况来了解是否处于正常状态. 运行状况检查可以测试应 ...
- SpringBoot基础架构篇1(SpringBoot、MyBatis-Plus与Thymeleaf)
show me the code and talk to me,做的出来更要说的明白 我是布尔bl,你的支持是我分享的动力! 1 引入 使用 MyBatis-Plus 以及 thymeleaf 实现增 ...
- GC 为什么要挂起用户线程? 什么愁什么怨?
GC 为什么要挂起用户线程? 什么愁什么怨? 前言 JVM 系列文章的第一篇.敬请期待后续. 故障描述 某年某月某日 上午,线上发生故障,经过排查,发现某核心服务 Dubbo 接口超时. 故障根源 查 ...
- 来吧,一文彻底搞懂Java中最特殊的存在——null
没事的时候,我并不喜欢逛 P 站,而喜欢逛 programcreek 这些技术型网站,于是那天晚上,在夜深人静的时候,我就发现了一个专注基础但不容忽视的主题.比如说:Java 中的 null 到底是什 ...
- MySQL故障演习
MySQL故障演习 接上次的 MySQL定时备份 该次实验主要是练习在MySQL数据库发生误删等意外情况下,利用全量备份文件和增量备份文件恢复数据. 1. 实验环境 -- 创建数据库 create d ...
- Java之IO流用法总结
Java的IO流概述:1.I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输.如读/写文件,网络通讯等.2.Java程序中,对于数据的输入/输出操作以“流( ...