当前位置:网站首页>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

边栏推荐
- Jerry's phonebook acquisition [chapter]
- 你知道电子招标最突出的5大好处有哪些吗?
- netperf 而网络性能测量
- Do you know the five most prominent advantages of E-bidding?
- IDEA重启后无法创建Servlet文件的解决方案
- The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
- 安装 torch 0.4.1
- Oracle connection pool is not used for a long time, and the connection fails
- New benchmark! Intelligent social governance
- Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
猜你喜欢
随机推荐
Netperf and network performance measurement
The whole process of knowledge map construction
HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
Jerry's phonebook acquisition [chapter]
leetcode
Decoration design enterprise website management system source code (including mobile source code)
Uniapp adaptation problem
C language string sorting
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
[cpk-ra6m4 development board environment construction based on RT thread studio]
Oauth2协议中如何对accessToken进行校验
【colmap】已知相机位姿情况下进行三维重建
Make (convert) ICO Icon
存储过程与函数(MySQL)
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
2022年信息安全工程师考试大纲
应用程序启动速度的优化









