//分成快慢指针,从第二个元素开始与前一个元素比较,不相等保持不变,两//个指针都后移,相等fast指针后移在进行比较,
//最后返回slow指针是去除重复后的字符串长度
public static void main(String[] args) {
String[] st = new String[]{"1", "2", "2", "3", "3", "3", "4"};
int fast = 1;
int slow = 1;
int length = st.length;
while (fast < length) {
if (st[fast] != st[fast - 1]) {
st[slow] = st[fast];
slow++;
}
fast++;
}
System.out.println("数组长度:" + slow);
for (int i = 0; i < slow; i++) {
System.out.println(st[i]);
}
}
Java_LeetCode_删除有序数组中重复数据
于 2022-05-27 17:09:41 首次发布