当前位置:网站首页>Leetcode 1351. Negative numbers in statistical ordered matrices
Leetcode 1351. Negative numbers in statistical ordered matrices
2022-04-23 20:26:00 【Die in a trap】
1351、 Count the negative numbers in an ordered matrix
1) Title Description
To give you one m * n
Matrix grid
, The elements of a matrix, whether by row or column , All in non increasing order . Please count and return grid
in negative Number of .
Example 1:
Input :grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output :8
explain : Common matrix 8 Negative numbers .
Example 2:
Input :grid = [[3,2],[1,0]]
Output :0
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 100
-100 <= grid[i][j] <= 100
2) analysis
Traverse .
- Traverse to the first element of each line , Judge positive and negative , If negative , The result directly adds the product of the length of one line and the number of remaining lines , Exit traversal ;
- If the first element of the current line is nonnegative , Traverse the row , Judge the positive and negative of each element , If negative , The result is added with the number of elements remaining in the current line , Exit the traversal of the current line .
Traveled through , Intra bank dichotomy .
- Traverse to the first element of each line , Judge positive and negative , If negative , The result directly adds the product of the length of one line and the number of remaining lines , Exit traversal ;
- If the first element of the current line is nonnegative , Find the position of the first negative number by two points , The result is added with the number of elements remaining in the current line , Exit the search of the current line .
3)C++
Code
Traverse
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int res=0;
for(int i=0;i<grid.size();i++){
if(grid[i][0]>=0){
for(int j=0;j<grid[0].size();j++){
if(grid[i][j]<0){
res+=grid[0].size()-j;
break;
}
}
}
else{
res+=grid[0].size()*(grid.size()-i);
break;
}
}
return res;
}
};
Traveled through , Intra bank dichotomy .
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int res=0;
int m=grid.size();
int n=grid[0].size();
for(int i=0;i<m;i++){
if(grid[i][0]>=0){
if(grid[i][n-1]>=0)
continue;
else{
int left=0;
int right=n-1;
while(left<=right){
int mid=(left+right)/2;
if(grid[i][mid]>=0)
left=mid+1;
else
right=mid-1;
}
res+=grid[i][left]<0?n-left:0;
}
}
else{
res+=n*(m-i);
break;
}
}
return res;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107752.html
边栏推荐
- Monte Carlo py solves the area problem! (save pupils Series)
- Rt-1052 learning notes - GPIO architecture analysis
- Use the rolling division method to find the maximum common divisor of two numbers
- [PTA] l1-006 continuity factor
- The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
- Installation and use of NVM
- [PTA] l2-011 play with binary tree
- Tensorflow 2 basic operation dictionary
- How does onlyoffice solve no route to host
- Intersection calculation of straight line and plane in PCL point cloud processing (53)
猜你喜欢
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Recognition of high-speed road signs by Matlab using alexnet
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
DTMF dual tone multi frequency signal simulation demonstration system
Rt-1052 learning notes - GPIO architecture analysis
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
随机推荐
Notes of Tang Shu's grammar class in postgraduate entrance examination English
LeetCode 1346、检查整数及其两倍数是否存在
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
Investigate why close is required after sqlsession is used in mybatties
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
内网渗透之DOS命令
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
Experience of mathematical modeling in 18 year research competition
Es index (document name) fuzzy query method (database name fuzzy query method)
Es error: request contains unrecognized parameter [ignore_throttled]
2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
What is the difference between a host and a server?
Monte Carlo py solves the area problem! (save pupils Series)
The ODB model calculates the data and outputs it to excel
How about CICC fortune? Is it safe to open an account
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Research on open source OCR engine
Matlab analytic hierarchy process to quickly calculate the weight
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions