diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a94212f --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +################################################################################ +# æ­¤ .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 +################################################################################ + +*.suo +*.db +*.opendb +*.json +*.sqlite diff --git a/LeafScar/expressive_words/expressive_words.cpp b/LeafScar/expressive_words/expressive_words.cpp new file mode 100644 index 0000000..8d82840 --- /dev/null +++ b/LeafScar/expressive_words/expressive_words.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +class Solution { +public: + int expressiveWords(string S, std::vector& words) { + int num = 0; + for (int i = 0; i < words.size(); ++i) + { + if (expressiveWord(S, words[i])) + num++; + } + return num; + } + + bool expressiveWord(string s, string t) + { + int s_i = 0; + int t_i = 0; + + while (s_i < s.length() && t_i < t.length()) + { + if (s[s_i] != t[t_i]) + return false; + + int s_flag = 0; + while (s[s_i] == t[t_i]) + { + s_i++; + s_flag++; + } + + int t_flag = 0; + while (s[s_i - 1] == t[t_i]) + { + t_flag++; + t_i++; + } + + if (t_flag == s_flag || (s_flag >= 3 && t_flag <= s_flag)) + continue; + + return false; + } + return s_i == s.length() && t_i == t.length(); + } +}; diff --git a/LeafScar/expressive_words/file.png b/LeafScar/expressive_words/file.png new file mode 100644 index 0000000..b28dcca Binary files /dev/null and b/LeafScar/expressive_words/file.png differ diff --git a/LeafScar/expressive_words/readme.md b/LeafScar/expressive_words/readme.md new file mode 100644 index 0000000..88a7245 --- /dev/null +++ b/LeafScar/expressive_words/readme.md @@ -0,0 +1,93 @@ +# LeetCode 809¡ª¡ªÇé¸Ð·á¸»µÄÎÄ×Ö +### ÌâÄ¿ +  ÓÐʱºòÈËÃÇ»áÓöîÍâµÄ×ÖĸÀ´±íʾ¶îÍâµÄÇé¸Ð£¬±ÈÈç "hello" -> "heeellooo", "hi" -> "hiii"¡£ÎÒÃǽ«Á¬ÐøµÄÏàͬµÄ×Öĸ·Ö×飬²¢ÇÒÏàÁÚ×éµÄ×Öĸ¶¼²»Ïàͬ¡£ÎÒÃǽ«Ò»¸öÓµÓÐÈý¸ö»òÒÔÉÏ×ÖĸµÄ×鶨ÒåΪÀ©ÕÅ״̬£¨extended£©£¬ÈçµÚÒ»¸öÀý×ÓÖÐµÄ "e" ºÍ" o" ÒÔ¼°µÚ¶þ¸öÀý×ÓÖÐµÄ "i"¡£ ´ËÍ⣬"abbcccaaaa" ½«ÓзÖ×é "a" , "bb" , "ccc" , "dddd"£»ÆäÖÐ "ccc" ºÍ "aaaa" ´¦ÓÚÀ©ÕÅ״̬¡£ + +  ¶ÔÓÚÒ»¸ö¸ø¶¨µÄ×Ö·û´® S £¬Èç¹ûÁíÒ»¸öµ¥´ÊÄܹ»Í¨¹ý½«Ò»Ð©×Öĸ×éÀ©ÕÅ´Ó¶øÊ¹ÆäºÍ S Ïàͬ£¬ÎÒÃǽ«Õâ¸öµ¥´Ê¶¨ÒåΪ¿ÉÀ©Õŵģ¨stretchy£©¡£ÎÒÃÇÔÊÐíÑ¡ÔñÒ»¸ö×Öĸ×飨Èç°üº¬×Öĸ c £©£¬È»ºóÍùÆäÖÐÌí¼ÓÏàͬµÄ×Öĸ c ʹÆä³¤¶È´ïµ½ 3 »òÒÔÉÏ¡£×¢Ò⣬ÎÒÃDz»Äܽ«Ò»¸öÖ»°üº¬Ò»¸ö×ÖĸµÄ×Öĸ×飬Èç "h"£¬À©Õŵ½Ò»¸ö°üº¬Á½¸ö×ÖĸµÄ×飬Èç "hh"£»ËùÓеÄÀ©ÕűØÐëʹ¸Ã×Öĸ×é±ä³ÉÀ©ÕÅ״̬£¨ÖÁÉÙ°üº¬Èý¸ö×Öĸ£©¡£ + +  ÊäÈëÒ»×éµ¥´Ê£¬Êä³öÆäÖпÉÀ©Õŵĵ¥´ÊÊýÁ¿¡£ + +**ʾÀý£º** +**ÊäÈ룺** +``` +S = "heeellooo" +words = ["hello", "hi", "helo"] +``` +**Êä³ö£º** +``` +1 +``` +**½âÊÍ£º** +ÎÒÃÇÄÜͨ¹ýÀ©ÕÅ"hello"µÄ"e"ºÍ"o"À´µÃµ½"heeellooo"¡£ +ÎÒÃDz»ÄÜͨ¹ýÀ©ÕÅ"helo"À´µÃµ½"heeellooo"ÒòΪ"ll"²»´¦ÓÚÀ©ÕÅ״̬¡£ + + +**˵Ã÷£º** +* 0 <= len(S) <= 100¡£ +* 0 <= len(words) <= 100¡£ +* 0 <= len(words[i]) <= 100¡£ +* S ºÍËùÓÐÔÚ words Öеĵ¥´Ê¶¼Ö»ÓÉСд×Öĸ×é³É¡£ + +### ˼· + +**¶ÔÌâÄ¿µÄÀí½â£º** +* ²»À©³ä״̬£ºÔ­µ¥´ÊµÄµ¥¸ö×ÖĸÁ¬Ðø³öÏÖ´ÎÊýÓëÀ©³äºóµ¥´Ê¶ÔÓ¦´¦Á¬Ðø³öÏÖ´ÎÊýÏàµÈ +* À©³ä״̬£ºÔ­µ¥´ÊµÄµ¥¸ö×ÖĸÁ¬Ðø³öÏÖ´ÎÊýÓëÀ©³äºóµ¥´Ê¶ÔÓ¦×Öĸ´¦±È½Ï£¬À©³äºóµ¥´Ê¶ÔÓ¦×ÖĸÁ¬Ðø³öÏÖ´ÎÊý´óÓÚÔ­µ¥´ÊµÄµ¥¸ö×ÖĸÁ¬Ðø³öÏÖ´ÎÊý£¬ÇÒ±ØÐë´óÓÚ3´Î + +**ʵÏÖÏë·¨£º** + +Ô­µ¥´ÊÓëÀ©³äºóµ¥´Ê˳ÐòɨÃ裬¼Ç¼µ¥¸ö×ÖĸÁ¬Ðø³öÏÖ´ÎÊý£¬·Ö±ð¼ÇΪt_flagÓës_flag£» +![File](file.png) +1. Èç¹û˳ÐòɨÃèʱ£¬³öÏÖÀ©³ä×ÖĸÓëÔ­µ¥´Ê×Öĸ²»Í¬£¬Ö±½Ó·µ»Øfalse£» +2. t_flagµÈÓÚs_flag »òÕß £¨s_flag´óÓÚµÈÓÚ3 ÇÒ s_flagµÄÖµ´óÓÚt_flagµÄÖµ£©²Å½øÐÐÏÂÒ»ÖÖ×ÖĸµÄÅжϣ» +3. Ö±µ½ÆäÖÐÒ»¸öµ¥´ÊɨÃèÍê½áÊø£¬ÁíÒ»¸öµ¥´Ê»¹Î´É¨ÃèÍ꣬ÄÇÒ»¶¨ÊÇfalse; + +**´úÂëʵÏÖ£º** +``` +bool expressiveWord(string s, string t) +{ + int s_i = 0; // À©³äµ¥´ÊɨÃèµ½µÄϱê + int t_i = 0; // Ô­µ¥´ÊɨÃèµ½µÄϱê + + while (s_i < s.length() && t_i < t.length()) + { + if (s[s_i] != t[t_i]) + return false; + + int s_flag = 0; + while (s[s_i] == t[t_i]) // ¼Ç¼À©³äµ¥´Ê×ÖĸÁ¬ÐøÓóöÏÖ´ÎÊý + { + s_i++; + s_flag++; + } + + int t_flag = 0; + while (s[s_i - 1] == t[t_i]) // ¼Ç¼ԭµ¥´Ê×ÖĸÁ¬ÐøÓóöÏÖ´ÎÊý + { + t_flag++; + t_i++; + } + + if (t_flag == s_flag || (s_flag >= 3 && t_flag <= s_flag)) + continue; + + return false; + } + return s_i == s.length() && t_i == t.length(); +} + +// ½ÓÏÂÈ¥¾ÍÊDZéÀúÕû×éµ¥´Ê£¬¼Ç¼Âú×ã·ûºÏ¸ø¶¨À©³äµ¥´ÊµÄÔ­µ¥´ÊµÄÊýÁ¿ +int expressiveWords(string S, std::vector& words) { + int num = 0; + for (int i = 0; i < words.size(); ++i) + { + if (expressiveWord(S, words[i])) + num++; + } + return num; +} +``` + + + + +