当前位置: 首页 > news >正文

【代码随想录】2

数组篇

二分查找

int search(int* nums, int numsSize, int target) {
int left=0;
int right=numsSize-1;
while(left<=right)
{int mlddle=(left+right)/2;if(nums[mlddle]>target){right=mlddle-1;}else if(nums[mlddle]<target){left=mlddle+1;}else{return mlddle;}}
return -1;    
}

移除元素

int removeElement(int* nums, int numsSize, int val) {
int slow=0;for(int fast=0;fast<numsSize;fast++)
{if(nums[fast]!=val)
{nums[slow]=nums[fast];slow++;}}return slow;}

有序数组的平方

int* sortedSquares(int* nums, int numsSize, int* returnSize) {*returnSize=numsSize;int*arr=(int*)malloc(*returnSize*sizeof(int));if(arr==NULL){perror("malloc fail");}int k=numsSize-1;int i=0;int j=numsSize-1;while(i<=j){if(pow(nums[i],2)>pow(nums[j],2)){arr[k]=pow(nums[i],2);i++;k--;}else{arr[k]=pow(nums[j],2);j--;k--;}}return arr;free(arr);}

长度最小的数组

int minSubArrayLen(int target, int* nums, int numsSize) {
int i=0;
int min=INT_MAX;
int j=0;
int sum=0;
for(j=0;j<numsSize;j++)
{sum+=nums[j];
while(sum>=target)
{int result=j-i+1;
if(result<min)
{min=result;
}
sum-=nums[i];
i++;}}
return min==INT_MAX?0:min;
}

螺旋矩阵2

int** generateMatrix(int n, int* returnSize, int** returnColumnSizes) {*returnSize=n;*returnColumnSizes=(int*)malloc(sizeof(int)*n);int**ans=(int**)malloc(sizeof(int*)*n);int i;for( i=0;i<n;i++){ans[i]=(int*)malloc(sizeof(int)*n);(*returnColumnSizes)[i]=n;}int startx=0;int starty=0;int offset=1;int count=1;int mid=n/2;while(mid){int i=startx;int j=starty;for(j=starty;j<n-offset;j++)ans[i][j]=count++;for(i=startx;i<n-offset;i++)ans[i][j]=count++;for(;j>starty;j--)ans[i][j]=count++;for(;i>startx;i--)ans[i][j]=count++;mid--;startx++;starty++;;offset+=1;}if(n%2)ans[n/2][n/2]=count;return ans;
}

链表篇

移除链表元素
1.无哨兵位头结点

struct ListNode* removeElements(struct ListNode* head, int val) {
while(head!=NULL&&head->val==val)
{
head=head->next;}  
struct ListNode* cur=head;
while(cur!=NULL&&cur->next!=NULL)
{if(cur->next->val==val){cur->next=cur->next->next;}else{cur=cur->next;}
}
return head;}

2.有

struct ListNode* removeElements(struct ListNode* head, int val) {struct ListNode*newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* cur=newnode;
cur->next=head;
while(cur->next!=NULL)
{if(cur->next->val==val)
cur->next=cur->next->next;
else
cur=cur->next;
}return newnode->next;}

翻转链表

struct ListNode* reverseList(struct ListNode* head) {if(head==NULL)return NULL;
struct ListNode*n1=NULL;
struct ListNode*n2=head;
struct ListNode*n3=n2->next;
while(n2)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3!=NULL)
n3=n3->next;}  
return n1;  }

递归版

struct ListNode* reverse(struct ListNode*prev,struct ListNode*cur)
{
if(!cur)
return prev;
struct ListNode* tmp=cur->next;
cur->next=prev;return reverse(cur,tmp);}struct ListNode* reverseList(struct ListNode* head) {return reverse(NULL,head);
}

两两交换链表

struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode* newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
if(head==NULL)
return NULL;
newnode->next=head;
struct ListNode*cur=newnode;
while(cur->next!=NULL&&cur->next->next!=NULL)
{struct ListNode*tmp1=cur->next;
struct ListNode*tmp2=cur->next->next->next;
cur->next=cur->next->next;
cur->next->next=tmp1;
tmp1->next=tmp2;
cur=cur->next->next;
}
return newnode->next;  
}

删除链表倒数第n个节点

struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {struct ListNode*newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode*fast=newnode;
struct ListNode*slow=newnode;
newnode->next=head;
int k=n+1;
while(k)
{if(fast==NULL)
return NULL;fast=fast->next;k--;
}   
while(fast)
{fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;return newnode->next;
}

环形链表2

struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *fast=head;
struct ListNode *slow=head;while(fast&&fast->next)
{fast=fast->next->next;
slow=slow->next;
if(slow==fast)
{struct ListNode *meet=slow;
while(meet!=head)
{meet=meet->next;
head=head->next;}
return meet;}
}
return NULL;
}

相关文章:

【代码随想录】2

数组篇 二分查找 int search(int* nums, int numsSize, int target) { int left0; int rightnumsSize-1; while(left<right) {int mlddle(leftright)/2;if(nums[mlddle]>target){rightmlddle-1;}else if(nums[mlddle]<target){leftmlddle1;}else{return mlddle;}} r…...

TCP性能分析

ref&#xff1a; TCP性能和发送接收窗口、Buffer的关系 | plantegg...

RibbonGroup 添加QRadioButton

RibbonGroup添加QRadioButton&#xff1a; QRadioButton * pRadio new QRadioButton(tr("Radio")); pRadio->setToolTip(tr("Radio")); groupClipboard->addWidget(pRadio); connect(pRadio, SIGNAL(clicked(…...

一篇文章掌握WebService服务、工作原理、核心组件、主流框架

目录 1、WebService定义 解决问题&#xff1a; 2、WebService的工作原理 2.1 实现一个完整的Web服务包括以下步骤 2.2 调用方式 3、Web Service的核心组件 3.1 XML 3.2 SOAP 3.3 WSDL 3.4 UDDI 4、主流框架 4.1 AXIS(已淘汰) 4.2 XFire 4.3 CXF 5、Soap协议详解…...

观成科技-加密C2框架EvilOSX流量分析

工具简介 EvilOSX是一款开源的&#xff0c;由python编写专门为macOS系统设计的C2工具&#xff0c;该工具可以利用自身释放的木马来实现一系列集成功能&#xff0c;如键盘记录、文件捕获、浏览器历史记录爬取、截屏等。EvilOSX主要使用HTTP协议进行通信&#xff0c;通信内容为特…...

PCL 计算异面直线的距离

目录 一、算法原理二、代码实现三、结果展示四、相关链接本文由CSDN点云侠原创,PCL 计算异面直线的距离,爬虫自重。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫与GPT。 一、算法原理 设置直线 A B AB A...

【数字人】9、DiffTalk | 使用扩散模型基于 audio-driven+对应人物视频 合成说话头(CVPR2023)

论文&#xff1a;DiffTalk: Crafting Diffusion Models for Generalized Audio-Driven Portraits Animation 代码&#xff1a;https://sstzal.github.io/DiffTalk/ 出处&#xff1a;CVPR2023 特点&#xff1a;需要音频对应人物的视频来合成新的说话头视频&#xff0c;嘴部抖…...

完成源示例

本主题演示如何创作和使用自己的完成源类&#xff0c;类似于 .NET 的 TaskCompletionSource。 completion_source 示例的源代码 下面的列表中的代码作为示例提供。 其目的是说明如何编写自己的版本。 例如&#xff0c;支持取消和错误传播不在此示例的范围内。 #include <w…...

业务和流程的关系

背景 概念不清&#xff0c;沟通就容易出现问题&#xff0c;最可怕会出现跑偏情况如何解决&#xff0c;数字化落地过程&#xff0c;程序是死的&#xff0c;最怕灵活&#xff0c;所以在沟通和编码&#xff0c;设计中&#xff0c;很重要的一点就是解决概念&#xff0c;澄清问题&a…...

【河海大学论文LaTeX+VSCode全指南】

河海大学论文LaTeXVSCode全指南 前言一、 LaTeX \LaTeX{} LATE​X的安装二、VScode的安装三、VScode的配置四、验证五、优化 前言 LaTeX \LaTeX{} LATE​X在论文写作方面具有传统Word无法比拟的优点&#xff0c;VScode作为一个轻量化的全功能文本编辑器&#xff0c;由于其极强的…...

学习python仅此一篇就够了(文件操作:读,写,追加)

python文件操作 文件编码 编码技术即&#xff1a;翻译的规则&#xff0c;记录了如何将内容翻译成二进制&#xff0c;以及如何将二进制翻译回可识别内容。 计算机中有许多可用编码&#xff1a; UTF-8 GBK BUG5 文件的读取操作 open&#xff08;&#xff09;函数 在pyth…...

vue中 ref 和 $refs的使用

1. 作用 利用 ref 和 $refs 可以用于 获取 dom 元素, 或 组件实例 2. 获取 dom 使用步骤&#xff1a; 2.1 目标标签添加属性 :ref <div ref"chartRef">我是渲染图表的容器</div>2.2 通过$ref&#xff1a;获取标签 mounted() {console.log(this.$re…...

Centos7升级openssl到openssl1.1.1

Centos7升级openssl到openssl1.1.1 1、先查看openssl版本&#xff1a;openssl version 2、Centos7升级openssl到openssl1.1.1 升级步骤 #1、更新所有现有的软件包列表并安装最新的软件包&#xff1a; $sudo yum update #2、接下来&#xff0c;我们需要从源代码编译和构建OpenS…...

uniapp中实现H5录音和上传、实时语音识别(兼容App小程序)和波形可视化

文章目录 Recorder-UniCore插件特性集成到项目中调用录音上传录音ASR语音识别 在uniapp中使用Recorder-UniCore插件可以实现跨平台录音功能&#xff0c;uniapp自带的recorderManager接口不支持H5、录音格式和实时回调onFrameRecorded兼容性不好&#xff0c;用Recorder插件可避免…...

HashMap集合万字源码详解(面试常考)

文章目录 HashMap集合1.散列2.hashMap结构3.继承关系4.成员变量5.构造方法6.成员方法6.1增加方法6.2将链表转换为红黑树的treeifyBin方法6.3扩容方法_resize6.3.1扩容机制6.3.2源码resize方法的解读 6.4 删除方法(remove)6.5查找元素方法(get)6.6遍历HashMap集合几种方式 7.初始…...

LeetCode1124. Longest Well-Performing Interval

文章目录 一、题目二、题解 一、题目 We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing in…...

如何使用手机公网远程访问本地群辉Video Station中视频文件【内网穿透】

最近&#xff0c;我发现了一个超级强大的人工智能学习网站。它以通俗易懂的方式呈现复杂的概念&#xff0c;而且内容风趣幽默。我觉得它对大家可能会有所帮助&#xff0c;所以我在此分享。点击这里跳转到网站。 文章目录 1.使用环境要求&#xff1a;2.下载群晖videostation&am…...

事件分析应急响应-Server2229(环境+解析)

任务环境说明: 服务器场景:Server2229(开放链接)用户名:root,密码:...

SpringCloud:微服务

文章目录 微服务服务架构演变单例架构&#xff08;集中式架构&#xff09;分布式架构 微服务SpringCloud 微服务 服务架构演变 单例架构&#xff08;集中式架构&#xff09; 单例架构&#xff1a; 将业务的所有功能集中在一个项目中开发&#xff0c;打成一个包部署 优点&…...

拥抱Guava之集合操作

深入Guava集合操作 在Java开发中&#xff0c;Google Guava库是处理集合的强大工具。起源于Google内部需求&#xff0c;Guava以简洁性、性能优化为理念&#xff0c;提供高效不可变集合和实用工具类。本文深入剖析Guava的核心功能&#xff0c;为开发者呈现集合操作的全新视角&am…...

Python爬虫实战:研究MechanicalSoup库相关技术

一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

stm32G473的flash模式是单bank还是双bank?

今天突然有人stm32G473的flash模式是单bank还是双bank&#xff1f;由于时间太久&#xff0c;我真忘记了。搜搜发现&#xff0c;还真有人和我一样。见下面的链接&#xff1a;https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

iOS性能调优实战:借助克魔(KeyMob)与常用工具深度洞察App瓶颈

在日常iOS开发过程中&#xff0c;性能问题往往是最令人头疼的一类Bug。尤其是在App上线前的压测阶段或是处理用户反馈的高发期&#xff0c;开发者往往需要面对卡顿、崩溃、能耗异常、日志混乱等一系列问题。这些问题表面上看似偶发&#xff0c;但背后往往隐藏着系统资源调度不当…...

MySQL 8.0 事务全面讲解

以下是一个结合两次回答的 MySQL 8.0 事务全面讲解&#xff0c;涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容&#xff0c;并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念&#xff08;ACID&#xff09; 事务是…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)

题目 做法 启动靶机&#xff0c;点进去 点进去 查看URL&#xff0c;有 ?fileflag.php说明存在文件包含&#xff0c;原理是php://filter 协议 当它与包含函数结合时&#xff0c;php://filter流会被当作php文件执行。 用php://filter加编码&#xff0c;能让PHP把文件内容…...

解决:Android studio 编译后报错\app\src\main\cpp\CMakeLists.txt‘ to exist

现象&#xff1a; android studio报错&#xff1a; [CXX1409] D:\GitLab\xxxxx\app.cxx\Debug\3f3w4y1i\arm64-v8a\android_gradle_build.json : expected buildFiles file ‘D:\GitLab\xxxxx\app\src\main\cpp\CMakeLists.txt’ to exist 解决&#xff1a; 不要动CMakeLists.…...

【无标题】湖北理元理律师事务所:债务优化中的生活保障与法律平衡之道

文/法律实务观察组 在债务重组领域&#xff0c;专业机构的核心价值不仅在于减轻债务数字&#xff0c;更在于帮助债务人在履行义务的同时维持基本生活尊严。湖北理元理律师事务所的服务实践表明&#xff0c;合法债务优化需同步实现三重平衡&#xff1a; 法律刚性&#xff08;债…...