当前位置:网站首页>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
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
- Swiper轮播图片并播放背景音乐
- nlohmann json 使用指南【visual studio 2022】
- AWS 控制台
- NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
- Does the satellite phone communicate directly with the satellite or through a ground station?
- The use of terminal split screen tool Terminalx
- Chapter 14 Type Information
- 【Prometheus】Prometheus联邦的一次优化记录[续]
- 【刷题篇】计算质数
- SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
猜你喜欢

Fixed asset visualization intelligent management system

Does the satellite phone communicate directly with the satellite or through a ground station?

Pytorch基础--tensorboard使用(一)

6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?

MySql中@符号的使用

AI基础:图解Transformer

经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法

Codeblocks + Widgets create window code analysis

kotlin by lazy

What is the value of biomedical papers? How to translate the papers into Chinese and English?
随机推荐
6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
监听开机广播
Scala学习:breakable
C# wpf 无边框窗口添加阴影效果
requet.getHeader("token") is null
又一家公司面试的内容
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
7.29模拟赛总结
Scala学习:类和对象
AI基础:图解Transformer
【刷题篇】计算质数
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
The use of @ symbol in MySql
【科普】无线电波怎样传送信息?
Basic use of scrapy
6块钱1斤,日本公司为何来中国收烟头?
VBA批量将Excel数据导入Access数据库
MongoDB打破了原则引入SQL?
自己需要努力
LeetCode Exercise - Two Questions About Finding Sum of Array Elements

