- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/majority-element-ii/description/
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times.
Note: The algorithm should run in linear time and in O(1) space.
Example 1:
Input: [3,2,3]
Output: [3]
Example 2:
Input: [1,1,1,3,3,2,2,2]
Output: [1,2]
找出一个数组中出现次数超过⌊ n/3 ⌋
次的所有数字。
虽然不符合题目的要求,但是一般情况下,对空间复杂度要求的题目都不用管它的空间要求。这样很快就能写出来。
时间复杂度是O(N),空间复杂度是O(N)。
Python代码如下:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
N = len(nums)
count = collections.Counter(nums)
res = []
for n, t in count.items():
if t > N / 3:
res.append(n)
return res
1 2 3 4 5 6 7 8 9 10 11 12 13
C++代码如下:
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
const int N = nums.size();
unordered_map<int, int> count;
for (int n : nums)
++count[n];
vector<int> res;
for (auto& c : count) {
if (c.second > N / 3) {
res.push_back(c.first);
}
}
return res;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
题目要求的是线性时间和常量的空间,和169. Majority Elementopen in new window基本一样的。169题使用一次遍历就找出了超过出现次数超过一半的数字。这个题需要在这个基础上更进一步。首先我们肯定知道数组中出现次数超过⌊ n/3 ⌋
次的最多有两个!因为如果3个的话,这三个数字的总次数 > 3×⌊ n/3 ⌋
= n,不可能的。所以我们对这个题的做法同样使用摩尔投票法,先使用两个变量分别保存次数最多和次多的就可以了。然后我们还需要再过一遍数组,判断次数最多和次多的是不是超过了⌊ n/3 ⌋
次,把超过的数字返回就行了。
踩到的坑:
1、 在第一个for循环中,必须先判断是不是已经和已有的相等,如果不满足的情况下才能判断是不是次数为0比如题目中给的例子[1,1,1,3,3,2,2,2]
,如果先判断cm和cn的次数是不是0,那么会把m和n分别都设置成了1而我们的目的是m和n分别代表两个不同的数字,所以应该先做是不是和已有的数字相等的判断;
2、 统计次数的时候需要用if和elseif,不能两个if这个是因为我们把m和n都初始化成了0,对于[0,0,0]
这个测试用例,如果两个if会导致结果是[0,0]
;
时间复杂度是O(N),空间复杂度是O(1)。
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
N = len(nums)
m = n = cm = cn = 0
for num in nums:
if num == m:
cm += 1
elif num == n:
cn += 1
elif cm == 0:
m = num
cm = 1
elif cn == 0:
n = num
cn = 1
else:
cm -= 1
cn -= 1
cm = cn = 0
for num in nums:
if num == m:
cm += 1
elif num == n:
cn += 1
res = []
if cm > N / 3:
res.append(m)
if cn > N / 3:
res.append(n)
return res
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
C++代码如下:
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int m = 0, n = 0, cm = 0, cn = 0;
for (int i : nums) {
if (m == i) {
++cm;
} else if (n == i) {
++cn;
} else if (cm == 0) {
m = i;
cm = 1;
} else if (cn == 0) {
n = i;
cn = 1;
} else {
--cm;
--cn;
}
}
cm = cn = 0;
for (int i : nums) {
if (i == m)
++cm;
else if (i == n)
++cn;
}
vector<int> res;
const int N = nums.size();
if (cm > N / 3)
res.push_back(m);
if (cn > N / 3)
res.push_back(n);
return res;
}
};
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
169. Majority Elementopen in new window
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
当我查看 IIS 日志文件时,我可以在一天中的不同时间看到标题行被写入日志文件。我唯一一次看到这种情况发生是在 IIS 重置时;或启动。 例如下面的标题行; #Software: Microsoft
有谁知道为什么以下 web.conig 更改不起作用: 在我添加 javascript 文件后仍然没有被压缩(gzip)。我折腾了几天,因为一开始我真的不知道问题是
目前,我们所有的网络应用程序都将其应用程序池标识设置为 ApplicationPoolIdentity。现在,当一个应用程序需要访问某些服务器上的某些资源时,比如添加/读取某些文件,该应用程序会在代码
在 IIS(特别是 6.0)中,在“主目录”选项卡下,如果我更改本地路径,是否会导致 IIS 重新启动或应用程序池回收? 相关的,是否有概述 IIS 元数据库的哪些更改将触发重新启动或应用程序池回收的
只要更改了任何 web.config 文件,AFAIK IIS 就会重新启动。 我创建了自己的配置文件(my.config,层次结构略有不同)。是否有可能让 IIS 自动(自动神奇地 :))重新启动,
你好, 我正在寻找 IIS 日志中列出的浏览器条目列表,以帮助识别访问我们网站的不同版本的浏览器。也许没有一个列表,而是一个应该用来识别不同浏览器及其版本的算法。 请注意,我不是在寻找日志分析器,而是
我想知道上次重新启动 iis 是什么时候。 IIS 是否保留了此日志,我在哪里可以找到此日志? 谢谢 最佳答案 打开事件查看器,导航到“Windows 日志”->“系统” 在右侧的“操作” Pane
在全新安装的带有 IIS 7 的 Windows Server 2008 R2 上,它会在 C:\inetpub\wwwroot 中创建一个默认网站。 .我确信对于存储网站文件的好位置有很多主观意见,
我已按照 this question 中的说明在 IIS 中实现动态内容压缩。 . 当我发送请求时,我看不到响应被 gzip 压缩的任何证据: IIS版本为7.5 有什么明显的我应该检查以解决此问题吗
这是场景: 服务器 A 托管“主”应用程序 (www.example.com) 服务器 B 托管支持应用程序 (b.example.com) 它们通过 192.* 地址在内部相互连接,并且都可以通过
是否有任何替代 IIS 管理 UI 可用于从程序重置 IIS?.. 现在我们已经创建了一个批处理文件,如果 iis 重置并每小时安排一次...... 我只是想要一些东西,这样我们就不能重置 iis..
我的 Windows Server 在 IIS 7.5 上运行 Web 应用程序。 我想知道为什么 IIS 应用程序池使用大量内存。 请参阅我从服务器捕获的屏幕截图。 在名为 TEST 2.0(.NE
我在 IIS 7.5 上运行的 ASP Classic 站点上收到服务器错误。 我将“向浏览器发送错误”设置为 True ,但是我仍然收到以下错误屏幕: 最佳答案 IIS 正在劫持您的经典 ASP 应
我正在尝试通过IIS上的反向代理连接到websockets服务器(websockify)。 IIS和Websockets服务器位于同一台物理服务器上(Windows Server 2012 R2,II
我需要让我的开发机器从本地机器商店读取证书 为此,我需要运行winhttpcertcfg.exe并指定我要提升的帐户 该帐户用于IIS表示什么? (对于IIS,它将是IWAM_MachineName)
我正在阅读特定命令(SET-WEBCONFIGURATIONPROPERTY),但出现"is not recognized as a cmdlet"错误。给我的建议是,即使我使用的是SharePoin
我已经为 IIS8 安装了静态和动态压缩并启用了它。 当我的CPU性能100%时,我的GZIP IIS不工作,为什么? 我可以清楚地看到 GZIP 没有在 Live HTTP header 中运行。我
我已经从官方镜像(https://hub.docker.com/r/microsoft/iis/)运行了IIS 在Windows Server 2016上 有什么方法可以从IIS管理器连接到该IIS,
你好, 如果 iis 未运行,我想编写一个状态为的代码。 最佳答案 从命令行启动 IIS: iisreset /start 从命令行停止 IIS: iisreset /stop 重新启动(停止和启动)
我已经为 IIS8 安装了静态和动态压缩并启用了它。 当我的CPU性能100%时,我的GZIP IIS不工作,为什么? 我可以清楚地看到 GZIP 没有在 Live HTTP header 中运行。我
我是一名优秀的程序员,十分优秀!