最近问到了这个问题:
编写一个包含数组(每个数组包含从最大到最小排序的数字)和一个数字(n)的函数。返回n个最大数字。
例如:
findLargest([ 10,5,3,1,9,8,7,6,11,2,1,0 ],5) => 11,10,9,8,7
findLargest([ 15,5,3,1,10,8,7,6],3) => 15,10,8
这样做无需复制或修改数组(只需从数组中读取)。优化时间复杂度。
我想出了这个问题,但对我的解决方案并不满意:
function findLargest(numberArrays, n ) {
var results = [];
var p
下面是每个的Hoare分区算法。
维基百科的伪代码:
algorithm partition(A, lo, hi) is
// Pivot value
pivot := A[ floor((hi + lo) / 2) ] // The value in the middle of the array
// Left index
i := lo - 1
// Right index
j := hi + 1
loop forever
// Move the left index to the right at least once and while
我正在尝试理解这个方法到底做了什么,它说它的假设是“不断交换最外层的错误位置对”。我把这个放到一个程序中,并尝试了不同的数组,但结果对我来说没有任何意义,这到底是做什么的
partition(A, p)
A: array of size n, p: integer s.t. 0 <= p < n
1. swap(A[0],A[p])
2. i <- 1, j <- n − 1
3. while i < j do
4. while A[i] <= A[0] and i < n do
5. i <- i + 1
我想在Java中递归地找到两个数字的乘法,只使用加法、减法和比较。所以,我用谷歌搜索了一下,找到了符合问题要求的Egyptian Algorithm。
但是,我不确定到达base case后如何找到乘法结果。
示例:
13 x 30
1 -- 30
2 -- 60
4 -- 120
8 -- 240 //we stop here because the double of 8 is larger than 13
为了得到结果,我们将left column中等于13的数字相加,它们是1+4+8,另一方面,我们将right column中相反的数字相加,它们是30+120+240
有人能解释一下从a中减去一个整数b是如何返回有序数组的吗?例如,我觉得自己很愚蠢,因为我不明白为什么这句简单的话能解决这个问题。
var arr = [7, 2, 3, 100, 5, 6];
var sorted = arr.sort(function(a, b){
return a - b; // What makes this line sort the integers??
});
console.log(sorted); // [2, 3, 5, 6, 7, 100];
我正在尝试理解函数指针,但遇到了困难。我看过K&R中的排序示例和其他一些类似的示例。我的主要问题是计算机实际在做什么。我创建了一个非常简单的程序来尝试查看基础知识。请参阅以下内容:
#include <stdio.h>
int func0(int*,int*);
int func1(int*,int*);
int main(){
int i = 1;
myfunc(34,23,(int(*)(void*,void*))(i==1?func0:func1));//34 and 23 are arbitrary inputs
}
void myfu
我在python 中找到了以下代码
def bisect_left(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo
返回在列表a
我一直在试图弄清楚CompareTo()方法是如何在内部工作的,但我失败了。我已经搜索了这个网站并阅读了一些帖子,我想我已经在MSDN上看到了关于这个主题的所有内容,我只是不明白。MSDN示例:
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
{
return this.
我正在学习基本的数组排序,我只是有点挣扎才能完全理解它的逻辑。我理解递归,意思是将一个数组分割成一个支点两侧的两个数组,然后继续对每个子数组进行分区,直到到达一个只包含一个元素的数组为止。我并不总是完全理解What循环本身的实现。
在这里,我遇到了一个选择中间元素作为枢轴的实现。我知道枢轴可以是任何元素,不管它是第一个,最后,任何随机元素,或者是为最大效率而专门选择的元素。由于某种原因,以中间元素为支点,我发现理解起来更直观。
while (i <= j) {
// If the current value from the left list is smaller
所以,我必须对整数数组进行排序,这样每一个比某个scanf'd整数小的数都在左边,这个集合变量在中间,每个更大的数字在右边。我得到了左和右的部分,但是我不知道如何使它处于中间。有什么想法吗?
#include <stdio.h>
int main()
{
int y, i, k, temp;
printf("give integer\n");
scanf("%d", &y);
int x[10] = {5,8,9,4,2,3,2,4,5,6};
i=0;
k=1;
while(i
我决定试着做一个关于分析算法可能最糟糕的运行时的问题,并获得一些实践。由于我是一个初学者,我只需要帮助我以正确的方式表达我的答案。我在一本书中提出了这个问题,它使用了以下算法:
输入:一组n个点(x1,y1),。。。,(xn,yn) with n≥2。输出:最近的一对点的平方距离。ClosePoints
1. if n = 2 then return (x1 − x2)^2 + (y1 − y2)^2
2. else
3. d ← 0
4. for i ← 1 to n − 1 do
5. for j ← i + 1 to n do
6. t ← (xi − xj)^2 + (yi
我有一个快速排序代码,它应该在文本"B A T T A J U S“(忽略空格)上运行。但是我似乎不是很好的理解代码。
void quicksort (itemType a[], int l, int r)
{
int i, j; itemType v;
if (r>l)
{
v = a[r]; i = l-1; j = r;
for (;;)
{
while (a[++i] < v);
while (a[--j] >= v);
i
我试图理解一种算法,给定数组A和整数x,返回需要添加到A中的元素的最小数量,这样x才能成为中间值。
该算法如下所示:
假设中间值总是位于(n-1)/2位置的元素。
n := |A|
m := 0
for i := 1 to n:
if (A[i] < x) then:
m++
if (m < n - m) then:
return (n - 2 * m)
else:
return (2 * m – n + 1)
我知道,如果m等于n-m,那么数组大小是偶数,所以我们可以在(n-1)/2位置添加想要的中值,这将是我们的新的中值。
当m低于n
对不起,如果这个问题觉得多余,但是我还没有在提供的链接上得到t been able to find the answer with this method. It doesn的答案。我一直试图制作一个Javascript程序,检查用户的输入是一周、一天还是周末。它必须用for或do while循环来完成。
我希望我的程序检查用户输入是否在数组中,如果是,程序应该能够判断是工作日还是周末。
我的问题是:它总是回到工作日,即使我把星期六或星期天。
这是标记为Javascript,但适用于任何语言,因为它是基本的东西。
到目前为止这是我的密码
var input = prompt("Ente