[英]Pramp Reverse sentence interview question in C#
這是從pramp網站獲取的,我正在嘗試遵循他們的psudo代碼,這是對以下問題的解答。
**
您將獲得一個字符數組arr,該數組由以空格字符分隔的字符序列組成。 每個以空格分隔的字符序列定義一個單詞。 實現功能reverseWords,以最有效的方式反轉數組中單詞的順序。
**
例:
input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]
output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'e', 'r', 'f', 'e', 'c', 't' ]
這是我的代碼。 有用。
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace StringQuestions
{
[TestClass]
public class ReverseSentanceTest
{
[TestMethod]
public void ManyWordsTest()
{
char[] inputArray = {
'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'r', 'a', 'c', 't', 'i', 'c', 'e'
};
char[] expectedOutputArr = {'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'e', 'r', 'f', 'e', 'c', 't'};
char[] outputArr = ReserverseSentence(inputArray);
CollectionAssert.AreEqual(expectedOutputArr, outputArr);
}
[TestMethod]
public void OneWordTest()
{
char[] inputArray = {
'p', 'e', 'r', 'f', 'e', 'c', 't',
};
char[] expectedOutputArr = {
'p', 'e', 'r', 'f', 'e', 'c', 't'};
char[] outputArr = ReserverseSentence(inputArray);
CollectionAssert.AreEqual(expectedOutputArr, outputArr);
}
public char[] ReserverseSentence(char[] inputArr)
{
if (inputArr == null || inputArr.Length == 0)
{
throw new ArgumentException("array is empty");
}
MirrorArray(inputArr, 0, inputArr.Length-1);
int indexStart = 0;
for (int i = 0; i < inputArr.Length; i++)
{
//end of a word in the middle of the sentence
if (inputArr[i] == ' ')
{
MirrorArray(inputArr, indexStart, i - 1);
indexStart = i+1; //skip the white space and start from the letter after
}
else if (i == inputArr.Length - 1)
{
MirrorArray(inputArr, indexStart, i);
}
}
return inputArr;
}
private void MirrorArray(char[] inputArr, int start, int end)
{
while (start < end)
{
var temp = inputArr[start];
inputArr[start] = inputArr[end];
inputArr[end] = temp;
start++;
end--;
}
}
}
}
但是我想我錯過了一個極端案例。 他們的偽代碼有3個if / else分支。 我只是將我的單詞以整數開頭,在其中它們使用nullable<int>
類的東西。
function reverseWords(arr):
# reverse all characters:
n = arr.length
mirrorReverse(arr, 0, n-1)
# reverse each word:
wordStart = null
for i from 0 to n-1:
if (arr[i] == ' '):
if (wordStart != null):
mirrorReverse(arr, wordStart, i-1)
wordStart = null
else if (i == n-1):
if (wordStart != null):
mirrorReverse(arr, wordStart, i)
else:
if (wordStart == null):
wordStart = i
return arr
# helper function - reverses the order of items in arr
# please note that this is language dependent:
# if are arrays sent by value, reversing should be done in place
function mirrorReverse(arr, start, end):
tmp = null
while (start < end):
tmp = arr[start]
arr[start] = arr[end]
arr[end] = tmp
start++
end--
您能不能解釋一下我是否缺少一些特殊情況? 並舉一個例子。 謝謝 !
您在以下代碼indexStart
設置為空格后的一個字母:
if (inputArr[i] == ' ')
{
MirrorArray(inputArr, indexStart, i - 1);
indexStart = i+1; //skip the white space and start from the letter after
}
而不是將indexStart
設置為未初始化的變量,而是檢查該變量是否未初始化,然后使用下一個新單詞數組中的位置對其進行初始化,如下所示:
wordStart = null
for i from 0 to n-1:
if (arr[i] == ' '):
if (wordStart != null):
mirrorReverse(arr, wordStart, i-1)
wordStart = null
您可以做的是將indexStart
設置為-1
,然后在循環中檢查indexStart
的值indexStart
為-1
並以此表示您已開始一個新單詞並可以將索引記錄在數組( arr
)中。 您可以這樣做:
int indexStart = -1;
for (int i = 0; i < inputArr.Length; i++)
{
//end of a word in the middle of the sentence
if (inputArr[i] == ' ')
{
MirrorArray(inputArr, indexStart, i - 1);
indexStart = -1; //ready to record next index of new word
}
else if (i == inputArr.Length - 1)
{
MirrorArray(inputArr, indexStart, i);
}
else
{
if(indexStart < 0)
indexStart = i; //index of new word
}
}
關鍵的一點是設置indexStart
到你會不會自然循環內部設置一個值,像-1
,或null
,如果你想使用可空類型一樣int?
但是然后您必須先檢查一個值,然后再嘗試訪問它以避免NullReferenceExceptions
,使用-1
幾乎更好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.