简体   繁体   English

如何用两个字符替换数组中的特定字符

[英]How to replace a specific character in an array with two characters

So I just came back from a job interview and one of the questions I had to face with was:所以我刚从工作面试回来,我不得不面对的一个问题是:

"Given an array of characters and three characters for example: “给定一个字符数组和三个字符,例如:

Array: [a,b,c,z,s,w,y,z,o]数组:[a,b,c,z,s,w,y,z,o]

Char 1: 'z'字符 1:'z'

Char 2: 'R' Char 3: 'R'字符 2:'R' 字符 3:'R'

Your goal is to replace each 'z' in the array to become two R characters within O(N) time complexity.您的目标是将数组中的每个“z”替换为 O(N) 时间复杂度内的两个 R 字符。

so your input will be Array: [a,b,c,z,s,w,y,z,o]所以你的输入将是 Array: [a,b,c,z,s,w,y,z,o]

and your output array will be: [a,b,c,R,R,s,w,y,R,R,o] and your output array will be: [a,b,c,R,R,s,w,y,R,R,o]

assume that there is no 'R' in the array before.假设之前数组中没有“R”。

You are not allowed to use other arrays or other variables.不允许使用其他 arrays 或其他变量。

The algorithm should be in-line algorithm.该算法应该是在线算法。

Your final array must be a characters array."您的最终数组必须是字符数组。”

My solution was within O(N^2) time complexity but there is a solution within O(N) time complexity.我的解决方案在 O(N^2) 时间复杂度内,但在 O(N) 时间复杂度内有一个解决方案。

The interview is over but I am still thinking about this problem, Can anyone help me to solve this?面试结束了,但是我还在想这个问题,谁能帮我解决这个问题?

First scan the input to count how many occurrences of char 1 exist.首先扫描输入以计算 char 1 的出现次数。 This has a linear time complexity.这具有线性时间复杂度。

From that you know that the length of the final array will be the input length + the number of occurrences.由此您知道最终数组的长度将是输入长度 + 出现次数。

Then extend the array to its new length, leaving the new slots empty (or whatever value).然后将数组扩展到其新长度,将新插槽留空(或任何值)。 The exact nature of the operation depends on how the array data structure is implemented.操作的确切性质取决于数组数据结构的实现方式。 This can surely be done with at worst a linear time complexity.这肯定可以在最坏的线性时间复杂度下完成。

Use two indexes, i and j , where i references the last character of the input array and j references the very last index in the array (potentially to an empty slot).使用两个索引ij ,其中i引用输入数组的最后一个字符, j引用数组中的最后一个索引(可能指向一个空槽)。

Start copying from i to j each time decreasing the values of these indices with one.每次将这些索引的值减一时,开始从i复制到j If you copy the matching letter, then duplicate the copied character to j again, and only reduce j .如果您复制匹配的字母,则将复制的字符再次复制到j ,并且只减少j This has again a linear time complexity.这再次具有线性时间复杂度。

The algorithm will end with both i and j equal to -1.该算法将以ij都等于 -1 结束。

Do two iterations.做两次迭代。

First, count the number of char1 s ('z' in your example).首先,计算char1的数量(在您的示例中为“z”)。
Now you know how long your array should be at the end: array.size() + num_char1s现在你知道你的数组应该多长了: array.size() + num_char1s

Then, go from last to first with input and output iterators.然后,go 从最后到第一个输入和 output 迭代器。 If the element is char1 , insert to the end iterator the new chars, otherwise - just copy.如果元素是char1 ,则将新字符插入到迭代器的末尾,否则 - 只需复制。

Pseudo code:伪代码:

num_char1s = 0
for x in array:
  if x == char1:
    num_char1s++

// Assuming array has sufficient memory already allocated.
out_iterator = num_char1s + size - 1
in_iterator = size - 1

while (in_iterator >= 0):
  if (array[in_iterator] == char1):
    array[out_iterator--] = char3
    array[out_iterator--] = char2
  else:
    array[out_iterator--] = array[in_iterator]
  in_iterator--

In your question, two things are very important.在你的问题中,有两件事非常重要。

  1. can't use new variable不能使用新变量
  2. can't use new array不能使用新数组

So, we must need to use given array.所以,我们必须需要使用给定的数组。

  1. First we will increase our given array size double.首先,我们将给定的数组大小增加一倍。 why?为什么? Cause at most our new array size = given_array_size*2 (if all characters = char 1)最多导致我们的新数组大小 = given_array_size*2 (如果所有字符 = char 1)
  2. Now we will shift our given array n times right , where n= given_array_size.现在我们将给定数组向右移动 n 次,其中 n= given_array_size。
  3. Now we will iterate our array from the new shifted position = n.现在我们将从新移位的 position = n 迭代我们的数组。 iterate i=n to 2*n-1迭代 i=n 到 2*n-1
  4. We will take j=0, which will write new array.我们将取 j=0,这将写入新数组。 if we found char 1 , we will make array[j++]=char 2 and array[j++]=char 3.如果我们找到char 1 ,我们将使 array[j++]=char 2 和 array[j++]=char 3。
  5. But if a character is not 'z', we simply don't do anything.但是如果一个字符不是'z',我们就什么也不做。 array[j++]=array[i]数组[j++]=数组[i]
  6. At last 0 to j-1 is the right answer.最后0 到 j-1是正确的答案。

Complexity: O(n)复杂度:O(n)
No new variable and array needed不需要新的变量和数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM