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的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 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++> 给出排序好的 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  9. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. 从头学pytorch(十一):自定义层

    自定义layer https://www.cnblogs.com/sdu20112013/p/12132786.html一文里说了怎么写自定义的模型.本篇说怎么自定义层. 分两种: 不含模型参数的la ...

  2. echart环形图制作及出现的一些问题总结

    环形图的形成其实就是echarts中的饼图pie,控制饼图的内圈半径和外圈半径来形成环形的效果!下面记录的问题是在开发中出现发现的,因为在网上找到了利用阴影来做下面的图: 说明: 由于代码比较长,不能 ...

  3. selenium自动化测试入门 Alert/Confirm/Prompt 弹出窗口处理

    一.Alert/Confirm/Prompt弹出窗口特征说明 Alert弹出窗口: 提示用户信息只有确认按钮,无法通过页面元素定位,不关闭窗口无法在页面上做其他操作. Confirm 弹出窗口: 有确 ...

  4. AQS原理及应用

    To use this class as the basis of a synchronizer, redefine the * following methods, as applicable, b ...

  5. Java虚拟机OOM问题和四大引用问题简述

    一.请你谈谈实际的项目中在Java虚拟机会抛出哪些异常,每个异常都是怎么产生的? 1.java.lang.StackOverflowError 栈空间满了 public static void sta ...

  6. 【转】DB2数据库编目的概念以及对其的正确解析

    此文章主要向大家描述的是DB2数据库编目的概念以及对DB2数据库编目的概念的正确理解,在DB2中编目(catalog)这个单词看似很难理解,我自己当初在学习DB2数据库的时候也常常被这个编目搞的很不明 ...

  7. 【转】在Eclipse下搭建Android开发环境教程

    本文将全程演示Android开发环境的搭建过程,无需配置环境变量.所有软件都是写该文章时最新版本,希望大家喜欢.   一 相关下载 三 Eclipse配置 (1)Java JDK下载 1 安装andr ...

  8. 第7节class与style绑定

    方法一 效果图:  方法二 效果图:  方法三 效果图: 代码: <!DOCTYPE html> <html lang="en" xmlns:v-bind=&qu ...

  9. python中super()

    super() : 获取当前类的父类 效果图: 代码: class Animal: def __init__(self,name): self._name = name @property def n ...

  10. cogs 14. [网络流24题] 搭配飞行员 二分图最大匹配 匈牙利算法

    14. [网络流24题] 搭配飞行员 ★★   输入文件:flyer.in   输出文件:flyer.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     飞行大队有 ...