avatar
d*i
1
First Q:
一直数组: {2, 3, 4, 5}
要求返回一个数组: {60, 40, 30, 24}, 其中每个element是其他元素的乘积。
Second Q:
用OOD的知识设计一个服装店,要求能够判断时候某个号码的衣服是否存在
avatar
d*x
2
first one is interesting
how did you deal with overflow?

【在 d******i 的大作中提到】
: First Q:
: 一直数组: {2, 3, 4, 5}
: 要求返回一个数组: {60, 40, 30, 24}, 其中每个element是其他元素的乘积。
: Second Q:
: 用OOD的知识设计一个服装店,要求能够判断时候某个号码的衣服是否存在

avatar
Q*e
3
估计面试官会告诉你
不用考虑overflow
^_^

【在 d**********x 的大作中提到】
: first one is interesting
: how did you deal with overflow?

avatar
d*x
4
i guess if it's not required, you can impress him by doing so...
it's simple :)

【在 Q*******e 的大作中提到】
: 估计面试官会告诉你
: 不用考虑overflow
: ^_^

avatar
Q*e
5
It depends.
However, I will be impressed :)
secure programming is more important.

【在 d**********x 的大作中提到】
: i guess if it's not required, you can impress him by doing so...
: it's simple :)

avatar
C*U
6
怎么设计比较好啊?

【在 d******i 的大作中提到】
: First Q:
: 一直数组: {2, 3, 4, 5}
: 要求返回一个数组: {60, 40, 30, 24}, 其中每个element是其他元素的乘积。
: Second Q:
: 用OOD的知识设计一个服装店,要求能够判断时候某个号码的衣服是否存在

avatar
e*e
7
Answer to Q1:
public int[] f(int[] a) {
if ( a == null || a.length == 0 )
throw new RuntimeException();

int[] r = new int[a.length];
for ( int i = 0; i < a.length; i++ ) {
r[i] = 1;
for ( int j = 0; j < a.length; j++ ) {
if ( j != i )
r[i] *= a[j];
}
}
return r;
}
avatar
d*x
8
10分,满分100

【在 e****e 的大作中提到】
: Answer to Q1:
: public int[] f(int[] a) {
: if ( a == null || a.length == 0 )
: throw new RuntimeException();
:
: int[] r = new int[a.length];
: for ( int i = 0; i < a.length; i++ ) {
: r[i] = 1;
: for ( int j = 0; j < a.length; j++ ) {
: if ( j != i )

avatar
e*e
9
Just my 2 cents.
public class Cloth {
int size;
int quantity;
String brand;
String name;
BigDecimal price;
}
public class ClothService {

// lookup cloth table in database to see if quality is 0;
public boolean query(Cloth c) {
//...
}

public void addCloth(Cloth c) {
//...
}

public void soldCloth(Cloth c) {
//...
}
}
public class Client {
public static void main(String[] args) {
Cloth c = new Cloth();
c.brand = "somebrand";
c.name = "somename";
c.size = 14;

ClothService s = new ClothService();
boolean exist = s.query( c );
}
}

【在 C***U 的大作中提到】
: 怎么设计比较好啊?
avatar
e*e
10
second version:
public int[] f(int[] a) {
if ( a == null || a.length == 0 )
throw new RuntimeException();

int s = 1;
for ( int i = 0; i < a.length; i++ ) {
s *= a[i];
}

int[] r = new int[a.length];
for ( int i = 0; i < a.length; i++ ) {
r[i] = s/a[i];
}

return r;
}

【在 d**********x 的大作中提到】
: 10分,满分100
avatar
d*x
11
60分。
there is a very severe problem in this piece of code. other than overflow.

【在 e****e 的大作中提到】
: second version:
: public int[] f(int[] a) {
: if ( a == null || a.length == 0 )
: throw new RuntimeException();
:
: int s = 1;
: for ( int i = 0; i < a.length; i++ ) {
: s *= a[i];
: }
:

avatar
p*2
12

5分吧

【在 e****e 的大作中提到】
: second version:
: public int[] f(int[] a) {
: if ( a == null || a.length == 0 )
: throw new RuntimeException();
:
: int s = 1;
: for ( int i = 0; i < a.length; i++ ) {
: s *= a[i];
: }
:

avatar
g*e
13
处理下a[i]=0的情况吧~

【在 d**********x 的大作中提到】
: 60分。
: there is a very severe problem in this piece of code. other than overflow.

avatar
e*e
14
public static int[] f(int[] a) {
if ( a == null || a.length == 0 )
throw new RuntimeException();

int s = 1;
int[] r = new int[a.length];
int zeroCount = 0;
int zeroPos = 0;
for ( int i = 0; i < a.length; i++ ) {
if ( a[i] == 0 ) {
zeroCount++;
if ( zeroCount == 2 )
return r;
else {
zeroPos = i;
continue;
}
}
s *= a[i];
}

if ( zeroCount == 1 ) {
r[zeroPos] = s;
return r;
} else {
for ( int i = 0; i < a.length; i++ ) {
r[i] = s/a[i];
}
}
return r;
}
avatar
d*x
15
好吧。
然后length==1的时候输出1是否合适可以定义一下,毕竟没有其他的元素了

【在 e****e 的大作中提到】
: public static int[] f(int[] a) {
: if ( a == null || a.length == 0 )
: throw new RuntimeException();
:
: int s = 1;
: int[] r = new int[a.length];
: int zeroCount = 0;
: int zeroPos = 0;
: for ( int i = 0; i < a.length; i++ ) {
: if ( a[i] == 0 ) {

avatar
d*i
16
If overflow is not to be considered.
Then what you should do is get the multiplication of all elements.
But the tricky part is that.
1. 0 exists
2. how many 0s exist.
How to deal with the above two situations is the key to solve this problem.
If overflow is an issue.
Then you could use DP, very simple DP.
Two times scan. one from left ->right, one from right -> left.
left ->right iteration record the multiplication of elements to the left of
current element. ex. MUL_Left[i] = A[i] * MUL_Left[ i - 1].
same idea from right -> left.
Then for each element in the array.
A[i] = MUL_Left[i] * MUL_Right[n - i];
I don't know if there is a solution can do it with one time scan.
Let me know if you get it.
avatar
d*x
17
i think there is a better way, in term of space.
think about this: if the multiplication without the largest one already
overflows, we will overflow anyway.
if it is not, we will have a chance. with this number, the largest number (
abs value), and a[i], you can easily calc target[i].
of course you should consider the number of 0's in the array.

of

【在 d******i 的大作中提到】
: If overflow is not to be considered.
: Then what you should do is get the multiplication of all elements.
: But the tricky part is that.
: 1. 0 exists
: 2. how many 0s exist.
: How to deal with the above two situations is the key to solve this problem.
: If overflow is an issue.
: Then you could use DP, very simple DP.
: Two times scan. one from left ->right, one from right -> left.
: left ->right iteration record the multiplication of elements to the left of

avatar
l*o
18
还多开了个数组...看到乱开数组就很敏感...
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。