reading note 2

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

本文主要是介绍reading note 2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//longest consecutive sequence
//given an unsorted array of integers, find the length of the longest 
//consecutive elements sequence. 
//先排序,然后求解时间复杂度o(nlogn)
//your algorithm should be run in o(n) complexity==>哈希表
//space complexity o(n)
int longestconsecutive(const vector<int>& nums){
unordered_map<int,bool> used;
for(auto i : nums) used[i] = false;
int longest = 0;
for(auto i : nums){
if(used[i]) continue;
int length = 1;
used[i] = true;
for(int j = i + 1; used.find(j) != used.end(); ++j){
used[j] = true;
++length;
}
for(int j = i - 1; used.find(j) != used.end(); --j){
used[j] = true;
++length;
}
longest = max(longest, length);
}
return longest;
}
//longest consecutive sequence
//time complexity o(n) space complexity o(n)
int longestconsecutive(vector<int>& nums){
unordered_map<int, int> map;
int size = nums.size();
int l = 1;
for(i = 0;i < size; i++){
if(map.find(nums[i]) != map.end()) continue;
map[nums[i]] = 1;
if(map.find(nums[i] - 1) != map.end()){
l = max(l,mergecluster(map,nums[i] - 1,nums[i]));
}
if(map.find(nums[i] + 1) != map.end()){
l = max(l,mergecluster(map,nums[i],nums[i] + 1));
}
}
return size == 0 ? 0 : 1;
}


int mergecluster(unordered_map<int,int>& map, int left, int right){
int upper = right + map[right] - 1;
int lower = left - map[left] + 1;
int length = upper - lower + 1;
map[upper] = length;
map[lower] = length;
return length;
}


// given an array of integers, find two numbers such that they add up to a 
//specific target number, the function twosum should return indices of the 
//two numbers such that they add up to the target, where index1 must be less
//than index2, please note that your returned answers(both index1 and index2)
//are not zero-based
//you may assume that each input would have exactly one solution
//方法1:暴力,复杂度o(n^2),超时
//方法2:hash,用一个哈希表,存储每个数对应的下标,复杂度o(n);
//time complexity o(n) space complexity o(n)
vector<int> twosum(vector<int>& nums, int target){
unordered_map<int,int> mapping;
vector<int> result;
for(int i=0; i < nums.size();i++){
mapping[nums[i]] = i;

for(int i=0;i < nums.size();i++){
const int gap = target - nums[i];
if(mapping.find(gap) != mapping.end() && mapping[gap] > i){
result.push_back(i+1);
result.push_back(mapping[gap]+1);
break;
}
}
return result;
}

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



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

相关文章

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为摩尔吸光系数,与吸收