reading note 1

//判断两个浮点数a和b是否相等
a == b ====> fabs(a-b) < 1e-9
//判断一个整数是否为奇数(x可能是负数)
x % 2 ==1 =====>x % 2 != 0
//char的值作为数组下标(char可能是负数)
先强制转型为unsigned char,再用作下标
//vector和string性能优先于动态分配的数组
vector > ary(row_num,vector(col_num,0));
//使用reserve来避免不必要的重新分配


线性表====>数组,单链表,双向链表
// remove duplicates from sorted array
//时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector& nums){
if(nums.empty()) return 0;
int index = 0;
for(int i=1;i if(nums[index] != nums[i])//不相等时赋值
nums[++index] = nums[i];
}
return index + 1;
}
//使用STL,时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector& nums){
return distance(nums.begin(),unique(nums.begin(),nums.end());
}
//使用STL,时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector& nums){
return distance(nums.begin(),removeduplicates(nums.begin(),nums.end(),nums.begin());
}
template
outit removeduplicates(init first, init last, outit output){
while(first != last){
*output++ = *first;
first = upper_bound(first,last,*first);
}
return output;
}


//remove duplicates from sorted array II
//时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector& nums){
if(nums.size() <= 2) return nums.size();
int index = 2;
for(int i=2;i if(nums[i] != nums[index - 2])
nums[index++] = nums[i];
}
return index;
}
//时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector& nums){
const int n = nums.size();
int index = 0;
for(int i=0;i if(i>0 && i continue;
nums[index++] = nums[i];
}
return index;
}


//search in rotated sorted array
//suppose a sorted array is rotated at some pivot unknown to you beforehand
//you are given a target value to search, if found in the array return its
//index, otherwise return -1, you may assume no duplicates exists in the array
//二分查找,注意边界的确定
//时间复杂度o(logn),空间复杂度o(1)
int search(const vector& nums, int target){
int first = 0, last = nums.size();
while(first ! = last){
const int mid = first + (last - first) / 2;
if(nums[mid] == target)
return mid;
if(nums[first] <= nums[mid]){
if(nums[first] <= target && target < nums[mid])
last = mid;
else 
first = mid + 1;
}else{
if(nums[mid] < target && target <= nums[last-1])
first = mid + 1;
else
last = mid;
}
}
return -1;
}
//search in rotated sorted array II
//时间复杂度o(n),空间复杂度o(1)
bool search(const vector& nums, int target){
int first = 0, last = nums.size();
while(first != last){
const int mid = first + (last - first) / 2;
if(nums[mid] == target)
return true;
if(nums[first] < nums[mid]){
if(nums[first] <= target && target < nums[mid])
last = mid;
else
first = mid + 1;
}else if(nums[first] > nums[mid]){
if(nums[mid] < target && target <= nums[last-1])
first = mid + 1;
else
last = mid;
}else  //skip duplicate one
first++;
}
return false;
}


//There are two sorted arrays A and B of size m and n respectively. Find the 
//median of the two sorted arrays. The overall run time complexity should be
//o(log(m+n))
//如果是要求time complexity o(m+n)的话,只要merge两个数组,然后求第k大element
//采用递归函数,终止条件如下
// 当A或者B为空时,直接返回B[k-1]或者A[k-1]
// 当k=1时,返回min(A[0],B[0])
// 当A[k/2-1] == B[k/2-1]时,返回A[k/2-1] 或 B[k/2-1]
//   median of two sorted arrays time complexity o(log(m+n)) 
//   space complexity o(log(m+n))
double findmediansortedarrays(const vector& A, const vector& B){
const int m = A.size();
const int n = B.size();
int total = m + n;
if(total & 0x1)
return find_kth(A.begin(),m,B.begin(),n,total / 2 + 1);
else
return (find_kth(A.begin(),m,B.begin(),n,total / 2) + find_kth(A.begin(),
m,B.begin(),n,total / 2 + 1)) / 2.0;
}
static int find_kth(std :: vector :: const_iterator A, int m, 
std :: vector :: const_iterator B,int n, int k){
//always assume that m is equal or smaller than n
if(m > n) return find_kth(B,n,A,m,k);
if(m == 0) return *(B+k-1);
if(k == 1) return min(*A,*B);
//divide k into two parts
int ia = min(k / 2, m), ib = k - ia;
if(*(A + ia -1) < *(B + ib -1))
return find_kth(A + ia, m - ia, B, n, k - ia);
else if(*(A + ia -1) > *(B + ib -1))
return find_kth(A, m, B + ib, n - ib, k - ib);
else
return A[ia-1];
}

你可能感兴趣的:(Judge,Online)