当前位置:网站首页>leetcode SVM
leetcode SVM
2022-08-03 16:02:00 【Anan.3】
20. 有效的括号
此题是运用到数据结构中的栈,把左括号都入栈,当遇到右括号出栈进行比较,判断是否能够匹配,匹配成功则输出true,反之输出false
bool isValid(char * s){
char *p=(char *)malloc(strlen(s));
int i,j=0;
for(i=0;i<strlen(s);i++){
if(s[i]=='(' || s[i]=='{' || s[i]=='['){
p[j++]=s[i];
}
if(s[i]==')'){
if(j-1<0 || p[j-1]!='('){
return 0;
}else{
j--;
}
}else if(s[i]=='}'){
if(j-1<0 || p[j-1]!='{'){
return 0;
}else{
j--;
}
}else if(s[i]==']'){
if(j-1<0 || p[j-1]!='['){
return 0;
}else{
j--;
}
}
}
free(p);
if(j){
return 0;
}
return 1;
}
378. 有序矩阵中第 K 小的元素
本题用冒泡排序会超时,因此我选择了qsort,对数组进行排序
int cmp(const void *a,const void *b){
return (*(int *)a - *(int *)b);
}
int kthSmallest(int** matrix, int matrixSize, int* matrixColSize, int k){
int *b=(int *)malloc(matrixSize * matrixSize * sizeof(int));
int num=0;
for(int i=0;i<matrixSize;i++){
for(int j=0;j<matrixSize;j++){
b[num++]=matrix[i][j];
}
}
qsort(b,num,sizeof(int),cmp);
return b[k-1];
}
206. 反转链表
定义指针cur,用来遍历链表,定义prev,存储cur上一个节点的位置,next,保存cur下一个节点的位置,通过prev和cur来调整每个节点指针的方向,next用来迭代,最后返回头指针head
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *prev=NULL;
struct ListNode *cur=head;
while(cur!=NULL){
struct ListNode *next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
head=prev;
return head;
}
141. 环形链表
使用前后指针法,slow一次走一步,fast一次走两步,如果存在环,那么他们最终会在环中相遇,设为meet,如果不存在环,fast可能走到空或者最后一个节点 ,此时返回空.存在环,slow和fast会在环中meet处相遇,此时一个指针从head开始走,一个指针从meet开始走,他们会在环的入口处相遇
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
bool hasCycle(struct ListNode *head) {
struct ListNode *slow=head,*fast=head;
while(fast&&fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
struct ListNode *meet=slow;
while(head!=meet){
head=head->next;
meet=meet->next;
}
return meet;
}
}
return NULL;
}
237. 删除链表中的节点
本题可以将后一个节点的值赋给要删除的节点,然后用要删除的节点代替后一个节点,释放掉后一个节点
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
void deleteNode(struct ListNode* node) {
struct ListNode *p=node->next;
node->val=p->val;
node->next=p->next;
free(p);
}
SVM
边栏推荐
猜你喜欢
Not to be ignored!Features and advantages of outdoor LED display
出海季,互联网出海锦囊之本地化
人脸识别损失函数的汇总 | Pytorch版本实现
面了个腾讯35k出来的,他让我见识到什么叫精通MySQL调优
30W 2C(JD6606S + FP6652X2)BOM
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 01
Windows 事件查看器记录到 MYSQL
Yii2安装遇到Loading composer repositories with package information
全新探险者以40万的产品击穿豪华SUV价格壁垒
元宇宙系列--Value creation in the metaverse
随机推荐
Not to be ignored!Features and advantages of outdoor LED display
证实了,百度没有快照了
30W 2C(JD6606S + FP6652X2)BOM
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
在 360 度绩效评估中应该问的 20 个问题
我在滴滴做开源
基于DMS的数仓智能运维服务,知多少?
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 02
小熊派——无线联网开发
vector类
如何选择合适的损失函数,请看......
CS免杀姿势
想进阿里?先来搞懂一下分布式事务
Some optional strategies and usage scenarios for PWA application Service Worker caching
1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
socket快速理解
[微信小程序开发者工具] × #initialize
字典表(还需要输入2个字)
20. Valid Parentheses
托尔斯泰:生活中只有两种不幸