日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode-Scramble String

發布時間:2023/12/10 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode-Scramble String 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

哎,難題又不會做,

思路沒有什么難度,

關鍵是要把問題思考透徹,

要考慮的要點還是挺多的,不容易一下子都考慮到;

我開始的思路是按照樹的根去遞歸的,

這樣就必須要沒有重復元素,因為需要每次在s2中查找s1中的元素;

怎么說呢,有點考慮的過于specific了,其實直接把字符串分成兩部分遞歸就可以了,

不過需要注意不能漏掉兩部分可能次序調換的情況。

1 class Solution { 2 public: 3 bool isScramble(string s1, string s2) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (s1.empty() && s2.empty()) { 7 return true; 8 } 9 if (s1 == s2) { 10 return true; 11 } 12 return scramble(s1, 0, s1.size() - 1, s2, 0, s2.size() - 1); 13 } 14 bool scramble(string &s1, int b1, int e1, string &s2, int b2, int e2) { 15 if (!same(s1, b1, e1, s2, b2, e2)) { 16 return false; 17 } 18 int len = e1 - b1 + 1; 19 if (len == 0) { 20 return true; 21 } 22 if (len == 1) { 23 return (s1[b1] == s2[b2]); 24 } 25 for (int i = 0; i < len - 1; ++i) { 26 if ((scramble(s1, b1, b1 + i, s2, b2, b2 + i) && scramble(s1, b1 + i + 1, e1, s2, b2 + i + 1, e2)) || 27 (scramble(s1, b1, b1 + i, s2, e2 - i, e2)) && scramble(s1, b1 + i + 1, e1, s2, b2, e2 - i - 1)) { 28 return true; 29 } 30 } 31 return false; 32 } 33 bool same(string &s1, int b1, int e1, string &s2, int b2, int e2) { 34 vector<int> count(256, 0); 35 for (int i = b1; i <= e1; ++i) { 36 ++count[s1[i]]; 37 } 38 for (int i = b2; i <= e2; ++i) { 39 --count[s2[i]]; 40 } 41 for (int i = 0; i < 256; ++i) { 42 if (count[i] != 0) { 43 return false; 44 } 45 } 46 return true; 47 } 48 };

?

轉載于:https://www.cnblogs.com/chasuner/p/scramble.html

總結

以上是生活随笔為你收集整理的LeetCode-Scramble String的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。