From 9028c46d59cbdeab99be24fbd86a9206e9c18766 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sun, 24 Apr 2022 12:26:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20868=20&=20Modify=202069?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\250\241\346\213\237.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 2 +- ...10\347\256\200\345\215\225\357\274\211.md" | 81 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 "LeetCode/861-870/868. \344\272\214\350\277\233\345\210\266\351\227\264\350\267\235\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index b7acb766..b6d745b2 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -96,6 +96,7 @@ | [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 | | [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 | | [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 | +| [868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/) | 简单 | 🤩🤩🤩🤩 | | [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 | | [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 | | [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/2061-2070/2069. \346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272 II\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/2061-2070/2069. \346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272 II\357\274\210\344\270\255\347\255\211\357\274\211.md" index 9c3e9db6..f0f449aa 100644 --- "a/LeetCode/2061-2070/2069. \346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272 II\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/2061-2070/2069. \346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272 II\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -115,7 +115,7 @@ class Robot { ### 最后 -这是我们「刷穿 LeetCode」系列文章的第 `No.2169` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 +这是我们「刷穿 LeetCode」系列文章的第 `No.2069` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 diff --git "a/LeetCode/861-870/868. \344\272\214\350\277\233\345\210\266\351\227\264\350\267\235\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/861-870/868. \344\272\214\350\277\233\345\210\266\351\227\264\350\267\235\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..9515bd27 --- /dev/null +++ "b/LeetCode/861-870/868. \344\272\214\350\277\233\345\210\266\351\227\264\350\267\235\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,81 @@ +### 题目描述 + +这是 LeetCode 上的 **[868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +给定一个正整数 $n$,找到并返回 $n$ 的二进制表示中两个 相邻 $1$ 之间的 最长距离 。如果不存在两个相邻的 $1$,返回 $0$ 。 + +如果只有 $0$ 将两个 $1$ 分隔开(可能不存在 $0$ ),则认为这两个 $1$ 彼此 相邻 。两个 $1$ 之间的距离是它们的二进制表示中位置的绝对差。例如,`"1001"` 中的两个 $1$ 的距离为 $3$ 。 + +示例 1: +``` +输入:n = 22 + +输出:2 + +解释:22 的二进制是 "10110" 。 +在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。 +第一对相邻的 1 中,两个 1 之间的距离为 2 。 +第二对相邻的 1 中,两个 1 之间的距离为 1 。 +答案取两个距离之中最大的,也就是 2 。 +``` +示例 2: +``` +输入:n = 8 + +输出:0 + +解释:8 的二进制是 "1000" 。 +在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。 +``` +示例 3: +``` +输入:n = 5 + +输出:2 + +解释:5 的二进制是 "101" 。 +``` + +提示: +* $1 <= n <= 10^9$ + +--- + +### 模拟 + +根据题意进行模拟即可,遍历 $n$ 的二进制中的每一位 $i$,同时记录上一位 $1$ 的位置 $j$,即可得到所有相邻 $1$ 的间距,所有间距取 $\max$ 即是答案。 + +代码: +```Java +class Solution { + public int binaryGap(int n) { + int ans = 0; + for (int i = 31, j = -1; i >= 0; i--) { + if (((n >> i) & 1) == 1) { + if (j != -1) ans = Math.max(ans, j - i); + j = i; + } + } + return ans; + } +} +``` +* 时间复杂度:$O(\log{n})$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.868` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +