- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在解决我所在的 leetcode 问题之一(最长回文)
<强>1。使用数组实现
public int longestPalindrome(String s) {
int[] charFreq = new int[58];
for(char sChar : s.toCharArray()){
++charFreq[sChar - 'A'];
}
int charsWithOddFreq = 0;
for(int freq : charFreq){
if((freq & 1) != 0){
charsWithOddFreq++;
}
}
int len = s.length();
return (charsWithOddFreq == 0) ? len : (len - charsWithOddFreq + 1);
}
在哪里, 1 <= s.length <= 2000
<强> s consists of lowercase and/or uppercase English letters only.
但是,说上面这个程序的时间复杂度是O(n + m)
是否正确? , 在这里 n is the length of the given string(s)
和 m is the size of array taken to store frequency(charFreq)
还是只说 O(n)
更好?因为我们可以忽略常数因子 (m),它不依赖于输入字符串的大小,并且每个输入的迭代总是相同的(即 58)。
简而言之,就是O(n + m) ~ O(n)
在这种情况下?
<强>2。使用 Map 实现相同
public int longestPalindrome(String s) {
Map<Character, Integer> charFreq = new HashMap<>();
for(char sChar : s.toCharArray()) {
charFreq.put(sChar, charFreq.getOrDefault(sChar, 0) + 1);
}
int charWithOddFreq = 0;
for(int val : charFreq.values()) {
if((val & 1) != 0) {
++charWithOddFreq;
}
}
int len = s.length();
return (charWithOddFreq == 0) ? len : (len - charWithOddFreq + 1);
}
使用Map,时间复杂度应该是O(n + m)
,因为 m 会因输入字符串而异,因为它取决于输入大小。但是,在这种情况下(使用 Map 时)我的问题是我们可以说 O(n + m)
作为O(n)
, 当 n > m
?因为根据限制,即
1 <= s.length <= 2000
和 s consists of lowercase and/or uppercase English letters only.
所以,字符串的长度(n) > 大小写字母的个数(m)简而言之,就是O(n + m) ~ O(n)
在这种情况下也是如此吗?
最佳答案
A1。自 m
在你的第一个算法中是一个常量 (58),这是 O(n)
时间。这O(n + constant)
与O(n)
相同.
A2。你的第二个算法也是 O(n)
时间。
Using Map, the time complexity should be O(n + m), as m will vary for input string as it is dependent on the input size.
您没有明确说明,但在这种情况下,n
是 s
中的字符数, 和 m
是 s
中不同字符的数量.
那些可变变量m
和 n
不是独立的。事实上m
将始终小于或等于 n
比n
.
但是m + n
<= 2n
, 和 O(2n)
与O(n)
相同.所以O(m + n)
与O(n)
相同在这种情况下。
(以上不是严格的证明......但它应该足以突出你推理中的缺陷。如果你想要严格的证明,它非常简单,尽管很乏味。)
并在(更正的)标题中回答您的问题:
Can we say O(n + m) is equivalent to O(n), when m is constant or when n > m
是的。见上文。
请注意,两个版本具有相同(时间)复杂度的事实并不意味着它们的性能相同。
关于java - 我们可以说 O(n + m) 等价于 O(n),当 m 是常数或当 n > m,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71613273/
我不明白基因编程的人工智能是如何实现的。可以确定最终方程中何时应该有一个常数。如果我采用公式 F(m) = ma; F(m) = m9.8,A.I.知道真正的数字 9.8 到底是多少吗?据我了解,您实
我的批次大小是可变的,所以我的所有输入都是以下形式 tf.placeholder(tf.float32, shape=(None, ...) 接受可变的批量大小。但是,如何创建具有可变批处理大小的常数
我有一个一维 Numpy 数组 A长度N .对于每个元素 x在数组中,我想知道数组中所有元素在[x-eps]范围内的比例是多少; x+eps ],其中 eps是一个常数。 N数量级为 15,000。
如果我像这样在文件中设置一些常量: 'use strict'; angular.module('balrogApp.config', []) .constant('balrogConfig',
谁能给我一个常量 r 值的例子? 因为显然即使文字也是右值而不是常量右值。 最佳答案 const T f(); 根据该定义,表达式 f() 是 const T 类型的右值表达式,即常量右值。 关于c+
我正在开发一个 Angular 应用程序,我想创建一个配置文件。根据我的阅读,我应该使用 Angular 常数。 所以我尝试创建我的常量。从我读到的内容( here , here 以及 here +
const int N = 100; void function1(int array[]){ // ... } void function2(int arra
考虑以下(工作)片段: Eigen::ArrayXd x (8); x > y (x.data(), 2, 4); 这也是可行的: const Eigen::ArrayXd const_x = x;
我知道你可以使用: #define _USE_MATH_DEFINES 然后: M_PI 得到常量 pi。但是,如果我没记错的话(欢迎评论)这是编译器/平台相关的。那么,当我将 pi 常量从 Linu
为什么这段代码无法编译? package main const a = 1.000001 const base = 0 const b = a+base func main() { f(b)
为什么下面的代码每条语句都引用了大O常量(这里我为了约定使用1)? 我的意思是,如果数组大小变大,时间复杂度可能会变大,对吗?而且总数会越来越大,会不会影响复杂度? 伪代码: def find_sum
我正在尝试创建一个函数来填充多个系列中缺失的数字,具有不同的数字比例,同时为每个系列生成一个常量列。 from tika import parser import pandas as pd impor
我正在尝试近似 e 的值(~2.7) 由此定义,对于每个第 n 项 在Python中使用递归函数。 到目前为止我已经得到了这个, def NapierConstant(runs): retur
系统提示我输入代码以某种方式打印 Champerowne 常量系列。 (实际使用它!)所以我们想找到这个系列的第 n 个数字:1234567891011121314...这是我的代码: #inclu
我正在学习硕士定理的期中考试,我遇到了案例 2 的示例,其中 k > 0。除了常数及其递增或计算方式之外,我了解有关定理的所有内容。 Case 2状态:T(n) = Θ(nlogba logk+1n)
问题实例:无向无权图 G=(V,E)。两个源节点a和b,两个目的节点c和d以及一个常数D(完全正数)。(我们可以假设lambda(c,d),lambda(a,b)>D,当lambda(x,y ) 是
我有一张很大的 table 。 它目前在 MySQL 数据库中。 我使用django。 我需要迭代 每个表的元素来预先计算一些特定的数据(也许如果我更好的话,我可以这样做,但这不是重点)。 我想在不断
VBScript 常数 常数是具有一定含义的名称,用于代替数字或字符串,其值从不改变。VBScript 定义了许多内部常数。详细信息,请参阅 VBScript 语言参考。 创建常数 您可以使用
我正在学习 javascript,我发现一个文件中有许多用操作数“:”而不是“=”完成的赋值。尽管问题的标题是这样,但我也在非常量中看到过它。那有什么意义呢?这里的“:”操作数是什么意思?谢谢。 v
鉴于应用程序启动: angular.module("starter", [ "ionic" ]) .constant("DEBUG", true) .run(function() {
我是一名优秀的程序员,十分优秀!