qingcheng奕  

2014年2月12日

摘要: http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/如果在数组中有重复的元素,则不一定说必定前面或者后面的一半是有序的,所以有个while循环的处理。#include using namespace std; class Solution {public: bool binarySearch(int A[],int target,int begin,int end) { int mid = (begin+end)/2; if(target == A[mid]) ... 阅读全文
posted @ 2014-02-12 17:33 qingcheng奕 阅读(133) 评论(0) 推荐(0) 编辑
 
摘要: http://oj.leetcode.com/problems/search-in-rotated-sorted-array/转换了一次的有序数组,进行类似二分查找。从begin到mid和从mid到end,二者中肯定有一个是有序的。#include using namespace std; class Solution {public: int binarySearch(int A[],int target,int begin,int end) { int mid = (begin+end)/2; if(target == A[mid]) ... 阅读全文
posted @ 2014-02-12 16:39 qingcheng奕 阅读(113) 评论(0) 推荐(0) 编辑
 
摘要: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/要求可以用重复元素,但是最大重复次数为两次。#include using namespace std;class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; if(n == 1) return 1; int times = 1; int p... 阅读全文
posted @ 2014-02-12 15:01 qingcheng奕 阅读(122) 评论(0) 推荐(0) 编辑
 
摘要: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/删除数组中的重复元素,要求为原地算法。进行一遍遍历,记录下一个可用位置,也就是用来存储不重复元素的位置。class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; if(n == 1) return 1; int preOne = A[0]; int... 阅读全文
posted @ 2014-02-12 14:41 qingcheng奕 阅读(121) 评论(0) 推荐(0) 编辑