From a9ab290f4f3f8c13def9837256009b0876ca8706 Mon Sep 17 00:00:00 2001 From: pezy Date: Tue, 27 Jan 2015 13:16:31 +0800 Subject: [PATCH] improved formatting for 103 --- 103. Scramble String/README.md | 41 +++++++++++++--------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/103. Scramble String/README.md b/103. Scramble String/README.md index f96a234..5da3b47 100644 --- a/103. Scramble String/README.md +++ b/103. Scramble String/README.md @@ -1,6 +1,6 @@ -两个字符串 s1, s2, 问是否是 scramble. --> 定义问题的解为:`S(s1, s2)`, s1,s2 的长度为 n (若不等,直接 `return false;`). +涓や釜瀛楃涓 s1, s2, 闂槸鍚︽槸 scramble. --> 瀹氫箟闂鐨勮В涓猴細`S(s1, s2)`, s1,s2 鐨勯暱搴︿负 n (鑻ヤ笉绛夛紝鐩存帴 `return false;`). -然后看给出的例子: +鐒跺悗鐪嬬粰鍑虹殑渚嬪瓙锛 gr|eat rg|tae ----- i == 2 (1) / \ / \ @@ -10,13 +10,13 @@ / \ / \ a t t a -可以来看看,这里面究竟有几种情况。按照上图所示的步骤进行分析: +鍙互鏉ョ湅鐪嬶紝杩欓噷闈㈢┒绔熸湁鍑犵鎯呭喌銆傛寜鐓т笂鍥炬墍绀虹殑姝ラ杩涜鍒嗘瀽锛 -1. `S(s1, s2) = S(s1.substr(0,i), s2.substr(0,i)) && S(s1.substr(i), s2.substr(i))` (第一种可能) -2. 重点看 `e|at` 与 `ta|e`, 可以推得: `S(s1, s2) = S(s1.substr(0,i), s2.substr(n-i)) && S(s1.substr(i), s2.substr(0, n-i))` (第二种可能) -3. 可以并入 2. 但可以看出 S 最简单的解: `S("a", "a") == true` 以及 `S("t", "t") == true`. 即 `if (s1 == s2) return true;` +1. `S(s1, s2) = S(s1.substr(0,i), s2.substr(0,i)) && S(s1.substr(i), s2.substr(i))` (绗竴绉嶅彲鑳) +2. 閲嶇偣鐪 `e|at` 涓 `ta|e`, 鍙互鎺ㄥ緱: `S(s1, s2) = S(s1.substr(0,i), s2.substr(n-i)) && S(s1.substr(i), s2.substr(0, n-i))` (绗簩绉嶅彲鑳) +3. 鍙互骞跺叆 2. 浣嗗彲浠ョ湅鍑 S 鏈绠鍗曠殑瑙: `S("a", "a") == true` 浠ュ強 `S("t", "t") == true`. 鍗 `if (s1 == s2) return true;` -综上三步,可以得到递归的公式: +缁间笂涓夋锛屽彲浠ュ緱鍒伴掑綊鐨勫叕寮忥細 _________ true; S(s1, s2) _|s1 == s2 @@ -25,45 +25,34 @@ |__ S(s1.substr(0,i), s2.substr(n-i)) && S(s1.substr(i), s2.substr(n-i)) -显然 i 的范围是 [1 ,n-1]. +鏄剧劧 i 鐨勮寖鍥存槸 [1 ,n-1]. ----- -有了这个公式做支撑,我们基本看到了递归的雏形。也可以试着用这个来写出代码。 +鏈変簡杩欎釜鍏紡鍋氭敮鎾戯紝鎴戜滑鍩烘湰鐪嬪埌浜嗛掑綊鐨勯洀褰€備篃鍙互璇曠潃鐢ㄨ繖涓潵鍐欏嚭浠g爜銆 ```cpp - if (s1.empty() || s2.empty() || s1.size() != s2.size()) return false; - else if (s1 == s2) return true; - - for (size_t i=1; i