注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
这道面经题现在leetcode上已经有了,https://leetcode.com/problems/android-unlock-patterns/ 。
题目要求是:
Rules for a valid pattern:
1.Each pattern must connect at least m keys and at most n keys.
2.All the keys must be distinct.
3.If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
4.The order of keys used matters.
我参考了一个评论区的代码来判断合理路径,有点不能理解,测试是可以通过的。- private boolean check(int n1, int n2) {
- if (visited[n2]) {
- return false;
- }
- int r1 = (n1-1) / 3;
- int r2 = (n2-1) / 3;
- int c1 = (n1-1) % 3;
- int c2 = (n2-1) % 3;
- int smallN = Math.min(n1, n2);
- int colDiff = Math.abs(c1 - c2);
- int rowDiff = Math.abs(r1 - r2);
- if (colDiff == 2) {
- if (rowDiff == 2) {
- return visited[5];
- } else if (rowDiff == 0) {
- return visited[smallN + 1]; // check middle col
- }
- } else if (colDiff == 0) {
- if (rowDiff == 2) {
- return visited[smallN + 3]; // check middle row
- }
- }
- return true;
- }
复制代码 假如是这样的排列:
1 2 3
4 5 6
7 8 9
上面的判断方法不是会把1-6,2-9 这种路径判断为正确吗,这样的话不是不符合题意,为什么能提交通过呢?
请大家指点,谢谢!
|