Skip to content

Commit 8cbcc37

Browse files
committed
fix: update style 🏁
1 parent 60f5a7f commit 8cbcc37

File tree

1 file changed

+41
-96
lines changed

1 file changed

+41
-96
lines changed

package/Other/ColorBricks.php

Lines changed: 41 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
* @date 2017/9/1
77
* @license MIT
88
* -------------------------------------------------------------
9-
* 思路分析:额。。。 没做出来求大佬带
9+
* 思路分析:
10+
* 例如: s = "ABAB",那么小易有六种排列的结果:
11+
* "AABB","ABAB","ABBA","BAAB", "BBAA" "BABA",
12+
* 1-2 0 2-3 0-1 0-3 1-2
13+
* "ABAB", 0
14+
* "BAAB", 0-1
15+
* "AABB", 0-2
16+
* "BABA" 0-3
17+
* "ABBA", 1-2
18+
* "BBAA" 1-3
1019
* -------------------------------------------------------------
1120
* 小易有一些彩色的砖块。每种颜色由一个大写字母表示。各个颜色砖块看起来都完全一样。
1221
* 现在有一个给定的字符串s,s中每个字符代表小易的某个砖块的颜色。小易想把他所有的砖块排成一行。
@@ -32,107 +41,43 @@
3241
*/
3342
function ColorBricks($inputting)
3443
{
35-
$str = ord($inputting);
36-
$strLen = strlen($inputting);
37-
// if (!($str > 64 && $str < 91) ||
38-
// !(1 <= $strLen && $strLen <= 50)
39-
// ) {
40-
// return false;
41-
// }
42-
$group = [];
43-
$temp = '';
44+
$count = strlen($inputting);
45+
$ascii = ord($inputting);
4446

45-
// 表示标排列 有24 种排列方式
46-
// 0123 0132 0213 0231 0312 0321
47-
// 1023 1032 1203 1230 1320 1302
48-
// 2013 2031 2130 2103 2301 2310
49-
// 3012 3021 3120 3102 3201 3210
47+
if (!($ascii > 64 && $ascii < 91)) {
48+
return false;
49+
}
50+
if (!(1 <= $count && $count <= 50)) {
51+
return false;
52+
}
5053

51-
// 0123 0132
52-
// 0213 0231
53-
// 0312 0321
54+
$container = array ();
55+
$max = floor($count / 2);
5456

55-
// 额。。。 没做出来求大佬带
57+
for ($i = 0; $i < $max; $i++) {
58+
for ($j = 1; $j < $count; $j++) {
59+
$temp = $inputting[$j];
60+
$inputting[$j] = $inputting[0];
61+
$inputting[0] = $temp;
62+
array_push($container, $inputting);
63+
}
64+
}
65+
$containerCount = count($container);
66+
$counter = 0;
5667

57-
for ($i = 1; $i < $strLen; $i++) {
58-
// $group[$inputting] = true;
59-
for ($j = 1; $j < $strLen ; $j++) {
60-
echo $i, $j, PHP_EOL;
61-
if (isset($inputting[$i + 1])) {
62-
$temp = $inputting[$i];
63-
$inputting[$i] = $inputting[$i + 1];
64-
$inputting[$i + 1] = $temp;
68+
for ($i = 0; $i < $max; $i++) {
69+
$success = false;
70+
for ($j = 0; $j < $containerCount; $j++) {
71+
$str = $container[$j];
72+
if ($str[$i] == $str[0]) {
73+
$success = true;
6574
}
6675
}
76+
if ($success) {
77+
$counter++;
78+
}
6779
}
68-
var_dump($group);
80+
return $counter;
6981
}
7082

71-
echo ColorBricks("AABB");
72-
73-
74-
#########################################
75-
76-
77-
78-
function ColorBricks2($str)
79-
{
80-
$count = strlen($str);
81-
$data = array();
82-
for($i = 0; $i < $count; $i++){
83-
$data[] = $str[$i];
84-
}
85-
$a = pre2($data);
86-
}
87-
88-
/*
89-
思路分析:
90-
91-
* 例如: s = "ABAB",那么小易有六种排列的结果:
92-
* "AABB","ABAB","ABBA","BAAB", "BBAA" "BABA",
93-
1-2 0 2-3 0-1 0-3 1-2
94-
"ABAB", 0
95-
"BAAB", 0-1
96-
"AABB", 0-2
97-
"BABA" 0-3
98-
"ABBA", 1-2
99-
"BBAA" 1-3
100-
101-
*/
102-
103-
function pre2($data)
104-
{
105-
$c = count($data);
106-
$new = array();
107-
$max = floor($c/2);
108-
for($i = 0; $i < $max;$i++){
109-
for($j = 1; $j < $c;$j++){
110-
$temp = $data[$j];
111-
$data[$j] = $data[0];
112-
$data[0] = $temp;
113-
$new[] = implode("",$data);
114-
echo '0##'.$j.'<br/>';
115-
}
116-
}
117-
$nMax = count($new);
118-
$successNumber = 0;
119-
for($i = 0 ; $i < $max;$i++){
120-
$success = false;
121-
for($j = 0; $j < $nMax;$j++){
122-
$str = $new[$j];
123-
if($str[$i] == $str[0]){
124-
$success = true;
125-
}
126-
}
127-
if($success)$successNumber++;
128-
}
129-
echo '<pre>';
130-
echo "小易可以有{$successNumber}种方式";
131-
echo "<br/><br/><br/><br/>";
132-
print_r($new);
133-
exit;
134-
}
135-
$str = "ABAB";
136-
ColorBricks2($str);
137-
138-
83+
echo ColorBricks("AABB");

0 commit comments

Comments
 (0)