Leetcode题解(26)
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)的更多相关文章
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- [LeetCode 题解]: Maximum Subarray
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Find the c ...
- [LeetCode 题解]: plusOne
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a no ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...
随机推荐
- String类的常见面试题(3)
1.判断定义为String类型的s1和s2是否相等 String s1 = "abc"; //这个"abc"对象首先会进常量池 String s2 = &quo ...
- 用 Python 撸一个区块链
本文翻译自 Daniel van Flymen 的文章 Learn Blockchains by Building One 略有删改.原文地址:https://hackernoon.com/learn ...
- GCD hdu2588
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- vue2购物车ch1-(安装依赖、简单配置、 axios获取api的模拟数据)
0--项目说明 说明此项目源自某课网购物车教程,但是在开发过程中,发现在开发过程中用的还是 vue-resource(宣布不更新的类$.ajx()插件),为了以后的发展使用axios.js,详情参考 ...
- 有关 Hybrid 开发模式实践总结
前言 随着公司业务不断发展,移动开发项目越来越多,项目任务时间紧,我们内部开发流程是以项目为导向,有别于一般公司对产品不断迭代的做法,但移动端开发人员资源有限,需要在不同项目之间做业务场景切换开发,就 ...
- 最近做的floyd的题目
基础: HDU1596 HDU2112 HDU1874 HDU1869 HDU2066 HDU2094 HDU2544 稍加复杂: HDU1217 ...
- 学习札记 ----wind7下如何安装SqlServer数据库
1.控制面板 ---找到程序和功能选项 如下图所示: 2.打开程序和功能后进入如下图所示的界面,点击打开或关闭window功能. 3.启动window7自带的IIS功能.如下图所示: 4.如上动作准备 ...
- commons-pool与commons-pool2连接池
commons-pool和commons-pool2是用来建立对象池的框架,提供了一些将对象池化必须要实现的接口和一些默认动作.对象池化之后可以通过pool的概念去管理其生命周期,例如对象的创建,使用 ...
- 开源API集成测试工具 Hitchhiker v0.2更新 - 压力测试
Hitchhiker 是一款开源的 Restful Api 集成测试工具,支持Schedule, 数据对比,压力测试,可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http: ...
- dotweb框架之旅 [三] - 常用对象-HttpServer
dotweb属于一个Web框架,希望通过框架行为,帮助开发人员快速构建Web应用,提升开发效率,减少不必要的代码臃肿. dotweb包含以下几个常用对象: App(dotweb) App容器,为Web ...