LeetCode-Go icon indicating copy to clipboard operation
LeetCode-Go copied to clipboard

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

Open ghost opened this issue 6 years ago • 2 comments

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
}

ghost avatar Jul 23 '19 02:07 ghost

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

halfrost avatar Jul 23 '19 10:07 halfrost

🤝学习交流!

ghost avatar Jul 23 '19 12:07 ghost