s**********4 发帖数: 3 | 1 之前都用C++,对于java的一个简单问题卡住了,求教:
比如说我要实现一个关于数组的函数,有如下两行代码:
char str[] = {'a', 'b', 'c', 'd', 'e'};
removeDup(str);
这时运行会报错out of boundary。但是如果在str[]里面加上数字又不对。那么怎样才
能让程序运行呢?非常感谢:) |
u****s 发帖数: 2186 | 2 这个没错。
Something is not right in your removeDup()
【在 s**********4 的大作中提到】 : 之前都用C++,对于java的一个简单问题卡住了,求教: : 比如说我要实现一个关于数组的函数,有如下两行代码: : char str[] = {'a', 'b', 'c', 'd', 'e'}; : removeDup(str); : 这时运行会报错out of boundary。但是如果在str[]里面加上数字又不对。那么怎样才 : 能让程序运行呢?非常感谢:)
|
Y**G 发帖数: 1089 | 3 the exception stack will tell you the culprit
【在 u****s 的大作中提到】 : 这个没错。 : Something is not right in your removeDup()
|
r******r 发帖数: 700 | 4 谁能写个比较简洁的 inplace 算法。试了试,好像还挺费劲的 。。。
【在 s**********4 的大作中提到】 : 之前都用C++,对于java的一个简单问题卡住了,求教: : 比如说我要实现一个关于数组的函数,有如下两行代码: : char str[] = {'a', 'b', 'c', 'd', 'e'}; : removeDup(str); : 这时运行会报错out of boundary。但是如果在str[]里面加上数字又不对。那么怎样才 : 能让程序运行呢?非常感谢:)
|
N***m 发帖数: 4460 | 5 今天来试试
【在 r******r 的大作中提到】 : 谁能写个比较简洁的 inplace 算法。试了试,好像还挺费劲的 。。。
|
N***m 发帖数: 4460 | 6 来个效率比较低的,不知道有没有bug
public class Main {
public static void main(final String[] args) {
char[] source = { 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'c', 'd', 'd', 'a
' };
new Main().removeDup(source);
for (char c : source) {
if (c != 0) {
System.out.print(c);
} else {
break;
}
}
System.out.println();
}
void removeDup(final char[] source) {
for (int i = 1; i < source.length; i++) {
if (this.contains(source[i], source, i - 1)) {
source[i] = 0;
}
}
this.compact(source);
}
boolean contains(final char c, final char[] source, final int k) {
for (int i = 0; i <= k; i++) {
if (source[i] == c) {
return true;
}
}
return false;
}
void compact(final char[] source) {
for (int i = 1; i < source.length; i++) {
if (source[i] != 0) {
for (int j = 0; j < i; j++) {
if (source[j] == 0) {
source[j] = source[i];
source[i] = 0;
break;
}
}
}
}
}
} |
B*****g 发帖数: 34098 | 7 为啥要用0?
'a
【在 N***m 的大作中提到】 : 来个效率比较低的,不知道有没有bug : public class Main { : public static void main(final String[] args) { : char[] source = { 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'c', 'd', 'd', 'a : ' }; : new Main().removeDup(source); : for (char c : source) { : if (c != 0) { : System.out.print(c); : } else {
|
N***m 发帖数: 4460 | 8 0='\0'in this case which is just an empty char to be compacted later.
【在 B*****g 的大作中提到】 : 为啥要用0? : : 'a
|
p*****2 发帖数: 21240 | |
z*******3 发帖数: 13709 | 10 java里面用了正确的类也是一句话的事
Set |
|
|
N***m 发帖数: 4460 | 11 关键是in place operation. otherwise,
use new LinkedHashSet(Arrays.asList(source)),
but source has to be Character[].
【在 z*******3 的大作中提到】 : java里面用了正确的类也是一句话的事 : Set
|
z*******3 发帖数: 13709 | 12 真抠,感觉这种事一般只会在面试时候故意问起
现实中真有人这样写吗?
【在 N***m 的大作中提到】 : 关键是in place operation. otherwise, : use new LinkedHashSet(Arrays.asList(source)), : but source has to be Character[].
|
F****n 发帖数: 3271 | 13 // non-duplicate elements from 0 to the returned index
// O(n^2)
public int removeDup(char[] array) {
int index = array.length - 1;
for (int i = 0; i <= index; i++ ){
for (int j = i; j <= index; ) {
if (array[i] == array[j]) {
array[j] = array[index--];
}
else {
j++;
}
}
}
return index;
}
// O(n) time O(n) space
public int removeDup(char[] array) {
Set set = new HashSet();
int index = array.length - 1;
for (int i = 0; i <= index; ) {
if (set.add(array[i]) {
i++;
}
else {
array[i] = array[index--];
}
}
return index;
}
【在 N***m 的大作中提到】 : 关键是in place operation. otherwise, : use new LinkedHashSet(Arrays.asList(source)), : but source has to be Character[].
|
d****g 发帖数: 7460 | 14 不是一般的聪明。不是一般的饶。pf.
【在 F****n 的大作中提到】 : // non-duplicate elements from 0 to the returned index : // O(n^2) : public int removeDup(char[] array) { : int index = array.length - 1; : for (int i = 0; i <= index; i++ ){ : for (int j = i; j <= index; ) { : if (array[i] == array[j]) { : array[j] = array[index--]; : } : else {
|
d****g 发帖数: 7460 | 15 是不是可以用各种sort 就可以了? O(nlogn)
【在 F****n 的大作中提到】 : // non-duplicate elements from 0 to the returned index : // O(n^2) : public int removeDup(char[] array) { : int index = array.length - 1; : for (int i = 0; i <= index; i++ ){ : for (int j = i; j <= index; ) { : if (array[i] == array[j]) { : array[j] = array[index--]; : } : else {
|
o***i 发帖数: 603 | 16 for (int j = i+1; j <= index; )
【在 F****n 的大作中提到】 : // non-duplicate elements from 0 to the returned index : // O(n^2) : public int removeDup(char[] array) { : int index = array.length - 1; : for (int i = 0; i <= index; i++ ){ : for (int j = i; j <= index; ) { : if (array[i] == array[j]) { : array[j] = array[index--]; : } : else {
|
F****n 发帖数: 3271 | 17 丢人了
【在 o***i 的大作中提到】 : for (int j = i+1; j <= index; )
|