Leetcode题解(26)
2016-02-02 15:31
639 查看
80. Remove Duplicates from Sorted Array II
题目
分析:简单的操作,代码如下:
class Solution { public: int removeDuplicates(vector<int>& nums) { int n = nums.size(); if(0==n) return 0; int i=0; int temp; int res = n; vector<int> result; int count; for(i=0;i<n;) { temp=nums[i]; count=1; i++; while(i<n&&nums[i] == temp) { count++; i++; } if(count>2) { res = res-(count-2); 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(0 == n) return false; int left = 0; int right = n - 1; while(left <= right) { int midle = (left + right) >> 1; 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 - 1; } else left = midle + 1; } else { if(A[midle] < target && target <= A[right]) left = midle + 1; else right = midle - 1; } } 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(0); 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(-100000); 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; } };
相关文章推荐
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- 如何设置停止更新支持的ubuntu更新源
- ffmpeg编译
- Android动态加载布局
- sp<> wp<>-Android中定义了两种智能指针强指针sp和弱指针wp
- hdu3342 Legal or Not(简单拓扑排序判定)
- 哈理工OJ 1643 多边形-2【极角排序+凸凹包判断】
- C#实现树的双亲表示法
- 学无止境,学习AJAX(一)
- mysql的账号管理
- Quartz1.8.5例子(六)
- Error: Linux下 mysql.sock文件丢失被删除解决方法
- 推荐几个数据分析网站
- RUST语法
- 为已有的web project项目加入maven支持,并使用myeclipse的插件.
- spring mvc+ mybaties+ehcache(maven工程),在mapper.xml中使用
- 【总结】深插浅出重拾Linux
- ES6学习——元数据(meta)编程:代理(Proxies)API介绍
- HTML转义字符表的使用
- 九度1502 最大值最小化问题