LeetCode 796 Rotate String
LeetCode 796 Rotate String
転送ゲート:LeetCode 796
テーマ分析
タイトル原文:
We are given two strings,
A shift on A consists of taking string
タイトルは、1つの文字列が
考える
タイトルの要求は簡単で、1つの文字列に対して
コード実装
感想
略
転送ゲート:LeetCode 796
テーマ分析
タイトル原文:
We are given two strings,
A
and B
. A shift on A consists of taking string
A
and moving the leftmost character to the rightmost position. For example, if A = 'abcde'
, then it will be 'bcdea'
after one shift on A
. Return True
if and only if A
can become B
after some number of shifts on A
. Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true
Example 2:
Input: A = 'abcde', B = 'abced'
Output: false
タイトルは、1つの文字列が
操作を経て別の文字列と等しくなり、循環シフトと同様に、与えられた2つの文字列がこの関係を満たしているかどうかを判断することである.考える
タイトルの要求は簡単で、1つの文字列に対して
操作を行って別の文字列と等しくし、シフト操作は私に循環キューを使う操作を考えさせ、キューの頭の要素をキューの尾に加えて、等しいかどうかを判断して、構想は循環キューを使う思想を比較して、時間の複雑度はO(O 2 O 2)であるべきです.コード実装
using ull = unsigned long long;
class Solution {
public:
bool rotateString(string A, string B) {
if (A.length() != B.length()) {
return false;
}
ull length = A.length();
//
for (int i = 0; i < length; ++i) {
bool same = true;
for (int j = 0; same && j < length; ++j) {
if (A[j] != B[(j + i) % length]) {
same = false;
}
}
// true
if (same) {
return true;
}
}
//
return false;
}
};
感想
略