当前位置:网站首页>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
边栏推荐
- 首届“量子计算+金融科技应用”研讨会在京成功举办
- Es6中Promise的使用
- 杰理之播内置 flash 提示音控制播放暂停【篇】
- HDU ACM 4578 Transformation-&gt;段树-间隔的变化
- 【达梦数据库】备份恢复后要执行两个sql语句
- Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
- [colmap] 3D reconstruction with known camera pose
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- input_ delay
- 杰理之关于 DAC 输出功率问题【篇】
猜你喜欢
Decoration design enterprise website management system source code (including mobile source code)
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
[cpk-ra6m4 development board environment construction based on RT thread studio]
centerX: 用中国特色社会主义的方式打开centernet
树莓派设置wifi自动连接
Oauth2协议中如何对accessToken进行校验
Utilisation de la promesse dans es6
Significance and measures of source code confidentiality
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
随机推荐
Codeforces round 264 (Div. 2) C gargari and Bishop [violence]
How to find file accessed / created just feed minutes ago
unrecognized selector sent to instance 0x10b34e810
[Dameng database] after backup and recovery, two SQL statements should be executed
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
Oracle connection pool is not used for a long time, and the connection fails
New benchmark! Intelligent social governance
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
Optimization of application startup speed
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
Shell 编程基础
Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
装饰设计企业网站管理系统源码(含手机版源码)
Household appliance industry under the "retail is king": what is the industry consensus?
Numpy中排序操作partition,argpartition,sort,argsort
Construction of knowledge map of mall commodities
SQL中删除数据
[cpk-ra6m4 development board environment construction based on RT thread studio]