当前位置:网站首页>刷题笔记-查找
刷题笔记-查找
2022-07-21 02:35:00 【[email protected]】
对于查找的题目,一般来说可以从暴力解法和二分查找这两个方法着手。
一、在升序的数组中,找到一个目标值
给定一个 元素升序的、无重复数字的整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标(下标从 0 开始),否则返回 -1
解法一:暴力解法
int search(vector<int>& nums, int target) {
// write code here
// 暴力解法
for(int i=0;i<nums.size();i++)
{
if(nums[i] == target)
return i;
}
return -1;
}
解法二:
int search(vector<int>& nums, int target) {
// write code here
int l= 0;
int r = nums.size()-1;
if(!nums.size())
{
return -1;
}
while(l<=r)
{
int mid = l + (r - l)/2;
if(nums[mid] < target)
{
l = mid +1;
}
else if(nums[mid] > target)
{
r = mid -1;
}
else
{
return mid;
}
}
return -1;
}
二、二维数组中的查找
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// 暴力解法
for(int i = 0;i< array.size();i++)
for(int j = 0;j< array[i].size();j++)
{
if(array[i][j] == target)
{
return true;
}
}
return false;
}
};
解法二:二分查找
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// 二分查找
for(int i=0;i<array.size();i++){
int low=0;
int high=array[i].size()-1;
while(low<=high){
int mid=(low+high)/2;
if(target>array[i][mid])
low=mid+1;
else if(target<array[i][mid])
high=mid-1;
else
return true;
}
}
return false;
}
};
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int rows = array.size();
int cols = array.size();
int i = rows-1; // 左下角元素坐标
int j = 0;
if(!array.size())
return false;
if(!array[0].size())
return false;
while(i>=0 and array[i][j])
{
if(target < array[i][j])
{
i--; // 查找的元素较小,往上的第一个数字开始找
}
else if(target>array[i][j])
j++; // 查找的元素较大,往后找
else
return true; // 找到
}
return false;
}
};
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42178122/article/details/125894660
边栏推荐
- Introduction to C language --- operators
- OSPF基础
- Zhongang Mining: four trends of fluorite industry development
- 这还是我最熟悉的package.json吗?
- Digital twin community management system, construction of nine application scenarios
- [translation] principles for designing and deploying extensible applications on kubernetes
- Interesting kotlin 0x0D: intarray vs array < int>
- Stm32f40x minimum system
- 程序初学者推荐学哪种编程语言比较好?
- ClickHouse表引擎
猜你喜欢
Qt development skills and three problems
Application cases and common technologies of digital twins
LeetCode:1260. 二维网格迁移【一维展开+拼接】
Design of the multi live architecture in different places of the king glory mall
Digital twin technology creates a visualization solution for smart mines
Linux中安装Redis
ORACLE导出表数据-dmp文件
牛客网刷题篇
The risk control data (model) is not bothered because of the processing skills in this flow process
手工遍历二叉树的技巧
随机推荐
Oracle startup command
Digital twin technology offshore wind farm solution
General paging (encapsulation of paging code)
Description du développement de l'ESB en combinaison avec la plateforme UMC Cloud
Introduction to C language --- operators
Part I - Fundamentals of C language_ 10. Document operation
ClickHouse表引擎
ESB結合UMC雲平臺開發說明
Application cases under the digital twin of industry 4.0
手工遍历二叉树的技巧
第一部分—C语言基础篇_10. 文件操作
推荐效果不如意,不如试试飞桨图学习
C语言类型转换
The longest valid bracket of question 32 in C language. Implement with stack
Practice of online problem feedback module (IX): realize image upload function (Part 2)
面试 02
鸿蒙 harmonyos DevEco-Studio 报错 UNINSTALL_FAILED_INTERNAL_ERROR
dp---背包问题
教程篇(7.0) 03. FortiClient EMS配置和管理 * FortiClient EMS * Fortinet 网络安全专家 NSE 5
powermock实战