【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows
题目如下:
给你一个矩阵
mat,其中每一行的元素都已经按 递增 顺序排好了。请你帮忙找出在所有这些行中 最小的公共元素。如果矩阵中没有这样的公共元素,就请返回
-1。示例:
输入:mat = [[1,2,3,4,5],[2,4,5,8,10],[3,5,7,9,11],[1,3,5,7,9]]
输出:5提示:
1 <= mat.length, mat[i].length <= 5001 <= mat[i][j] <= 10^4mat[i]已按递增顺序排列。
解题思路:我最初的想法遍历整个矩阵,记录每一个值出现的个数,如果某个值出现的次数恰好等于矩阵的行数,说明这个值是一个公共的值。但是考虑到可能一行内有重复值的情况,改成用二进制的方式记录每个值出现在的行数,这样就可以避免重复计算。
代码如下:
class Solution(object):
def smallestCommonElement(self, mat):
"""
:type mat: List[List[int]]
:rtype: int
"""
val = [0] * (10**4+1)
for i in range(len(mat)):
for j in range(len(mat[i])):
val[mat[i][j]] |= 2**i
for i in range(len(val)):
if val[i] == 2**len(mat) - 1:
return i
return -1
【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows的更多相关文章
- 【leetcode&CN&竞赛】1196.How Many Apples Can You Put into the Basket
题目如下: 楼下水果店正在促销,你打算买些苹果,arr[i] 表示第 i 个苹果的单位重量. 你有一个购物袋,最多可以装 5000 单位重量的东西,算一算,最多可以往购物袋里装入多少苹果. 示例 1: ...
- LeetCode 1257. Smallest Common Region
原题链接在这里:https://leetcode.com/problems/smallest-common-region/ 题目: You are given some lists of region ...
- Smallest Common Multiple-freecodecamp算法题目
Smallest Common Multiple 1.要求 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 2.思路 设定一个twoMultiple(a,b)函数,求出输入两个参数的最小公 ...
- [Intermediate Algorithm] - Smallest Common Multiple
题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...
- 【leetcode】1257. Smallest Common Region
题目如下: You are given some lists of regions where the first region of each list includes all other reg ...
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- Leetcode Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
- [LeetCode&Python] Problem 744. Find Smallest Letter Greater Than Target
Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
随机推荐
- WebSocket-Node
WebSocket Client & Server Implementation for Node 参考资料:[https://github.com/theturtle32/WebSocket ...
- C++编译错误提示 [Error] name lookup of 'i' changed for ISO '
在VC 6 中,i的作用域范围是函数作用域,在for循环外仍能使用变量i 即: for (int i = 0; i < n; ++i) { //…… } cout<< ...
- 修改了Mysql密码后连接不到服务且无报错信息解决方法以及修改密码方法
安装MYSQL后更改了root的密码后用 net start mysql 启动时出现:无法启动,无报错信息 使用以下命令:1.管理员方式cmd进入mysql安装目录的bin目录下2.执行命令:mysq ...
- 【VS开发】【C++语言】reshuffle的容器实现算法random_shuffle()的使用
假设你需要指定范围内的随机数,传统的方法是使用ANSI C的函数random(),然后格式化结果以便结果是落在指定的范围内.但是,使用这个方法至少有两个缺点.首先,做格式化时,结果常常是扭曲的,所以得 ...
- PHP 静态变量的介绍
PHP静态变量 变量在程序里是生命周期,存储在栈上,函数执行完就会被释放,而静态变量不同之处在于,函数执行完后并不会被释放,而是保留到下次函数调用,直到程序执行完,才从栈上释放 在PHP中有3类全局变 ...
- javascript 数据类型 undefined 和null
数据类型 undefind null boolean number string object type of 功能:检测变量类型 语法:type of 变量或 type of (变量) consol ...
- spring boot-16.使用redis做缓存
spring boot 自动配置了多种 缓存管理器,按照下面的顺序查找,如果容器中有相应的组件,则使用相应的缓存管理器. Generic JCache (JSR-107) EhCache 2.x Ha ...
- CPU飙高,频繁GC,怎么排查?
处理过线上问题的同学基本上都会遇到系统突然运行缓慢,CPU 100%,以及Full GC次数过多的问题.当然,这些问题的最终导致的直观现象就是系统运行缓慢,并且有大量的报警. 本文主要针对系统运行缓慢 ...
- centos7 下网卡的配置
一般通过修改配置文件的方式去修改: 网卡配置文件位置 /etc/sysconfig/network-scripts/ifcfg-ens33 DNS配置文件位置 /etc ...
- Labeling Balls POJ - 3687 优先队列 + 反向拓扑
优先队列 + 反向拓扑 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...