给出2个独特,可订购,非连续元素的列表,请说:
['d', 'a', 'z', 'b']
Run Code Online (Sandbox Code Playgroud)
我想在另一个列表中找到他们的索引,说:
['a', 'b', 'z', 'd']
Run Code Online (Sandbox Code Playgroud)
结果将是一个包含其职位的列表:
[3, 0, 2, 1] -- element at 0 is at 3,
-- element at 1 is at 0, etc.
Run Code Online (Sandbox Code Playgroud) 我正在尝试解决一个简单的挑战,我编写一个返回数组中第一个重复数字的函数.
这是我试过的:
function duplicateNumber(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = arr.length; j >= 0; j--) {
if (arr[i] === arr[j]) {
var dup_num = arr[i]
}
}
}
return dup_num
}
Run Code Online (Sandbox Code Playgroud)
它似乎没有工作.我究竟做错了什么?
刚刚意识到我也是从结束循环,从而开始并开始结束.
在数组= [3, 5, 6, 8, 5, 3]
重复的数字应该是5因为它在3之前重复.
给定两个长度为 N 的整数数组 A 和 B。您必须找到两个求和的值:
\nZ=\xce\xa3 \xce\xa3 max(Ai+Bj, Bi+Aj)
\n这是我的暴力算法
\n请告诉我一个更有效的算法。
\n我正在寻找将python中列表的最后一个元素移动到适当位置的有效方法.例如,如果我们有list = [1,3,4,5,6,2],我们应该得到list = [1,2,3,4,5,6].我尝试的方法并不适用于理想的方式:
def sort1(lng, lst):
if len(lst) != lng:
return
else:
i = -2
last = lst[-1]
for x in lst:
if last < lst[i]:
lst[i] = last
i -= 1
print(lst)
sort1(6,[1,3,4,5,6,2])
It is giving me following result:
[1, 3, 4, 5, 2, 2]
[1, 3, 4, 2, 2, 2]
[1, 3, 2, 2, 2, 2]
[1, 2, 2, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud) 这是地球上最独特、最有趣的排序算法之一,其中完成排序所需的时间取决于每个元素的大小而不是元素的数量。但它是否有任何实际有效的用例呢?我的意思是,当处理大量数据时,使用睡眠排序将是一个可怕的想法。但是对于非常非常小的数字呢?它的性能优于归并排序吗?
如何实现将float或int转换为字符串的算法?我发现了一个链接 http://geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-0-2-years-about-algorithms-13
但我无法理解那里给出的算法
我正在编写代码来合并2个排序的数组代码如下
void main()
{
int a[]={7};
int b[] = {8};
int ret;
int *c;
merge(a,b,c,1,1);
}
void merge(int *a, int *b,int *sorted, int i, int j)
{
int c1,c2,k=0;
c1=0;
c2=0;
for(k=0;c1<i && c2< j;k++)
{
if(a[c1]<b[c2])
sorted[k]=a[c1++];
else
sorted[k]=b[c2++];
}
while(c1<i)
sorted[k++]=a[c1++];
while(c2<j)
sorted[k++]=b[j++];
}
Run Code Online (Sandbox Code Playgroud)
运行此代码程序时停止,我得到以下异常
Problem Event Name: APPCRASH
Application Name: merge.exe
Application Version: 0.0.0.0
Application Timestamp: 556fb91c
Fault Module Name: merge.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 556fb91c
Exception Code: c0000005
Exception Offset: 00001739
OS Version: …Run Code Online (Sandbox Code Playgroud) 似乎编译器没有进入for循环.数组的总和是计算的.SumAll([1,4])应该返回10(1 + 2 + 3 + 4)作为输出.
function sumAll(arr) {
//return Math.max.apply(Math,arr);
//return Math.min.apply(Math,arr);
// return "0";
var sum=arr.reduce(function(a,b){
for(var i=Math.min.apply(Math,arr);i<=Math.max.apply(Math,arr);i++){
return a+b;
}
},0);
//return sum;
}
sumAll([1, 4]);
Run Code Online (Sandbox Code Playgroud) 在这些数组中,数字可以是正数也可以是负数.每个阵列只能使用一个数字.
我在电话采访中收到了这个问题作为算法问题,这让我很难过.面试官似乎相信有一个O(n)解决方案.
编辑:我的问题不同于"可能的重复",因为这个问题涉及2个数组,而不是一个.
库中是否有标准算法可以完成以下 for 循环的工作?
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main( )
{
const char oldFillCharacter { '-' };
std::vector<char> vec( 10, oldFillCharacter ); // construct with 10 chars
// modify some of the elements
vec[1] = 'e';
vec[7] = 'x';
vec[9] = '{';
const char newFillCharacter { '#' };
for ( auto& elem : vec ) // change the fill character of the container
{
if ( elem == oldFillCharacter )
{
elem = newFillCharacter;
}
} …Run Code Online (Sandbox Code Playgroud) array-algorithms ×10
algorithm ×5
arrays ×4
sorting ×3
c++ ×2
javascript ×2
array-merge ×1
c ×1
c++20 ×1
for-loop ×1
haskell ×1
java ×1
list ×1
python ×1