当前位置:网站首页>The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
2022-07-30 19:02:00 【snowflakes】
很多小伙伴为了刷题发愁
今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
各大互联网大厂面试真题.从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,solo全场面试官
一:搜索旋转排序数组
1.题目
2.代码实现
class Solution {
public:
int search(vector<int>& nums, int target) {
int left=0;
int right =nums.size()-1;
int mid;
while(left<=right)
{
mid = (left+right)/2;
if(nums[0]>target)//当twhen the array on the right
{
if(nums[mid]>=nums[0]) //当mid左边时,需要将left=mid+1
nums[mid] = -10001;
}
else//当twhen the array on the left
{
if(nums[mid] < nums[0]) //当mid右边时,需要将right = mid-1;
nums[mid] = 10001;
}
if(nums[mid] > target)
right = mid-1;
else if(nums[mid] <target)
left = mid+1;
else
return mid;
}
return -1;
}
};
3.思路和注意事项
- The idea is to simulate binary search to achieve
- 普通的二分查找是
if(nums[mid] > target)
right = mid-1;
else if(nums[mid] <target)
left = mid+1;
else
return mid; - nums[mid] > target时, right = mid-1; 所以我们可以用 nums[mid] = 10001;来模拟 这种情况.
- When we find that in special casesright 要变成mid-1时,我们就可以用 nums[mid] = 10001;来模拟 这种情况.
- See the code comments for details(主要是看mid和t在哪一边)
二:链表内指定区间反转
1.题目
2.代码实现
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */
ListNode* reverseBetween(ListNode* head, int m, int n) {
// write code here
ListNode* res =new ListNode(0);
ListNode* cur = head;
ListNode* pre = res;
res->next= head;
for(int i=1;i<m;i++)
{
pre =cur;
cur =cur->next;
}
for(int i=m;i<n;i++)
{
ListNode* tem =cur->next;
cur->next = tem->next;
tem->next = pre->next;
pre->next = tem;
}
return res->next;;
}
};
3.思路和注意事项
The main idea is to reverse again and again
- It should be noted that a virtual head node should be set,In case the head node changes
ps
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
- 运营 23 年,昔日“国内第一大电商网站”黄了...
- 沉浸式体验科大讯飞2022消博会“官方指定产品”
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- 延时队列优化 (2)
- nlohmann json 使用指南【visual studio 2022】
- Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
- Spark学习:编译Spark项目时遇到的报错
- 【PHPWord】Quick Start of PHPWord in PHPOffice Suite
- MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
- VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
猜你喜欢
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
vxe-table实现复选框鼠标拖动选中
深化校企合作 搭建技术技能人才成长“立交桥”
redis
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
MongoDB打破了原则引入SQL?
深入浅出边缘云 | 3. 资源配置
NXP IMX8QXP更换DDR型号操作流程
What is the value of biomedical papers? How to translate the papers into Chinese and English?
[TypeScript]编译配置
随机推荐
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
AWS console
第一次进入小程序判断
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
MySql中@符号的使用
golang日志库zerolog使用记录
Spark学习:编译Spark项目时遇到的报错
SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
WEBSOCKETPP使用简介+demo
你好,我的新名字叫“铜锁/Tongsuo”
Vulkan开启特征(feature)的正确姿势
Swiper轮播图片并播放背景音乐
AI基础:图解Transformer
怎么样的框架对于开发者是友好的?
Anaconda Navigator stuck on loading applications
Codeblocks + Widgets 创建窗口代码分析
设计消息队列存储消息数据的 MySQL 表格
几个GTest、GMock的例子
【刷题篇】计算质数
第4章 控制执行流程