JavaScriptの文字列の反転
10103 ワード
私は最近データ構造とアルゴリズムを勉強し始めました.文字列を反転するJavaScriptのインタビューでは非常に一般的な課題です.インタビュアーは、あなたに異なる挑戦でこの挑戦を解決するよう頼みます.
JavaScriptの文字列を逆にする方法は次の三つです.
1 . JavaScript組み込みメソッドの使用
このメソッドでは、JavaScript : Stringで3つの組み込みメソッドを使用します.プロトタイプ.split ()は配列です.プロトタイプ.reverse ()およびarray ().プロトタイプ.join ().メソッドは、文字列オブジェクトを新しい文字列の配列に分割します. メソッドは、配列オブジェクトを変異して、それを逆にします.配列の最後の要素が最初になり、最初の要素が最後になります. メソッドは、配列オブジェクトを受け取り、要素を一緒に新しい文字列に結合します.
2 .ループを反転( decrecmenting )する
3 .使用の再帰
このメソッドでは、文字列を使用します.プロトタイプ.substr ()メソッドメソッドは、指定されたインデックスから始まる文字列の一部を返し、その後指定した文字数に対して拡張します.
JavaScriptの文字列を逆にする方法は次の三つです.
1 . JavaScript組み込みメソッドの使用
このメソッドでは、JavaScript : Stringで3つの組み込みメソッドを使用します.プロトタイプ.split ()は配列です.プロトタイプ.reverse ()およびarray ().プロトタイプ.join ().
function reverseString(str) {
// Step 1. Use the split() method to return a new array
let splitString = str.split(""); // "hello".split("");
// ["h", "e", "l", "l", "o"]
// Step 2. Use the reverse() method to reverse the newly created array
let reverseArray = splitString.reverse(); // ["h", "e", "l", "l", "o"].reverse();
// ["o", "l", "l", "e", "h"]
// Step 3. Use the join() method to join all elements of the array into a string
let joinArray = reverseArray.join(""); // ["o", "l", "l", "e", "h"].join("");
// "olleh"
//Step 4. Return the reversed string
return joinArray; // "olleh"
}
reverseString("hello");
上記の.split()
関数は、すべての3つのメソッドをチェインすることによって書き換えることができます.function reverseString(str) = {
return str.split("").reverse().join("") // "olleh"
}
reverseString("hello");
2 .ループを反転( decrecmenting )する
function reverseString(str) {
// Step 1. Create an empty string that will house the new reversed string
let newString = "";
// Step 2. Create the FOR loop
/* The starting point of the loop will be (str.length - 1) which corresponds to the
last character of the string, "o"
As long as i is greater than or equals 0, the loop will continue
We decrement i after each iteration
Once i is less than 0, the loop stops */
for (let i = str.length - 1; i >= 0; i--) {
newString += str[i]; // add the character into the new string
}
/* Here hello's length equals 5
For each iteration: i = str.length - 1 and newString = newString + str[i]
First iteration: i = 5 - 1 = 4, newString = "" + "o" = "o"
Second iteration: i = 4 - 1 = 3, newString = "o" + "l" = "ol"
Third iteration: i = 3 - 1 = 2, newString = "ol" + "l" = "oll"
Fourth iteration: i = 2 - 1 = 1, newString = "oll" + "e" = "olle"
Fifth iteration: i = 1 - 1 = 0, newString = "olle" + "h" = "olleh"
End of the FOR loop*/
// Step 3. Return the reversed string
return newString; // "olleh"
}
reverseString("hello");
3 .使用の再帰
このメソッドでは、文字列を使用します.プロトタイプ.substr ()メソッド
"hello".substr(1); // "ello"
再帰関数は単にそれ自体を呼び出す関数です.これはベースケースと再帰的な呼び出しで構成され、ベースケースは関数を停止し、再帰的な呼び出しは関数が呼び出すときです.基底ケースがない場合、関数は無限ループ内で継続します.function reverseString(str) {
if (str === "") { // This is the base case that will end the recursion
return "";
} else {
return reverseString(str.substr(1)) + str[0]; // This is the recursive call
}
}
/*
First Part of the recursive function:
You will have several nested calls to reverseString(), each time passing in a substring of the previous string
Each call: str === "?" will return reverseString(str.substr(1)) + str[0]
1st call: reverseString("hello") will return reverseString("ello") + "h"
2nd call: reverseString("ello") will return reverseString("llo") + "e"
3rd call: reverseString("llo") will return reverseString("lo") + "l"
4th call: reverseString("lo") will return reverseString("o") + "l"
5th call: reverseString("o") will return reverseString("") + "o"
Second part of the recursive function:
The function hits the base case and the most highly nested call returns immediately (the 5th call returns first)
5th call will return reverseString("") + "o" = "o"
4th call will return reverseString("o") + "l" = "o" + "l"
3rd call will return reverseString("lo") + "l" = "o" + "l" + "l"
2nd call will return reverserString("llo") + "e" = "o" + "l" + "l" + "e"
1st call will return reverserString("ello") + "h" = "o" + "l" + "l" + "e" + "h"
*/
}
reverseString("hello");
Reference
この問題について(JavaScriptの文字列の反転), 我々は、より多くの情報をここで見つけました https://dev.to/jasonmeidev/reversing-a-string-in-javascript-59ofテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol