Leetcode87:Scramble String 这个题目条件,我是没看明白,被坑了,但是还是值得看一遍的。

题目如下:

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = "great":

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /    \
  rg    eat
 / \    /  \
r   g  e   at
           / \
          a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

总之看到这种:我的第一反应是从中间分,结果是错的,人家是从任意 i 处可以分,不过焉知非福,我从中间分的看明白了规则,s1=a+b; s2=c+d;二叉树嘛;要么a的树可以和c的树相互转换,要么就是a的树可以和d的树想换转换,对于b来说同理。这道题就解开了,但是对于刚拿到这道题的我来说,我并没有立马想到,我想到了收到长度为1或者为2的叶子节点,但如何递归自己还是迷迷糊糊的,只是徘徊在利用递归,得到s1的所有可能,再和s2对比。真的是傻,s2也是一个参数条件呀,笨死了。

package test;

import java.util.Arrays;

public class LC87Try2
{
	public boolean isScramble(String s1, String s2)
	{
		boolean ret = false;
		if (s1.equals(s2))
		{
			return true;
		}
		int len = s1.length();
		//节省时间,所以进行了比较。
		char[] chars1 = s1.toCharArray();
		char[] chars2 = s2.toCharArray();

		Arrays.sort(chars1);
		Arrays.sort(chars2);
		for (int i = 0; i < len; i++)
		{
			if (chars1[i] != chars2[i])
				return false;
		}

		for (int i = 1; i < len; i++)
		{
			if (isScramble(s1.substring(0, i), s2.substring(0, i))
					&& isScramble(s1.substring(i), s2.substring(i)))
			{
				return true;
			}
			if (isScramble(s1.substring(0, i), s2.substring(len - i))
					&& isScramble(s1.substring(i), s2.substring(0, len - i)))
			{
				return true;
			}

		}

		return ret;
	}

	public static void main(String[] args)
	{
		LC87Try2 Object1 = new LC87Try2();
		System.out.println(Object1.isScramble("great", "rgtae"));
		System.out.println(Object1.isScramble("abb", "bab"));
		System.out.println(Object1.isScramble("abc", "bac"));
	}

}

嘿嘿,看一个,不知道规则时候的我写的一个代码;哈哈错误的

package test;

public class LC87Try1
{
	public boolean isScramble(String s1, String s2)
	{
		boolean ret = false;
		if (s1.equals(s2))
		{
			return true;
		}
		if(s1.length()!=s2.length()){
			return false;
		}
		if (s1.length() == 1)
		{
			return false;
		}
		int len = s1.length();
		int mid = len / 2;
		int left=mid;
		int right=len-mid;//但我喜欢这个变量,就不用判断参数是不是余1了。

		String m1 = s1.substring(0, left);
		String m2 = s1.substring(left);
		String m3 = s1.substring(0, right);
		String m4 = s1.substring(right);
		String n1 = s2.substring(0, left);
		String n2 = s2.substring(left);
		String n3 = s2.substring(0, right);
		String n4 = s2.substring(right);
		

		if (isScramble(m1, n1) && isScramble(m2, n2))
		{
			return true;
		}
		if (isScramble(m1, n4) && isScramble(m2, n3))
		{
			return true;
		}
		if (isScramble(m3, n2) && isScramble(m4, n1))
		{
			return true;
		}
		if (isScramble(m3, n3) && isScramble(m4, n4))
		{
			return true;
		}

		return ret;

	}

	public static void main(String[] args)
	{
		LC87Try1 Object1 = new LC87Try1();
		System.out.println(Object1.isScramble("great", "rgtae"));
		System.out.println(Object1.isScramble("abb", "bab"));
		System.out.println(Object1.isScramble("abc", "bac"));
	}
}

哈哈哈

猜你喜欢

转载自blog.csdn.net/ata_123/article/details/80671362
今日推荐