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 ...
随机推荐
- 线程的通信与协作:sleep、wait、notify、yield、join关系与区别
一.sleep.join.yield.wait.notify.notifyAll 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也 ...
- JAVA8之 Stream 流(四)
如果说前面几章是函数式编程的方法论,那么 Stream 流就应该是 JAVA8 为我们提供的最佳实践. Stream 流的定义 Stream 是支持串行和并行操作的一系列元素.流操作会被组合到流管道中 ...
- TensorFlow之tf.less()
函数:tf.less less( x, y, name=None ) 以元素方式返回(x <y)的真值. 注意:Less支持广播. 参数: x:张量.必须是下列类型之一:float32,floa ...
- 【题解】JXOI2018游戏(组合数)
[题解]JXOI2018游戏(组合数) 题目大意 对于\([l,r]\)中的数,你有一种操作,就是删除一个数及其所有倍数.问你删除所有数的所有方案的步数之和. 由于这里是简化题意,有一个东西没有提到: ...
- Spring Security详解
Spring Security 一. 简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文 ...
- 曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 【已解决】CentOS7使用yum安装Docker显示错误:cannot find a valid baseurl for repo: base/7/x86_64
不得不说,Docker 要求 CentOS 系统的内核版本高于 3.10,这就让有些人开始头疼了,而要查看具体的版本可以用以下命令 uname -r 当然,CentOS 6.8版本也能安装Docker ...
- 微信小程序吸顶功能
---------------------------HTML------------------------ <view class="navbar-wrap"> ...
- Qt 下QMessageBox下中文乱码问题
Qt版本 Qt Creator 2.4.1 Based on Qt 4.8.0 (64 bit) 现象 QMessageBox调用setText()一直是乱码 解决方法 QTextCodec::set ...
- 如何制作地图故事使用esri story maps
博客作者原创 制作方法如下:http://url.cn/5dnsVQd