当前位置:网站首页>2022.6.28
2022.6.28
2022-07-06 19:47:00 【bu_xiang_tutou】
早上:学习《MySQl必知必会》学到了第5章,html,一些标签(meta等);
中午:
题解:
我的思路是把链表中的数放进一个数组里面,然后再将数组中的数反转进链表中。这个思路怎么使用在链表的长度不是很长的时候。时间复杂度为O(n),空间复杂度为O(n)
代码入下:
/**
* 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;
}
};优化:使用链表可以优化,思路是在遍历链表时,将指针反转(由指向为next反转为prev);
空间复杂度为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;
}
};题目:3. 无重复字符的最长子串 - 力扣(LeetCode)
题解: 1.用一个数记录当前最长的子串长度。不断更新。
2.用一个数组记录当前字符有没有出现过,如果出现过,数组的值为该字符上一次出现的位置。如果没有出现,数组的值为0。
3.遍历数组,如果当前字符没有出现过,就接着走,重新加载最长的子串长度,然后将数组的值全部改成0,如果出现过,就找到这个字符上一次出现的位置,如果是当前位置的前一个就把数组的值改成当前位置,反之将之前出现过字符数组的值再一次赋值。
4.时间复杂度为O(n^2)。
代码如下:
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){//之前没有出现
nums[s[i]-NULL]=i+1;
maxx=max(maxx,i-j+1);//求最长的子串
}else{//之前出现过
j=nums[s[i]-NULL];//找到重复字符出现的位置
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;
}
};题解:有思路但是代码写不出来,看了一下hashmap的源码更懵逼了。
MySQL的刷题

边栏推荐
- C language string sorting
- c语言字符串排序
- Redis getting started complete tutorial: replication configuration
- sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
- Household appliance industry under the "retail is king": what is the industry consensus?
- Cocos2d-x Box2D物理引擎编译设置
- The solution of unable to create servlet file after idea restart
- How to verify accesstoken in oauth2 protocol
- The whole process of knowledge map construction
- 「小样本深度学习图像识别」最新2022综述
猜你喜欢

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

Construction of knowledge map of mall commodities

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

商城商品的知识图谱构建
![[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]](/img/f0/79e7915ba3ef32aa21c4a1d5f486bd.jpg)
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]

Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application

Left value, right value

Redis入门完整教程:RDB持久化

Software testing -- common assertions of JMeter interface testing

杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
随机推荐
Appx代码签名指南
Cloud Mail . NET Edition
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
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
Leetcode 77: combination
Matlab Error (Matrix dimensions must agree)
如何分析粉丝兴趣?
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
mos管实现主副电源自动切换电路,并且“零”压降,静态电流20uA
uniapp的表单验证
How to find file accessed / created just feed minutes ago
【基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建】
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
杰理之播内置 flash 提示音控制播放暂停【篇】
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
c语言(字符串)如何把字符串中某个指定的字符删除?
Redis getting started complete tutorial: client management
Examples of how to use dates in Oracle
Analysis of USB network card sending and receiving data