当前位置:网站首页>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
边栏推荐
- c语言(字符串)如何把字符串中某个指定的字符删除?
- 新标杆!智慧化社会治理
- Use of tensorboard
- 上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
- Opencv environment, and open a local PC camera.
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Jerry's question about DAC output power [chapter]
- [tools] basic concept of database and MySQL installation
- 商城商品的知识图谱构建
- HDU 4337 King Arthur&#39; S Knights it outputs a Hamiltonian circuit
猜你喜欢
Stored procedures and functions (MySQL)
How to verify accesstoken in oauth2 protocol
知识图谱构建全流程
Flink task exit process and failover mechanism
Utilisation de la promesse dans es6
Unity uses maskablegraphic to draw a line with an arrow
2022.6.28
[cpk-ra6m4 development board environment construction based on RT thread studio]
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
Mathematical induction and recursion
随机推荐
C language string sorting
2022.6.28
Simple bubble sort
Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
体会设计细节
input_delay
CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
Analysis of USB network card sending and receiving data
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
Es6中Promise的使用
Use of tensorboard
How to analyze fans' interests?
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
leetcode-02(链表题)
Experience design details
Unity uses maskablegraphic to draw a line with an arrow
Flink task exit process and failover mechanism
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺