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. springboot前后端分离项目redis做验证码及用户信息存储验证长时间不操作失效问题解决

    1.错误回显:Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: MISCO ...

  2. HashMap、lru、散列表

    HashMap HashMap的数据结构:HashMap实际上是一个数组和链表("链表散列")的数据结构.底层就是一个数组结构,数组中的每一项又是一个链表. hashCode是一个 ...

  3. 洛谷$P5329\ [SNOI2019]$字符串 字符串

    正解:字符串 解题报告: 传送门$QwQ$ 有两个很妙的方法,分别港下$QwQ$ 首先为了表示方便,这里和题面一样设$s_i$表示去掉第$i$个字母得到的字符串.另设$lcp(i,j)$表示$suf_ ...

  4. 洛谷$P2598\ [ZJOI2009]$狼和羊的故事 网络流

    正解:网络流 解题报告: 传送门! 昂显然考虑最小割鸭$QwQ$,就考虑说每个土地要么属于羊要么属于狼,然后如果一条边上是栅栏一定是相邻两边所属不同. 所以考虑给所有羊向$S$连$inf$,所有狼向$ ...

  5. HDU5179 beautiful number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题目大意: 给你一个数 \(A = a_1a_2 \cdots a_n\) ,我们称 \(A\) ...

  6. 设置文本框的 placeholder 的颜色

    使用方法: 选择器类型::-webkit-input-placeholder input::-webkit-input-placeholder{ color:rgba(144,147,153,1); ...

  7. SpringBoot项目的代理机制【一】

    这是了解Spring代理机制的第一篇,尝试了解Spring如何实现Bean的注册和代理.这篇文章会抛出问题:Spring注册Bean,都会用Jdk代理或cglib创建代理对象吗? 1 项目准备 1.1 ...

  8. OAuth2.0概念以及实现思路简介

    一.什么是OAuth? OAuth是一个授权规范,可以使A应用在受限的情况下访问B应用中用户的资源(前提是经过了该用户的授权,而A应用并不需要也无法知道用户在B应用中的账号和密码),资源通常以REST ...

  9. C++ | C++ 概览 基础知识 | 01

    一.基本概念 1.1 类型.变量和算术运算 1.2 常量 1.3 检验和循环 1.4 指针,数组和循环 二.用户自定义类型 2.1 结构 2.2 类 2.3 枚举 三.模块化 3.1 分离编译 3.2 ...

  10. Scala实践10

    1.模式匹配 模式匹配是一种根据模式检查值的机制.它是switch(Java中语句)的更强大版本,它同样可以用来代替一系列if / else语句. 句法 匹配表达式具有值,match关键字和至少一个c ...