檢查給定字串是否為註釋
在計算機程式設計中,註釋是用原始碼編寫的文字,但編譯器或直譯器會忽略它們。它們用於透過描述程式碼及其功能來提高程式碼的可讀性,以便閱讀程式碼的人(而非編譯器或直譯器)能夠理解。它們不會被執行,也不會影響程式的整體功能,它們僅供程式設計師參考。每種程式語言都有不同的語法來表示註釋。
C/C++ − 在 C 或 C++ 中,單行註釋以 '//' 開頭,多行註釋用 '/*' 和 '*/' 括起來。
// Single-lined comment /* Multi- lined comment */
Java − 在 Java 中,單行註釋以 '//' 開頭,多行註釋用 '/*' 和 '*/' 括起來。
// Single-lined comment /* Multi- lined comment */
Python − 在 Python 中,單行註釋以 # 開頭,三引號可用於編寫未賦值給變數的多行字串。
# Single-lined comment ''' Multi- lined comment '''
Javascript − 在 Javascript 中,單行註釋以 '//' 開頭,多行註釋用 '/*' 和 '*/' 括起來。
// Single-lined comment /* Multi- lined comment */
問題陳述
給定一個字串。檢查該字串是否為 C++ 中的註釋。
示例 1
Input: ‘/hello world */’
Output: FALSE
解釋 − 輸入字串既不以 '//' 開頭,也不包含在 '/*' 和 '*/' 之間。因此,該字串不是 C++ 中的註釋。
示例 2
Input: ‘//hello world */’
Output: TRUE
解釋 − 輸入字串以 '//' 開頭。因此,它是 C++ 中的註釋。
方法 1:單行註釋
單行註釋只跨越一行,並且可以透過在 C++ 中以 '//' 開頭來識別,即 C++ 中的單行註釋始終以 '//' 開頭。因此,為了檢查給定字串中的單行註釋,我們獲取字串中的前兩個字元,並檢查它們是否為 '//',如果是,則無論 '//' 字元後面是什麼,該字串都可以稱為單行註釋。
虛擬碼
procedure isComment (string) if string[0] == ‘/’ and string[1] == ‘/’ ans = TRUE end if ans = FALSE end procedure
示例
以下是上述方法的 C++ 實現。
在下面的程式中,我們檢查輸入字串的前兩個字元以檢查單行註釋。
#include <iostream> #include <string> using namespace std; // Function to check if the string is a single-lined comment bool isComment(string str){ // Single-lined comment if first two characters are '/' if (str[0] == '/' && str[1] == '/') { return true; } return false; } int main(){ string input = "/hello world */"; cout << "Input String: "<< input << endl; if (isComment(input)) { cout << "The input string is a comment." << endl; } else { cout << "The input string is not a comment." << endl; } return 0; }
輸出
編譯上述程式後,將產生以下輸出:
Input String: /hello world */ The input string is not a comment.
時間複雜度 − O(1),因為在 isComment() 函式中,我們使用索引檢查前兩個字元,這需要常數時間。
空間複雜度 − O(1),因為沒有使用額外的空間。
方法 2:多行註釋
多行註釋跨越多行,並且可以在 C++ 中識別為用 '/*' 和 '*/' 括起來的。因此,為了檢查給定字串中的多行註釋,我們獲取字串中的前兩個字元,並檢查它們是否為 '/*',並檢查最後兩個字元,並檢查它們是否為 '*/',如果是,則無論 '/*' 和 '*/' 之間是什麼,該字串都可以稱為多行註釋。
Input: ‘/* hello world */’
Output: TRUE
解釋 − 輸入字串包含在 '/*' 和 '*/' 中,因此它是 C++ 中的字串。
虛擬碼
procedure isComment (string) n = string.length if (string[0] == ‘/’ and string[1] == ‘*’) and (string[n - 1] == ‘/’ and string[n - 2] == ‘*’) ans = TRUE end if ans = FALSE end procedure
示例:C++ 實現
在下面的程式中,我們檢查輸入字串是否包含在 '/*' 和 '*/' 中。
#include <iostream> #include <string> using namespace std; // Function to check for multi-lined comment bool isComment(string str){ int n = str.length(); // Multi-lined comment if first two characters are '/*' and last two characters are '*/' if ((str[0] == '/' && str[1] == '*') && (str[n-1] == '/' && str[n-2] == '*')) { return true; } return false; } int main(){ string input = "/* hello world */"; cout << "Input String: " << input << endl; if (isComment(input)) { cout << "The input string is a comment." << endl; } else { cout << "The input string is not a comment." << endl; } return 0; }
輸出
編譯上述程式後,將產生以下輸出:
Input String: /* hello world */ The input string is a comment.
時間複雜度 − O(1),因為在 isComment() 函式中,我們使用索引檢查前兩個和後兩個字元,這需要常數時間。
空間複雜度 − O(1),因為沒有使用額外的空間。
方法 3:單行和多行註釋
對於給定的字串,要查詢註釋是單行註釋還是多行註釋,我們將結合上述兩種方法,其中單行註釋以 '//' 開頭,多行註釋包含在 '/*' 和 '*/' 中。
Input: ‘/&* hello world */’
Output: Not a comment
虛擬碼
procedure isComment (string) n = string.length if string[0] == ‘/’ and string[1] == ‘/’ ans = 1 else if (string[0] == ‘/’ and string[1] == ‘*’) and (string[n - 1] == ‘/’ and string[n - 2] == ‘*’) ans = 2 end if ans = 0 end procedure
示例:C++ 實現
在下面的程式中,給定一個字串,我們檢查它是否是單行註釋、多行註釋還是根本不是註釋。
#include <iostream> #include <string> using namespace std; // FUunction to check if the input string is comment int isComment(string str){ int n = str.length(); // SIngle-lined comment if starting with '//' if (str[0] == '/' && str[1] == '/') { return 1; } // Multi-lined comment if enclosed in '/*' and '*/' else if ((str[0] == '/' && str[1] == '*') && (str[n-1] == '/' && str[n-2] == '*')) { return 2; } // Not a comment return 0; } int main(){ string input = "// hello world */"; cout << "Input String: " << input << endl; if (isComment(input) == 1) { cout << "The input string is a single-lined comment." << endl; } else if (isComment(input) == 2) { cout << "The input string is a multi-lined comment." << endl; } else { cout << "The input string is not a comment." << endl; } return 0; }
輸出
Input String: // hello world */ The input string is a single-lined comment.
時間複雜度 − O(1),因為在 isComment() 函式中,我們使用索引檢查註釋說明符,這需要常數時間。
空間複雜度 − O(1),因為沒有使用額外的空間。
結論
總之,不同的程式語言有不同的語法來表示註釋。在上述方法中,C 或 C++ 中的註釋已識別,時間和空間複雜度均為 O(1)。