1351.统计有序矩阵中的负数,代码超级简单,一看就懂0ms

本文介绍了一种高效算法,用于计算一个二维矩阵中负数的数量。该矩阵的每一行和每一列都是非递增排列。算法从矩阵的右上角开始遍历,根据当前元素的正负性调整搜索路径。
class Solution {
    public int countNegatives(int[][] grid) {
        if(grid.length == 0 || grid[0].length == 0) return 0;
        int count = 0;
        // 从右上角开始遍历,如果当前值≥0,由于是非递增,那么前面的都是非负
        // 如果当前的<0,那么剩下的就都是负数了,直接开始下一列
        for(int i = 0, j = grid[0].length - 1; i < grid.length && j >= 0; ){
            if(grid[i][j] >= 0){
                i++;
            }else{
                count += grid.length - i;
                j--;
            }
        }
        return count;
    }
}
实现简单的线性表,用于存储字母和数字元素,长度为100的顺序表list,并提供以下功能: 输出列表:将键盘输入字母与数字进行输出 插入元素:在第3个位置插入数字100。 删除元素:删除第6个位置的元素。 查找元素:查找数字15的位置(如果存在,返回位置,如果不存在,返回None) 输入格式: 例如:11 A 12 B 13 C 14 D 15 E 输出格式: 输出列表,结果:11 A 12 B 13 C 14 D 15 E 插入元素,结果:11 A 100 12 B 13 C 14 D 15 E 删除元素,结果:11 A 100 12 B C 14 D 15 E 查找元素,结果:9 输入样例: 在这给出组输入。例如: 11 A 12 B 13 C 14 D 15 E 输出样例: 在这给出相应的输出。例如: 11 A 12 B 13 C 14 D 15 E 11 A 100 12 B 13 C 14 D 15 E 11 A 100 12 B C 14 D 15 E 9 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB Python (python3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 max_size = 100 class SqList(): def __init__(self): self.elem = [None] * max_size self.length = 0 def list_insert(self,i,e): if i > len(self.elem): raise Exception('空间已满') if i < 1 or i > self.length 1: if i < 1 or i > self.length + 1: #if not 1 <= i <= self.length + 1: raise Exception('位置不合法') for idx in range(self.length - 1,i - 2,-1): self.elem[idx + 1] = self.elem[idx] self.elem[i - 1] = e self.length += 1 #def __str__(self): #return str(self.elem[:self.length]) def list_delete(self,i): # 1 <= i <= self.length if i < 1 or i > self.length: raise Exception('位置不合法') for idx in range(i,self.length): self.elem[idx - 1] = self.elem[idx] self.length -= 1 def locate_elem(self,e): for i,elem in enumerate(self.elem[:self.length]): if elem == e: return i + 1 return None def __str__(self): output = '' #for i in range(self.length): #output += self.elem[i] + '' for i in range(self.length): output += '{0}'.format(self.elem[i]) return output l1 = SqList() for i in range(10): l1.list_insert(i + 1,input()) print(l1) l1.list_insert(3,100) print(l1) l1.list_delete(6) print(l1) print(l1.locate_elem('15'))
11-06
题目 7-375 用户 LWY 提交时间 2025/08/21 15:20:56 编译器 C (gcc) 内存 4364 / 65536 KB 用时 0 / 5000 ms 状态 运行超时 分数 0 / 10 评测时间 2025/08/21 15:20:57 评测详情 测试点 提示 内存(KB) 用时(ms) 结果 得分 sum 4364 5000 运行超时 0 / 10 提交代码 复制内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define MAXN 100010 // 滑动窗口法:找最小长度子数组,和 ≥ s int minSubarrayWithSum(long long *arr, int n, long long s) { int left = 0; long long sum = 0; int min_len = INT_MAX; for (int right = 0; right < n; ++right) { sum += arr[right]; while (sum >= s) { int len = right - left + 1; if (len < min_len) min_len = len; sum -= arr[left++]; } } return (min_len == INT_MAX) ? -1 : min_len; } int minSubmatrixSum(int R, int C, long long s, long long **matrix) { int min_area = INT_MAX; for (int top = 0; top < R; ++top) { long long *sumRow = (long long *)calloc(C, sizeof(long long)); for (int bottom = top; bottom < R; ++bottom) { for (int j = 0; j < C; ++j) { sumRow[j] += matrix[bottom][j]; } 编译器输出 a.c: In function ‘main’: a.c:53:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 53 | scanf("%d", &t); | ^~~~~~~~~~~~~~~ a.c:57:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 57 | scanf("%d %d %lld", &R, &C, &s); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a.c:63:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 63 | scanf("%lld", &matrix[i][j]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
08-22
题目 7-375 用户 LWY 提交时间 2025/08/21 15:19:22 编译器 C (gcc) 内存 4364 / 65536 KB 用时 0 / 5000 ms 状态 运行超时 分数 0 / 10 评测时间 2025/08/21 15:19:25 评测详情 测试点 提示 内存(KB) 用时(ms) 结果 得分 sum 4364 5000 运行超时 0 / 10 提交代码 复制内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define MAXN 100010 // 二分查找函数 int findCeil(long long *arr, int size, long long target) { int low = 0, high = size - 1; int res = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= target) { res = mid; low = mid + 1; } else { high = mid - 1; } } return res; } int minSubarrayWithSum(long long *rowSum, int C, long long s) { long long *prefix_sums = (long long *)malloc((C + 1) * sizeof(long long)); prefix_sums[0] = 0; int prefix_len = 1; long long curr_sum = 0; int min_len = INT_MAX; for (int j = 1; j <= C; ++j) { curr_sum += rowSum[j]; long long target = curr_sum - s; int idx = findCeil(prefix_sums, prefix_len, target); if (idx != -1) { 编译器输出 a.c: In function ‘main’: a.c:91:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 91 | scanf("%d", &t); | ^~~~~~~~~~~~~~~ a.c:95:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 95 | scanf("%d %d %lld", &R, &C, &s); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a.c:101:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 101 | scanf("%lld", &matrix[i][j]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值