SORU
21 NİSAN 2010, ÇARŞAMBA


Sayı dizisi verilmiş, diğer tüm sayılar ürünler (bölüm)dizisi döndürür

İş görüşmesinde bu soru soruldu, ve diğerleri çözmek için nasıl bilmek istiyorum. Java ile en rahat değilim, ama diğer dillerde çözümler bekliyoruz.

Sayı dizisi verildiğinde, nums, sayı dizisi products[i] nums[j], j != i tüm ürün nerede products, dönüş.

Input : [1, 2, 3, 4, 5]
Output: [(2
int a[N] // This is the input
int products[N];

// Get the products below the current index
p=1;
for(int i=0;i<N;  i) {
  products[i]=p;
  p*=a[i];
}

// Get the products above the curent index
p=1;
for(int i=N-1;i>=0;--i) {
  products[i]*=p;
  p*=a[i];
}
4*5), (1
int a[N] // This is the input
int products[N];

// Get the products below the current index
p=1;
for(int i=0;i<N;  i) {
  products[i]=p;
  p*=a[i];
}

// Get the products above the curent index
p=1;
for(int i=N-1;i>=0;--i) {
  products[i]*=p;
  p*=a[i];
}
4*5), (1
int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N;  i) {
  products_below[i]=p;
  p*=a[i];
}

int products_above[N];
p=1;
for(int i=N-1;i>=0;--i) {
  products_above[i]=p;
  p*=a[i];
}

int products[N]; // This is the result
for(int i=0;i<N;  i) {
  products[i]=products_below[i]*products_above[i];
}
4*5), (1
int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N;  i) {
  products_below[i]=p;
  p*=a[i];
}

int products_above[N];
p=1;
for(int i=N-1;i>=0;--i) {
  products_above[i]=p;
  p*=a[i];
}

int products[N]; // This is the result
for(int i=0;i<N;  i) {
  products[i]=products_below[i]*products_above[i];
}
3*5), (1
int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N;  i) {
  products_below[i]=p;
  p*=a[i];
}

int products_above[N];
p=1;
for(int i=N-1;i>=0;--i) {
  products_above[i]=p;
  p*=a[i];
}

int products[N]; // This is the result
for(int i=0;i<N;  i) {
  products[i]=products_below[i]*products_above[i];
}
3*4)] = [120, 60, 40, 30, 24]

Bölümü kullanmadan O(N) bunu yapmak gerekir.

CEVAP
21 NİSAN 2010, ÇARŞAMBA


polygenelubricants yöntem açıklaması: Hile diziler (4 element için) oluşturmaktır

{              1,         a[0],    a[0]*a[1],    a[0]*a[1]*a[2],  }
{ a[1]*a[2]*a[3],    a[2]*a[3],         a[3],                 1,  }

Sol ve sağ kenarlarına sırasıyla başlatarak O(n) yapılabilir.

Sonra iki dizi öğe öğesi çarparak istenen sonucu verir

Benim kod şöyle görünecektir

int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N;  i) {
  products_below[i]=p;
  p*=a[i];
}

int products_above[N];
p=1;
for(int i=N-1;i>=0;--i) {
  products_above[i]=p;
  p*=a[i];
}

int products[N]; // This is the result
for(int i=0;i<N;  i) {
  products[i]=products_below[i]*products_above[i];
}

Eğer O(1) uzayda olmak gerekirse bu kadar net IMHO) yapabilirsiniz

int a[N] // This is the input
int products[N];

// Get the products below the current index
p=1;
for(int i=0;i<N;  i) {
  products[i]=p;
  p*=a[i];
}

// Get the products above the curent index
p=1;
for(int i=N-1;i>=0;--i) {
  products[i]*=p;
  p*=a[i];
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ehowhome

    ehowhome

    22 NİSAN 2009
  • kamaniusilelis

    kamaniusilel

    10 HAZİRAN 2011
  • Warner Bros. UK

    Warner Bros.

    6 HAZİRAN 2008