reading note 1

2024-05-24 19:32
文章标签 note reading

本文主要是介绍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<vector<int> > ary(row_num,vector<int>(col_num,0));
//使用reserve来避免不必要的重新分配


线性表====>数组,单链表,双向链表
// remove duplicates from sorted array
//时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector<int>& nums){
if(nums.empty()) return 0;
int index = 0;
for(int i=1;i<nums.size();i++){
if(nums[index] != nums[i])//不相等时赋值
nums[++index] = nums[i];
}
return index + 1;
}
//使用STL,时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector<int>& nums){
return distance(nums.begin(),unique(nums.begin(),nums.end());
}
//使用STL,时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector<int>& nums){
return distance(nums.begin(),removeduplicates(nums.begin(),nums.end(),nums.begin());
}
template<typename init, typename outit>
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<int>& nums){
if(nums.size() <= 2) return nums.size();
int index = 2;
for(int i=2;i<nums.size();i++){
if(nums[i] != nums[index - 2])
nums[index++] = nums[i];
}
return index;
}
//时间复杂度o(n),空间复杂度o(1)
int removeduplicates(vector<int>& nums){
const int n = nums.size();
int index = 0;
for(int i=0;i<n;++i){
if(i>0 && i<n-1 && nums[i] == nums[i-1] && nums[i] == nums[i+1])
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<int>& 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<int>& 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<int>& A, const vector<int>& 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<int> :: const_iterator A, int m, 
std :: vector<int> :: 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];
}

这篇关于reading note 1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999320

相关文章

nginx 8051#0: *4 recv() failed (104: Connection reset by peer) while reading response header from u

环境    php7   nginx1.8.0    nginx   报错  500  GATWAY网关错误 2017/08/28 10:45:42 [error] 7975#0: *333 recv() failed (104: Connection reset by peer) while reading response header from upstream, clien

SLAM Paper Reading和代码解析

最近对VINS、LIO-SAM等重新进行了Paper Reading和代码解析。这两篇paper和代码大约在三年前就读过,如今重新读起来,仍觉得十分经典,对SLAM算法研发具有十分重要的借鉴和指导意义。重新来读,对其中的一些关键计算过程也获得了更新清晰的了解,现整理分享出来,供有需要的同学参考。 VINS-MONO算法总结-徐胜攀.pdf资源-CSDN文库 对VINS-MONO的算法框架进

configparser.DuplicateSectionError: While reading from '/home/qinghua/.theanorc' [line 18]: section

python代码: import theano 出现错误: configparser.DuplicateSectionError: While reading from '/home/qinghua/.theanorc' [line 18]: section 'nvcc' already exists 解决方法是, vim ~/.theeanorc 删除行: [nvcc]

Paper Reading: EfficientAD:毫秒级延迟的准确视觉异常检测

EfficientAD 简介方法高效的patch描述PDN教师pretraining 轻量级的师生模型逻辑异常检测异常图像的标准化 实验局限性 EfficientAD: Accurate Visual Anomaly Detection at Millisecond-Level Latencies EfficientAD:毫秒级延迟的准确视觉异常检测, WACV 2024 pa

【Reading List】【20190510】预训练(pre-trained)语言模型

RNN,seq2seq,Attention: https://www.leiphone.com/news/201709/8tDpwklrKubaecTa.html 图解transformer : https://blog.csdn.net/qq_41664845/article/details/84969266 Attentinon: https://blog.csdn.net/male

关于VS、Qt报 error reading VS project setting的错误处理

关于VS、Qt报 error reading VS project setting的错误处理 首先: 卸载Qt编辑插件。 其次: 卸载相关的VS软件,并用Everything,查找VS,visualstudio,删除相对应当的文件。 最后: 重装VS软件。由于删除了VS先关文件,以前安装的其他辅助工具都没有了。 在此安装 qt-vsaddin-msvc***-***.

【Python报错】已解决EOFError: EOF when reading a line

解决Python报错:EOFError: EOF when reading a line 成功解决“EOFError: EOF when reading a line”错误的全面指南 一、引言 在Python编程中,EOFError: EOF when reading a line是一个常见的异常,通常出现在使用input()函数或文件读取操作时。这个错误表明程序在尝试从输入流(如文件或标

chisel note

使用jupyter/binder(Scala编程第四版高宇翔译):https://gke.mybinder.org/ https://github.com/freechipsproject/chisel-bootcamp  等待几秒后:点击对应的标签就可以一章一章的在线练习了:  //------------------------------------------------

(Note)朗伯比尔定律

朗伯比尔定律(Lambert-Beer law)是分光光度法的基本定律,是描述物质对某一波长光吸收的强弱与吸光物质的浓度及其液层厚度间的关系,适用于所有的电磁辐射和所有的吸光物质,包括气体、固体、液体、分子、原子和离子。         其数学表达式为A=lg(1/T)=Kbc,其中A为吸光度,T为透射比(透光度),是出射光强度(I)比入射光强度(I0),K为摩尔吸光系数,与吸收