当前位置:网站首页>复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
2022-07-01 16:01:00 【如何写出最优雅的代码】
两道关于复杂度的LeetCode OJ题
代码:C语言
文章目录
1. 消失的数字
点击前往LeetCode查看消失的数字
1.1 题目描述
要求 :时间复杂度要在O(n)内
1.2 解决思路及代码实现
1.2.1 思路1
- malloc一个额外的个数为n+1的数组,并全部初始化为-1。
- 遍历要输入的数据,这个数是多少,就填写到该数组对应位置。
- 再遍历一遍该数组,哪个位置是-1,那么哪个位置的下标就是缺失的数字。
ps:Drawio的绘图很不错!!!有需要的可以导Microsoft Store搜索应用Drawio,直接下载正版。
- 时间复杂度:O(n),符合题目要求
- 空间复杂度:O(n)
- 代码部分也比较简单,这里就不实现了。
1.2.2 思路2
- 使用异或,这里先给大家补充一下异或的知识。
- 什么是异或?
在逻辑学中,逻辑算符异或(exclusive or)是对两个运算元的一种逻辑析取类型,符号为 XOR 或 EOR 或 ⊕(编程语言中常用^)。
但与一般的逻辑或不同,异或算符的值为真仅当两个运算元中恰有一个的值为真,而另外一个的值为非真。转化为命题,就是:“两者的值不同。”或“有且仅有一个为真。”- 异或的特性:
- 相同的两个数异或的结果为0。
- 异或不分顺序。按位异或,相同为0,相异为1。
- 0和任何数异或结果为该数本身
下面进入正题,解决思路如下:
- 创建一个变量x,并赋初值为0
- x跟输入的数据都异或一遍
- x和0~n之间的数都异或一遍,最后的x即为缺失的数字
C语言代码实现如下:
int missingNumber(int* nums, int numsSize) {
int x = 0;
for (int i = 0; i < numsSize; ++i)
{
x ^= nums[i];
}
for (int j = 0; j < numsSize+1; ++j)
{
x ^= j;
}
return x;
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
2. 轮转数组
点击前往LeetCode查看轮转数组
2.1 题目描述
轮转数组(旋转数组)要求如下:
- 想出三种不同的解决方法
- 时间复杂度O(n)
- 空间复杂度O(1)
2.2 解决思路及代码实现
2.2.1 思路1
- 每次旋转一个
- 旋转K次
- 时间复杂度:O(N^2)
- 空间复杂度:O(1)
- 不符合要求(过不了OJ)
2.2.2 思路2
以空间换时间
- 开辟一个额外的数组
- 把原数组的后k个数据拷贝到新数组的前k个位置
- 把原数组的前n-k个数据拷贝到新数组的后n-k个位置
- 再将新数组拷贝回原数组,旋转结束
- 时间复杂度:O(n)
- 空间复杂度:O(n)
- 不符合要求(但能过OJ)
2.2.3 思路3
- 原地逆置
- 前n-k个逆置
- 后k个逆置
- 整体逆置
- 时间复杂度:O(n)
- 空间复杂度:O(1)
- 符合题意,是最优解法也最难想到
C语言代码实现如下:
int* reverse(int* nums, int left, int right)
{
while (left < right)
{
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
left++;
right--;;
}
return nums;
}
void rotate(int* nums, int numsSize, int k) {
k %= numsSize;
reverse(nums, numsSize - k, numsSize - 1);
reverse(nums, 0, numsSize - k - 1);
reverse(nums, 0, numsSize - 1);
}
学习记录:
- 本篇整理于2022.6.21
- 如果有错误的地方请多多指教
边栏推荐
- DO280管理应用部署--pod调度控制
- Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
- 基于PHP的轻量企业销售管理系统
- STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
- Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
- When ABAP screen switching, refresh the previous screen
- Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
- [daily news]what happened to the corresponding author of latex
- 2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
- Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
猜你喜欢
自動、智能、可視!深信服SSLO方案背後的八大設計
u本位合约和币本位合约有区别,u本位合约会爆仓吗
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
【Hot100】20. 有效的括号
Research on multi model architecture of ads computing power chip
近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧
Nuxt.js数据预取
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
并发编程系列之什么是ForkJoin框架?
随机推荐
process. env. NODE_ ENV
最新NLP赛事实践总结!
How to adjust the size of computer photos to what you want
The picgo shortcut is amazing. This person thinks exactly the same as me
Factory high-precision positioning management system, digital safety production management
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Idea start command line is too long problem handling
Which MySQL functions are currently supported by tablestore in table storage?
自动、智能、可视!深信服SSLO方案背后的八大设计
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
For the sustainable development of software testing, we must learn to knock code?
Preorder, inorder, follow-up of binary tree (non recursive version)
Tensorflow team: we haven't been abandoned
What is the forkjoin framework in the concurrent programming series?
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
Motion capture system for apple picking robot
【Hot100】19. 删除链表的倒数第 N 个结点
When ABAP screen switching, refresh the previous screen
vscode 查找 替换 一个文件夹下所有文件的数据
高端程序员上班摸鱼指南