Leetcode题解(26)
80. Remove Duplicates from Sorted Array II
题目
分析:简单的操作,代码如下:
1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int n = nums.size(); 5 if(0==n) 6 return 0; 7 8 int i=0; 9 int temp; 10 int res = n; 11 vector<int> result; 12 int count; 13 for(i=0;i<n;) 14 { 15 temp=nums[i]; 16 count=1; 17 i++; 18 while(i<n&&nums[i] == temp) 19 { 20 count++; 21 i++; 22 } 23 24 if(count>2) 25 { 26 res = res-(count-2); 27 result.push_back(temp); 28 result.push_back(temp); 29 30 } 31 else 32 while(count--) 33 { 34 result.push_back(temp); 35 } 36 37 } 38 nums = result; 39 return res; 40 41 } 42 };
---------------------------------------------------------------------------------分割线-----------------------------------------------------------------
81. Search in Rotated Sorted Array II
题目
分析:题目和33题很相识,代码如下:
1 class Solution { 2 public: 3 bool search(vector<int>& nums, int target) { 4 int n=nums.size(); 5 vector<int> A=nums; 6 if(0 == n) return false; 7 int left = 0; 8 int right = n - 1; 9 while(left <= right) 10 { 11 int midle = (left + right) >> 1; 12 if(A[midle] == target) return true; 13 if(A[left] == A[midle] && A[midle] == A[right]) 14 { 15 ++left; 16 --right; 17 } 18 else if(A[left] <= A[midle]) 19 { 20 if(A[left] <= target && target < A[midle]) 21 { 22 right = midle - 1; 23 } 24 else 25 left = midle + 1; 26 } 27 else { 28 if(A[midle] < target && target <= A[right]) 29 left = midle + 1; 30 else 31 right = midle - 1; 32 } 33 } 34 return false; 35 } 36 };
--------------------------------------------------------------------------------分割线-----------------------------------------------------------------
82. Remove Duplicates from Sorted List II
题目
分析:这道题主要是考察指针操作,为了方便,在处理之前,对链表添加一个头节点,以便处理起来更加方便,代码如下
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* deleteDuplicates(ListNode* head) { 12 if(NULL == head) 13 return NULL; 14 15 ListNode *pre,*current; 16 ListNode *pHead = new ListNode(0); 17 pHead->next = head;//添加头节点 18 pre = pHead; 19 current = head; 20 int key; 21 bool flag = false; 22 while(current!= NULL) 23 { 24 key = current->val; 25 current = current->next; 26 while( current != NULL && current->val == key) 27 { 28 flag = true; 29 current = current->next; 30 } 31 if(flag) 32 { 33 pre->next = current; 34 flag = false; 35 } 36 else 37 pre = pre->next; 38 } 39 40 return pHead->next; 41 42 } 43 };
-------------------------------------------------------------------------分割线-------------------------------------------------------------------------
83. Remove Duplicates from Sorted List
分析:这一题和82题类似,代码如下:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution 10 { 11 public: 12 ListNode *deleteDuplicates(ListNode *head) 13 { 14 if(head==NULL || head->next==NULL) return head; 15 ListNode *helper = new ListNode(-100000); 16 ListNode *ret=head; 17 while(ret) 18 { 19 ListNode *next=ret->next; 20 if(ret->val!=helper->val) 21 { 22 helper->next=ret; 23 helper=ret;//将helper指新链表的尾结点 24 helper->next=NULL;//尾指向空,因为后面的结点有可能被删去了,它不知道下一个指向谁 25 } 26 else delete ret; 27 ret=next; 28 } 29 return head; 30 } 31 };
转载于:https://www.cnblogs.com/LCCRNblog/p/5177685.html
Leetcode题解(26)相关推荐
- 算法与数据结构+LeetCode题解-Js版
LeetCode题解Js版 https://webbj97.github.io/leetCode-Js/ 题外话 LeetCode题解:传送门 前端笔记:传送门 项目背景 旨在提高自己对算法的理解,将 ...
- 900 多道 LeetCode 题解,这个 GitHub 项目值得 Star!
转自 | 码农有道 大家好,我是小 G. 周末风和日丽,适合刷 LeetCode 今天给你们推荐个 GitHub 项目,里面收集了 900 多道 LeetCode 题解,并包含中英文两个版本,适合大多 ...
- PHP版Leetcode题解开始随缘更新
2019独角兽企业重金招聘Python工程师标准>>> PHP版Leetcode题解 我叫skys215,是一名bug工程师. 我接触编程的时间比较早,但是因为我数学不好加上比较懒, ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...
- LeetCode 题解汇总
为什么80%的码农都做不了架构师?>>> LeetCode 题解汇总 转载于:https://my.oschina.net/michao/blog/801863
- leetcode题解【持续更新】
leetcode题解不断更新,以及nowcoder题解.一起加油! 完整请移步我的Github 转载于:https://juejin.im/post/5c8a73d8f265da2de80fa774
- 32位有符号整数_008. 字符串转换整数 (atoi) | Leetcode题解
点击上方"蓝色字体",选择"设为星标" 每天复习一道面试题,轻松拿大厂Offer~ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...
- leetcode题解132-分割回文串 II
问题描述 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文. 返回符合要求的 最少分割次数 . 示例 1: 输入:s = "aab" 输出:1 解释:只需一次分割 ...
- leetcode题解131-分割回文串
问题描述 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 .返回 s 所有可能的分割方案. 回文串 是正着读和反着读都一样的字符串. 示例 1: 输入:s = "aa ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a roman ...
最新文章
- ABAP將數字輸出前面補0
- C++ using namespace
- NET问答: Log4Net 无法将日志写入到 log 文件的求助.....
- AliOS Things SIG BLE Mesh网络的介绍和搭建
- zw版_zw中文增强版Halcon官方Delphi例程
- denali vip使用经验
- Kettle构建Hadoop ETL实践(一):ETL与Kettle
- java支付方法_java实现微信H5支付方法详解
- java 字符串转pdf_Java pdf转String 并修正格式
- 45台计算机的网络拓扑图,网络复习习题
- 计算机控制系统刘恩沧课后题答案,第八章2 提高计算机控制系统可靠性的技术措施_内蒙古农业大学:计算机控制技术_ppt_大学课件预览_高等教育资讯网...
- 失物招领网站html源码,基于web的失物招领系统(完整源码+论文全套+教学视频)...
- 院士评选2012中国世界十大科技进展新闻揭晓
- 解决 Elasticsearch 分页查询记录超过10000时异常
- 生活-仙剑3的另类台词
- 北京地铁客流数据特征分析
- PS制作CSS精灵图
- 索尼xzp升级android p,索尼XZP国行版升级安卓8.0 相机功能优化
- 详细说明热轧圆钢测径仪选择需要注意哪些
- 图灵学院Java架构师五期笔记