由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问个数组问题
相关主题
A家面筋:最多用一个循环,怎么去重复?Amazon 电面[第2轮]
被G电面给毙了
相关话题的讨论汇总
话题: source话题: int话题: char话题: index话题: removedup
进入Java版参与讨论
1 (共1页)
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
9
这个在scala里就是一句话的事。
z*******3
发帖数: 13709
10
java里面用了正确的类也是一句话的事
Set
相关主题
被G电面给毙了A家面筋:最多用一个循环,怎么去重复?
Amazon 电面[第2轮]
进入Java版参与讨论
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; )
1 (共1页)
进入Java版参与讨论
相关主题
A家面筋:最多用一个循环,怎么去重复?Amazon 电面[第2轮]
被G电面给毙了
相关话题的讨论汇总
话题: source话题: int话题: char话题: index话题: removedup