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; }
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 )
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 的大作中提到】 : 怎么设计比较好啊?
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
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]; : } :
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]; : } :
g*e
13 楼
处理下a[i]=0的情况吧~
【在 d**********x 的大作中提到】 : 60分。 : there is a very severe problem in this piece of code. other than overflow.
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; }
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 ) {
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.
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