是否count()真的计算了PHP数组的所有元素,或者这个值是否缓存在某处并且只是被检索?
PHP array是PHP的核心功能之一.它是稀疏的,允许同一数组中的多类型键,并支持集合,字典,数组,堆栈/队列和迭代功能.
但是在使用PHP一段时间之后,我发现很多array_*功能都比你初看起来慢得多.就像在array_rand一个非常大的阵列(10000+)的情况下.array_rand实际上是这么慢,在你使用php数组作为索引数组的情况下,像rand( 0, array_length( $array ) - 1 )运行MUCH 的函数要快array_rand.
现在我的问题.
如何在C级上实现PHP数组?这对于预测大量使用PHP数组数据类型的不同功能的函数的Big O非常有用.
我一直在谷歌搜索过去2个小时,我找不到PHP内置函数时间和空间复杂性的列表.我有isAnagramOfPalindrome问题要解决以下最大允许的复杂性:
expected worst-case time complexity is O(N)
expected worst-case space complexity is O(1) (not counting the storage required for input arguments).
Run Code Online (Sandbox Code Playgroud)
其中N是输入字符串长度.这是我最简单的解决方案,但我不知道它是否在复杂性限制范围内.
class Solution {
// Function to determine if the input string can make a palindrome by rearranging it
static public function isAnagramOfPalindrome($S) {
// here I am counting how many characters have odd number of occurrences
$odds = count(array_filter(count_chars($S, 1), function($var) {
return($var & 1);
}));
// If the string length is odd, then a palindrome …Run Code Online (Sandbox Code Playgroud) 获得一系列值并将其转换为键数组的最有效方法是什么?我真的想避免任何foreach循环......
$in = array(
'red',
'green',
'blue'
);
Run Code Online (Sandbox Code Playgroud)
INTO
$out = array(
'red' => NULL,
'green' => NULL,
'blue' => NULL
);
Run Code Online (Sandbox Code Playgroud) 这段时间我用PHP做了很多.寻找大海捞针.
$names = [
'Mike',
'John',
'Dave',
'Tony'
];
$gotDave = in_array('Dave', $names);
Run Code Online (Sandbox Code Playgroud)
in_array的运行时是O(n),其中n是元素的数量.
我经常将查找数据结构设置为这样.
$names = [
'Mike' => true,
'John' => true,
'Dave' => true,
'Tony' => true
];
$gotDave = isset($names['Dave']);
Run Code Online (Sandbox Code Playgroud)
运行时是O(1),因为在php中,关联数组是一个hashmap.
一些问题:
我有一个较大的字符串数组,我想用作查找.
我正在使用in_array(),但我怀疑它做了一个简单的循环 - 有没有人知道in_array()算法是否使用了bsearch算法?
我怀疑在数组中进行快速搜索的更好方法是什么(我在谈论具体情况).
假设我有一个数组L = [A,B,C](当我开始时).当程序运行时,可能L会增长(但到最后),我将进行搜索的一种可能情况是L = [A,B,C,D,E].
事实是,当我搜索时,我想要找到的值可能只有D和E.现在我正在使用find_array(elem,array),但是这个函数不能被"调整"到搜索开始结束并减少索引,我"害怕"对于所有搜索,函数in_array将检查所有具有较低索引的元素,然后才能找到我正在搜索的值.
¿有另一种搜索功能更适合我的问题吗?¿如何在in_array函数内部工作?
提前致谢
我正在参与常见的"MaxProfit"编程挑战.它基本上是这样的:
给定零索引数组A由N个整数组成,其中包含连续N天的每日股票价格,在此期间返回一笔交易的最大可能利润.
我对这个PHP算法非常满意,我避免了天真的暴力尝试:
public function maxProfit($prices)
{
$maxProfit = 0;
$key = 0;
$n = count($prices);
while ($key < $n - 1) {
$buyPrice = $prices[$key];
$maxFuturePrice = max( array_slice($prices, $key+1) );
$profit = $maxFuturePrice - $buyPrice;
if ($profit > $maxProfit) $maxProfit = $profit;
$key++;
}
return $maxProfit;
}
Run Code Online (Sandbox Code Playgroud)
然而,在测试了我的解决方案之后,它似乎表现得非常糟糕,甚至可能在O(n 2)时间内.
我做了一些关于这个主题的阅读,发现了一个非常相似的python解决方案.Python有一些非常方便的数组功能,允许使用a[s : e]语法分割数组,这与我使用该array_slice函数的PHP不同.我认为这一定是瓶颈所以我做了一些测试:
PHP array_slice()
$n = 10000;
$a = range(0,$n);
$start = microtime(1);
foreach ($a as $key => $elem) {
$subArray …Run Code Online (Sandbox Code Playgroud) 我正在寻找最有效的算法来检查数组中是否有一个唯一的元素.结果不一定是唯一元素,因此只是真或假.我知道我可以使用哈希表或某些东西达到O(n)效率,但我仍然在寻找一种在空间方面更有效的结果.
e-数组未排序,并包含整数.
我知道这是简单的PHP逻辑,但它不会起作用......
$str = "dan";
if(($str != "joe")
|| ($str != "danielle")
|| ($str != "heather")
|| ($str != "laurie")
|| ($str != "dan")){
echo "<a href='/about/".$str.".php'>Get to know ".get_the_author_meta('first_name')." →</a>";
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?