- Function rank ($ base, $ temp = null)
- {
- $ Len = strlen ($ base );
- If ($ len <= 1)
- {
- Echo $ temp. $ base .'
';
- }
- Else
- {
- For ($ I = 0; $ I <$ len; ++ $ I)
- {
- Rank (substr ($ base, 0, $ I ). substr ($ base, $ I + 1, $ len-$ i-1), $ temp. $ base [$ I]);
- }
- }
- }
- Rank ('20140901 ');
- ?>
However, after multiple tests, we found that there was a problem: if the same element exists, there would be duplicates in the full arrangement. For example, there are only three conditions for the full arrangement of '20160301': '20160301', '20160301', and '20160301'. the above method is repeated. Make a slight modification, add a mark to judge the repetition, and solve the problem.
- Function fsRank ($ base, $ temp = null)
- {
- Static $ ret = array ();
- $ Len = strlen ($ base );
- If ($ len <= 1)
- {
- // Echo $ temp. $ base .'
';
- $ Ret [] = $ temp. $ base;
- }
- Else
- {
- For ($ I = 0; $ I <$ len; ++ $ I)
- {
- $ Had_flag = false;
- For ($ j = 0; $ j <$ I; ++ $ j)
- {
- If ($ base [$ I] ==$ base [$ j])
- {
- $ Had_flag = true;
- Break;
- }
- }
- If ($ had_flag)
- {
- Continue;
- }
- FsRank (substr ($ base, 0, $ I ). substr ($ base, $ I + 1, $ len-$ i-1), $ temp. $ base [$ I]);
- }
- }
- Return $ ret;
- }
- Print'
'; - print_r(fsRank('122'));
- print '
';
- ?>
This article introduces a fully-arranged recursive algorithm. Here we recommend a non-recursive algorithm implementation code for php arrays. for details, refer. |