80. Remove Duplicates from Sorted Array II

题目

分析:简单的操作,代码如下:

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(==n)
return ; int i=;
int temp;
int res = n;
vector<int> result;
int count;
for(i=;i<n;)
{
temp=nums[i];
count=;
i++;
while(i<n&&nums[i] == temp)
{
count++;
i++;
} if(count>)
{
res = res-(count-);
result.push_back(temp);
result.push_back(temp); }
else
while(count--)
{
result.push_back(temp);
} }
nums = result;
return res; }
};

---------------------------------------------------------------------------------分割线-----------------------------------------------------------------

81. Search in Rotated Sorted Array II

题目

分析:题目和33题很相识,代码如下:

 class Solution {
public:
bool search(vector<int>& nums, int target) {
int n=nums.size();
vector<int> A=nums;
if( == n) return false;
int left = ;
int right = n - ;
while(left <= right)
{
int midle = (left + right) >> ;
if(A[midle] == target) return true;
if(A[left] == A[midle] && A[midle] == A[right])
{
++left;
--right;
}
else if(A[left] <= A[midle])
{
if(A[left] <= target && target < A[midle])
{
right = midle - ;
}
else
left = midle + ;
}
else {
if(A[midle] < target && target <= A[right])
left = midle + ;
else
right = midle - ;
}
}
return false;
}
};

--------------------------------------------------------------------------------分割线-----------------------------------------------------------------

82. Remove Duplicates from Sorted List II

题目

分析:这道题主要是考察指针操作,为了方便,在处理之前,对链表添加一个头节点,以便处理起来更加方便,代码如下

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(NULL == head)
return NULL; ListNode *pre,*current;
ListNode *pHead = new ListNode();
pHead->next = head;//添加头节点
pre = pHead;
current = head;
int key;
bool flag = false;
while(current!= NULL)
{
key = current->val;
current = current->next;
while( current != NULL && current->val == key)
{
flag = true;
current = current->next;
}
if(flag)
{
pre->next = current;
flag = false;
}
else
pre = pre->next;
} return pHead->next; }
};

-------------------------------------------------------------------------分割线-------------------------------------------------------------------------

83. Remove Duplicates from Sorted List

分析:这一题和82题类似,代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if(head==NULL || head->next==NULL) return head;
ListNode *helper = new ListNode(-);
ListNode *ret=head;
while(ret)
{
ListNode *next=ret->next;
if(ret->val!=helper->val)
{
helper->next=ret;
helper=ret;//将helper指新链表的尾结点
helper->next=NULL;//尾指向空,因为后面的结点有可能被删去了,它不知道下一个指向谁
}
else delete ret;
ret=next;
}
return head;
}
};

Leetcode题解(26)的更多相关文章

  1. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  2. [LeetCode 题解]: Maximum Subarray

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the c ...

  3. [LeetCode 题解]: plusOne

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a no ...

  4. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  5. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  6. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  7. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  8. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  10. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

随机推荐

  1. Dubbo与Zookeeper、SpringMVC整合和使用

    作为dubbo框架初学者,能让框架跑起来非常不容易,非常感谢网上诸多大神提供的文章,本人参考文章地址是:https://my.oschina.net/xshuai/blog/891281 不过别人的记 ...

  2. 转自知乎(JAVA后台开发可以纯粹用JAVA SE吗?)

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:巴多崽链接:http://www.zhihu.com/question/29663744/answer/45154839来源: ...

  3. RAID及热备盘详解

    RAID,为Redundant Arrays of Independent Disks的简称,中文为廉价冗余磁盘阵列. 一.出现的原因(RAID的优点): 它的用途主要是面向服务器,但现在的个人电脑由 ...

  4. 第4章 同步控制 Synchronization ----事件(Event Objects)

    Win32 中最具弹性的同步机制就属 events 对象了.Event 对象是一种核心对象,它的唯一目的就是成为激发状态或未激发状态.这两种状态全由程序来控制,不会成为 Wait...() 函数的副作 ...

  5. 使用JS动态修改网页body的背景色

    大部分网页默认的背景色为白色,个人感觉比较刺眼,于是写了个JS的脚本去改变body部分的背景色,代码如下: // ==UserScript== // @name ChangeBackgroundCol ...

  6. 关于el-dialog,我更推荐的用法

    最近的项目里用上了vue和element-ui.vue这种轻量级渐进式框架的舒适自不必说,但一直困扰着我的,是如何方便又优雅的弹出模态dialog... 对于我这种在jquery出现之前就用docum ...

  7. 五年.net程序员转型Java之路

    大学毕业后笔者进入一家外企,做企业CRM系统开发,那时候开发效率最高的高级程序语言,毫无疑问是C#.恰逢公司也在扩张,招聘了不少.net程序员,笔者作为应届生,也乐呵呵的加入到.net程序员行列中. ...

  8. python之串口操作

    1.安装pyserial linux上直接安装: #python2 sudo pip install pyserial #或者python3 sudo pip3 install pyserial Wi ...

  9. jsp web JavaBean MVC 架构 EL表达式 EL函数 JSTL

     一.JavaBean概念(非常重要) 1.JavaBean就是遵循一定书写规范的Java类型(开发中:封装数据) a.必须有默认的构造方法,类必须是public的   public class  ...

  10. 使用dropload.js插件进行下拉刷新

    移动端的下拉刷新是一个比较常见的功能了,网上也有很多框架,插件都有这种功能,所以直接拿来用就好了. html代码: <!--选项卡--><div class="tab&qu ...