当前位置:网站首页>2022.6.28
2022.6.28
2022-07-07 03:18:00 【bu_ xiang_ tutou】
morning : Study 《MySQl Will know 》 Learned the first 5 Chapter ,html, Some labels (meta etc. );
At noon, :
subject :206. Reverse a linked list - Power button (LeetCode)
Answer key :
My idea is to put the numbers in the linked list into an array , Then invert the number in the array into the linked list . How to use this idea when the length of the linked list is not very long . The time complexity is O(n), The space complexity is O(n)
Code in :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *p;
p=head;
int i=0;
vector<int> a(5001);
while(p!=NULL){
a[i]=p->val;
i++;
p=p->next;
}
p=head;
for(int j=0;j<i;j++){
p->val=a[i-j-1];
p=p->next;
}
return head;
}
};
Optimize : Using linked lists can optimize , The idea is to traverse the linked list , Reverse the pointer ( From to next Reversal is prev);
The space complexity is O(1).
class Solution {
public:
ListNode* reverseList(ListNode* head){
ListNode* p = NULL;
ListNode* c = head;
while (c!=NULL) {
ListNode* next = c->next;
c->next = p;
p = c;
c = next;
}
return p;
}
};
subject :3. Longest substring without repeating characters - Power button (LeetCode)
Answer key : 1. Record the current longest substring length with a number . Constantly updated .
2. Use an array to record whether the current character has appeared , If there has been , The value of the array is the last occurrence of the character . If not , The value of the array is 0.
3. Traversal array , If the current character does not appear , Just keep going , Reload the longest substring length , Then change all the values of the array to 0, If there has been , Just find the last position of this character , If it is the previous one of the current position, change the value of the array to the current position , On the contrary, assign the value of the previous character array again .
4. The time complexity is O(n^2).
The code is as follows :
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.size()==0)
return 0;
int nums[500],maxx=1,j=0;
memset(nums,0,sizeof(nums));
nums[s[0]-NULL]=1;
for(int i=1;i<s.size();i++){
if(nums[s[i]-NULL]==0){// It didn't appear before
nums[s[i]-NULL]=i+1;
maxx=max(maxx,i-j+1);// Find the longest substring
}else{// There have been... Before
j=nums[s[i]-NULL];// Find the position where the repeated characters appear
memset(nums,0,sizeof(nums));
if(s[i-1]==s[i]){
j=i;
nums[s[i]-NULL]=i+1;
}else{
for(int k=j;k<=i;k++)
nums[s[k]-NULL]=k+1;
}
}
}
return maxx;
}
};
subject :146. LRU cache - Power button (LeetCode)
Answer key : There are ideas, but the code can't be written , Look at the hashmap The source code of is even more confused .
MySQL The brush topic of
边栏推荐
- 杰理之开 BLE 退出蓝牙模式卡机问题【篇】
- 装饰设计企业网站管理系统源码(含手机版源码)
- [Dameng database] after backup and recovery, two SQL statements should be executed
- 知识图谱构建全流程
- HDU 4337 King Arthur&#39; S Knights it outputs a Hamiltonian circuit
- 「小样本深度学习图像识别」最新2022综述
- 【达梦数据库】添加自动收集统计信息的任务
- 首届“量子计算+金融科技应用”研讨会在京成功举办
- [tools] basic concept of database and MySQL installation
- 树莓派设置静态ip
猜你喜欢
Hazel engine learning (V)
QT Bluetooth: qbluetooth DeviceInfo
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
The whole process of knowledge map construction
Cryptography series: detailed explanation of online certificate status protocol OCSP
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
随机推荐
SQL中删除数据
源代码保密的意义和措施
杰理之RTC 时钟开发【篇】
Opencv environment, and open a local PC camera.
SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
杰理之关于 DAC 输出功率问题【篇】
uniapp适配问题
Data analysis from the perspective of control theory
Starting from 1.5, build a micro Service Framework -- log tracking traceid
LAB1配置脚本
Use of promise in ES6
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
[cpk-ra6m4 development board environment construction based on RT thread studio]
从0开始创建小程序
unrecognized selector sent to instance 0x10b34e810
编译常量、ClassLoader类、系统类加载器深度探析
2022年信息安全工程师考试大纲
变量、流程控制与游标(MySQL)