Chapter 1 Arrays and Strings - 1.3

The statement of problem 1.3 is:

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.
FOLLOW UP
Write the test cases for this method.


To be honest, the problem sucks. It doesn't make it clear which one should be removed in a bunch of duplicated characters. Let's assume that we should keep the first one of several duplicated characters. (First one means the first one we meet when iterating from left to right.)

Brute force solution takes O(n^2) running time.

Recall problem 1.1 and you will find this problem is a similar one. We can simply create a hash table for all characters and it will run in O(n). However, the smart method used in problem 1.1 has a space complexity of O(k), as k is the number of characters in the character set.

Another solution flashed into my mind is sorting the string in O(nlgn) and then using one extra variable to eliminate duplicated charaters. Nevertheless, it will change the order of characters and I am not sure whether it is proper.

Test cases of the program is given below:

1) empty string

2) "a"

3) "aa"

4) "aba"

5) "bab"

6) "abcd"


Seems that I cannot find a ideal solution whose running time is less than O(n^2). OK, let's turn to the answer page...

Well, the standard answer is the O(n^2) one and the author suggests that we'd better to ask what the interviewer means by an addtional buffer? Can we use addtional array of constant size?

I implemented the brute force solution as below:

def removeDuplicates(str):
    if len(str) < 2:
        return
    i = 0
    while i < len(str) - 1:
        j = i + 1
        while j < len(str):
            if str[i] == str[j]:
                del str[j]
                j = j - 1   # --j is wrong
            j = j + 1       # ++j is wrong
        i = i + 1           # ++i is wrong


if __name__ == '__main__':
    str = 'aaaaaabaaaaa'
    # One cannot change a string,
    # so we convert it to a list for convenience
    str_list = [i for i in str] 
    removeDuplicates(str_list)
    print str_list

I went into a endless loop when firstly tried to implement the algorithm above. The wrong code is in the comments: there is no ++ operator and -- operator in Python. ++ and -- will be translated to double positive operators and double negative operators.

Thanks to the delete operation in Python and it enable me to delete the duplicated elements directly. However, in some languages, we have no such a powerful tool in hands. For languages without delete operation, the answer page gives a smart implementation:

public static void removeDuplicates(char[] str) {
	if (str == null) return;
	int len = str.length;
	if (len < 2) return;

	int tail = 1;

	for (int i = 1; i < len; ++i) {
		int j;
		for (j = 0; j < tail; ++j) {
			if (str[i] == str[j]) break;
		}
		if (j == tail) {
			str[tail] = str[i];
			++tail;
		}
	}
	str[tail] = 0;
}

In my view, the algorithm above is similar to quick sort to some extent, for both of them keep the end of a particular segment in the whole array. This kind of strategy utilizes the memory that won't be used further.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 类型错误:只有大小为1的数组 这个错误通常出现在使用numpy数组时,当你尝试使用一个大小不为1的数组作为参数传递给只接受大小为1的函数时,就会出现这个错误。解决方法是检查你的代码,确保你传递给函数的数组大小正确。 ### 回答2: Type Error: Only Size-1 Arrays 是一种常见的错误类型,通常会在使用 NumPy 数组时发生。该错误通常是由于尝试将未被分配的一个数组作为另一个数组的索引而引起的。 更具体地说,Type Error: Only Size-1 Arrays 往往会在以下情况下发生: 1. 在 NumPy 中,尝试使用一个未被分配的数组作为索引 2. 尝试使用错误的数据类型对数组进行操作 3. 在使用基于数组的函数或方法时出现类型不匹配的错误 解决这个错误的方法取决于引发它的原因。以下是一些常见的解决方法: 1. 确保在使用数组时分配了正确的维度,以便数组中的每个元素都具有正确的形状。可以使用 np.shape() 函数来检查数组的维度和形状。 2. 考虑使用不同的数据类型或转换数据类型以确保正确地执行操作。 3. 阅读 NumPy 文档并查找有关数组操作的详细信息,以确定如何正确地使用函数或方法。 最后,值得一提的是,使用代码编辑器和调试工具可以大大减少 Type Error: Only Size-1 Arrays 等错误的发生和发现。因此,始终在编写代码时保持准确性和细心是非常重要的。 ### 回答3: Type error: only size-1 arrays 是一个常见的 Python 错误。该错误通常会在 NumPy 库的使用过程中出现,而导致这个错误的原因可能是多方面的。 首先,这个错误通常是由于代码中未正确调用 NumPy 库中的函数所致。例如,在使用 NumPy 库中的函数时,如果传递给函数的参数不是一个有效的数组或矩阵,则会出现这个错误。 另一个导致这个错误的原因可能是使用了不兼容的数据类型。例如,在 NumPy 数组中只能使用同一种数据类型的元素,如果使用了不兼容的数据类型,则会出现这个错误。 最后,当尝试将不同大小的数组拼接在一起时,也可能会出现 Type error: only size-1 arrays 错误。 为了解决这个问题,用户可以采取一些措施。首先,必须确定每个 NumPy 函数的正确参数。其次,需要确保传递给函数的所有输入参数都是有效的数组,并且数据类型兼容。最后,如果尝试将两个不同大小的数组拼接起来,则必须确保在拼接之前将它们转换为相同的大小。 总之,Type error: only size-1 arrays 错误是由于 NumPy 库函数调用的问题或者输入不兼容数据类型所致。无论是哪种情况,都必须经过仔细的检查和修改才能够成功解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值