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 ...
随机推荐
- 正基AP6212固件
需要正基技术资料联系我 QQ507014762 电话17666215391 #AP6212_NVRAM_V1.0.1_20160606 2.4 GHz, 20 MHz BW modeThe fo ...
- 006 管理Ceph的RBD块设备
一, Ceph RBD的特性 支持完整和增量的快照 自动精简配置 写时复制克隆 动态调整大小 二.RBD基本应用 2.1 创建RBD池 [root@ceph2 ceph]# ceph osd pool ...
- promethues安装
prometheus 1. 下载安装 下载安装:https://github.com/prometheus/prometheus/releases/tag/v2.9.2 wget https://gi ...
- 1094 谷歌的招聘 (20 分)C语言
2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com 结尾的网址,而前面的网址是一个 10 位素数,这个素数是自然常数 e ...
- 1082 射击比赛 (20 分)C语言
本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟.我们假设靶心在原点(0,0). 输入 ...
- [推荐]icheck-bootstrap(漂亮的ckeckbox/radiobox)
适用于Twitter Bootstrap框架的纯CSS样式的复选框/单选框按钮的插件. GitHub:https://github.com/bantikyan/icheck-bootstrap 如果你 ...
- 用 Serverless 快速搭建个人相册网站
日常生活中我们经常会拍摄一些视频.照片等,这些文件会占用比较多的存储空间.本文将介绍一种方法:利用 ThumbsUp 工具,结合 Serverless Framework 的 component 快速 ...
- P2722 总分 Score Inflation (完全背包模板)
题目传送门:P2722 总分 Score Inflation 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时 ...
- 如何对N个接口按比例压测
随着微服务盛行,公司的服务端项目也越来越多.单一的接口性能测试并不能准确反映某个服务的总体处理能力,在服务功能划分比较清晰的架构下,对于某一服务的总体性能测试也相对变得简单.下面分享一个对于某个模块对 ...
- 【转】推荐!国外程序员整理的Java资源大全
构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...