Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在你这里学习了,谢谢。并附上 0351 Android Unlock Pattern 的golang题解 #2

Open
ghost opened this issue Jul 23, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Jul 23, 2019

var blocker = [][]int{
	//  1  2  3  4  5  6  7  8  9   // first column is added so we use 1-9
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0 phantom node. as the STARTING point
	{0, 0, 0, 2, 0, 0, 0, 4, 0, 5}, // 1
	{0, 0, 0, 0, 0, 0, 0, 0, 5, 0}, // 2
	{0, 2, 0, 0, 0, 0, 0, 5, 0, 6}, // 3
	{0, 0, 0, 0, 0, 0, 5, 0, 0, 0}, // 4
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 5
	{0, 0, 0, 0, 5, 0, 0, 0, 0, 0}, // 6
	{0, 4, 0, 5, 0, 0, 0, 0, 0, 8}, // 7
	{0, 0, 5, 0, 0, 0, 0, 0, 0, 0}, // 8
	{0, 5, 0, 6, 0, 0, 0, 8, 0, 0}, // 9
}

var M, N int

func numberOfPatterns(m, n int) int {
	M = m
	N = n
	var work = make([]bool, 10, 10)
	return count(work, 0, 0)
}

func count(work []bool, now int, already int) (res int) {
	if already >= M && already <= N {
		res++
	} else if already > N {
		return
	}
	for i, tmp := 1, 0; i != 10; i++ {
		if work[i] { // if already visited
			continue
		}
		tmp = blocker[now][i]
		if tmp == 0 || work[tmp] {
			work[i] = true
			already++
			res += count(work, i, already)
			already--
			work[i] = false
		}
	}
	return
}
@halfrost
Copy link
Owner

感谢🙏🏻,这两天我把你这道题也做一下哈。解答代码里面我标明你的代码哈,给大家都一起学习🤝我们一起学习一起交流呀

@ghost
Copy link
Author

ghost commented Jul 23, 2019

🤝学习交流!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant