首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
矩阵元素查找
[编程题]矩阵元素查找
热度指数:29281
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
已知一个有序矩阵
mat
,同时给定矩阵的大小
n
和
m
以及需要查找的元素
x
,且矩阵的行和列都是从小到大有序的。设计查找算法返回所查找元素的二元数组,代表该元素的行号和列号(均从零开始)。保证元素互异。
数据范围:
,矩阵中的任何元素满足
要求:空间复杂度
,时间复杂度
示例1
输入
[[1,2,3],[4,5,6]],2,3,6
输出
[1,2]
示例2
输入
[[1,2,3]],1,3,2
输出
[0,1]
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(3)
邀请回答
收藏(173)
分享
提交结果有问题?
113个回答
46篇题解
开通博客
棒棒糖🍭201906101800876
发表于 2021-07-16 10:39:28
精华题解
nc86.矩阵元素查找 1. 思路一: (笨办法, 暴力搜索, 不合要求) 直接遍历二维数组即可. class Solution { public: vector<int> findElement(vector<vector<int> > mat, int
展开全文
不会做题的小菜鸡
发表于 2021-07-17 15:16:41
精华题解
思路 暴力搜索,双重循环遍历 依据有序性按照一定顺序来查找 方法一:暴力搜索 双重循环直接遍历所有元素找到指定元素 class Solution { public: vector<int> findElement(vector<vector<int> >
展开全文
LaN666
发表于 2020-12-06 19:29:12
思路分析: 从矩阵的左下角开始,因为每行每列都是有序的。 import java.util.*; public class Finder { public int[] findElement(int[][] mat, int n, int m, int x) { int nn
展开全文
badly1226
发表于 2021-11-23 14:50:07
// write code here int[] result = new int[2]; int row = 0; int col = m - 1; while(row < n && col >= 0) {
展开全文
摸鱼学大师
发表于 2021-07-16 16:58:58
思路: 题目中给的信息: 行和列都是有序的,且元素互异 寻找的是目标值的坐标,从零开始 即这是一个二维矩阵上的查找,查找算法如下: 方法一:暴力搜索 直接两个for循环找到目标值的坐标 class Solution { public: vector<int> findElem
展开全文
OfferCall!
发表于 2021-04-05 16:21:35
由于给定的二维数组具备每行从左到右递增以及每列从上到下递增的特点,当访问到一个元素时,可以排除数组中的部分元素。我们可以从矩阵的右上角元素开始进行查询,如果其等于目标元素则直接返回结果;如果其大于目标元素,则只能往列坐标减少的方向去寻找,其他位置的元素都是大于当前访问的元素的,自然也就大于目标元素;
展开全文
星云·忒弥斯
发表于 2021-08-16 17:14:10
class Solution {public: vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) { // write code here
展开全文
godhands
发表于 2022-02-07 23:20:20
描述 题目描述 给了我们一个二维数组, 和他的行和列, 给定我们一个值, 让我们在这个矩阵中找到等于这个值的横纵坐标, 并作为一个vectorvectorvector返回 样例解释 样例输入 [[1,2,3],[4,5,6]],2,3,6 如图所示 所以我们的样例输出就是 [1,2] 题解 解
展开全文
宫水三叶的刷题日记
发表于 2021-08-25 15:24:03
朴素解法 一个朴素的做法是,直接对矩阵 进行遍历。 由于题目确保有解,因此当我们找到 时,直接将二元组 作为答案输出即可。 代码: import java.util.*; public class Solution { public int[] findElement(int[][]
展开全文
牛客983371096号
发表于 2021-11-13 17:01:07
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @param n int整型 # @para
展开全文
总之就是非常可爱
发表于 2022-02-27 16:10:42
class Solution { public: vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
展开全文
Maokt
发表于 2021-07-29 14:24:39
算法思想一:暴力循环法 解题思路: 主要是对矩阵元素进行全部遍历查找目标值,采用双层遍历的方式 代码展示: Python版本 class Solution: def findElement(self, mat, n, m, x): # write code here
展开全文
问题信息
二分
分治
难度:
113条回答
173收藏
15848浏览
热门推荐
通过挑战的用户
查看代码
牛客68842...
2022-12-30 10:18:10
剑胆琴心_liu
2022-11-09 21:36:38
在做ppt的伊...
2022-09-21 09:50:11
在写周报的你很...
2022-09-16 17:38:06
牛客45533...
2022-09-15 14:40:27
相关试题
航海
排序
思维
二分
评论
(1)
远亲不如近邻
排序
二分
评论
(13)
要求先给出思路,然后写代码,可以使...
搜狐
查找
分治
评论
(3)
编译方法中,动态存储分配的含义是:()
编译和体系结构
评论
(2)
来自
乐视2017秋招开发工程...
闪速存储器能提供高性能、低功耗、字...
编程基础
评论
(1)
矩阵元素查找
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ public int[] findElement (int[][] mat, int n, int m, int x) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型vector
> * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型vector */ vector
findElement(vector
>& mat, int n, int m, int x) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @param n int整型 # @param m int整型 # @param x int整型 # @return int整型一维数组 # class Solution: def findElement(self , mat , n , m , x ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ public List
findElement (List
> mat, int n, int m, int x) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ function findElement( mat , n , m , x ) { // write code here } module.exports = { findElement : findElement };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @param n int整型 # @param m int整型 # @param x int整型 # @return int整型一维数组 # class Solution: def findElement(self , mat: List[List[int]], n: int, m: int, x: int) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ func findElement( mat [][]int , n int , m int , x int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param matRowLen int mat数组行数 * @param matColLen int* mat数组列数 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* findElement(int** mat, int matRowLen, int* matColLen, int n, int m, int x, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @param n int整型 # @param m int整型 # @param x int整型 # @return int整型一维数组 # class Solution def findElement(mat, n, m, x) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ def findElement(mat: Array[Array[Int]],n: Int,m: Int,x: Int): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ fun findElement(mat: Array
,n: Int,m: Int,x: Int): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ public int[] findElement (int[][] mat, int n, int m, int x) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ export function findElement(mat: number[][], n: number, m: number, x: number): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ func findElement ( _ mat: [[Int]], _ n: Int, _ m: Int, _ x: Int) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @param m int整型 * @param x int整型 * @return int整型一维数组 */ pub fn findElement(&self, mat: Vec
>, n: i32, m: i32, x: i32) -> Vec
{ // write code here } }
[[1,2,3],[4,5,6]],2,3,6
[1,2]
[[1,2,3]],1,3,2
[0,1]