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

边栏推荐
- tensorboard的使用
- Shell 编程基础
- 房费制——登录优化
- mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
- Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
- The version control of 2021 version is missing. Handling method
- 存储过程与函数(MySQL)
- CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
- 杰理之FM 模式单声道或立体声选择设置【篇】
- input_delay
猜你喜欢

如何分析粉丝兴趣?

Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions

制作(转换)ico图标

How to verify accesstoken in oauth2 protocol

Hazel engine learning (V)

【基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建】

知识图谱构建全流程

体会设计细节

从0开始创建小程序

Another million qubits! Israel optical quantum start-up company completed $15million financing
随机推荐
源代码保密的意义和措施
2022.6.28
How-PIL-to-Tensor
Cocos2d-x Box2D物理引擎编译设置
Household appliance industry under the "retail is king": what is the industry consensus?
2022 information security engineer examination outline
Unity uses maskablegraphic to draw a line with an arrow
Construction of knowledge map of mall commodities
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
tensorboard的使用
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
又一百万量子比特!以色列光量子初创公司完成1500万美元融资
leetcode
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
Kubernetes source code analysis (II) -- resource
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
How to analyze fans' interests?