当前位置:网站首页>牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
2022-07-30 18:59:00 【雪芙花】
很多小伙伴为了刷题发愁
今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
各大互联网大厂面试真题。从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,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)//当t在右边的数组时
{
if(nums[mid]>=nums[0]) //当mid左边时,需要将left=mid+1
nums[mid] = -10001;
}
else//当t在左边的数组时
{
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.思路和注意事项
- 思路是模拟二分查找来实现的
- 普通的二分查找是
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;来模拟 这种情况。
- 当我们找到在特殊情况下的right 要变成mid-1时,我们就可以用 nums[mid] = 10001;来模拟 这种情况。
- 具体的情况看代码注释(主要是看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.思路和注意事项
主要思路就是一次一次的反转
- 需要注意的是要设虚拟头节点,以防头节点的改变的情况
ps
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
- The sixteenth issue of eight-part article Balabala said (MQ)
- 【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
- Hello, my new name is "Bronze Lock/Tongsuo"
- "Ruffian Heng Embedded Bimonthly" Issue 59
- Anaconda Navigator stuck on loading applications
- 延时队列优化 (2)
- OneFlow源码解析:Op、Kernel与解释器
- 二分答案裸题(加一点鸽巢原理)
- Codeblocks + Widgets 创建窗口代码分析
- 你好,我的新名字叫“铜锁/Tongsuo”
猜你喜欢

攻防世界web-Cat

Pytorch foundation -- tensorboard use (1)

kotlin的by lazy

自然语言处理nltk

scrapy基本使用

cocos creater 热更重启导致崩溃

After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest

kotlin by lazy

6块钱1斤,日本公司为何来中国收烟头?
随机推荐
mysql的多实例
Node encapsulates a console progress bar plugin
荐号 | 对你有恩的人,不要请吃饭来报答
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
第4章 控制执行流程
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
C# wpf borderless window add shadow effect
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
架构师如何成长
CCNA-子网划分(VLSM)
Go system collection
Scala学习:类和对象
【每日一道LeetCode】——191. 位1的个数
vxe-table实现复选框鼠标拖动选中
6块钱1斤,日本公司为何来中国收烟头?
Redis for infrastructure
Pytorch基础--tensorboard使用(一)
二分答案裸题(加一点鸽巢原理)
Vulkan开启特征(feature)的正确姿势
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

