Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
commit
3e38718a76
68
src/main/java/leetcode/editor/cn/GrayCode.java
Normal file
68
src/main/java/leetcode/editor/cn/GrayCode.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。
|
||||||
|
//
|
||||||
|
// 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。
|
||||||
|
//
|
||||||
|
// 格雷编码序列必须以 0 开头。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: 2
|
||||||
|
//输出: [0,1,3,2]
|
||||||
|
//解释:
|
||||||
|
//00 - 0
|
||||||
|
//01 - 1
|
||||||
|
//11 - 3
|
||||||
|
//10 - 2
|
||||||
|
//
|
||||||
|
//对于给定的 n,其格雷编码序列并不唯一。
|
||||||
|
//例如,[0,2,3,1] 也是一个有效的格雷编码序列。
|
||||||
|
//
|
||||||
|
//00 - 0
|
||||||
|
//10 - 2
|
||||||
|
//11 - 3
|
||||||
|
//01 - 1
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: 0
|
||||||
|
//输出: [0]
|
||||||
|
//解释: 我们定义格雷编码序列必须以 0 开头。
|
||||||
|
// 给定编码总位数为 n 的格雷编码序列,其长度为 2n。当 n = 0 时,长度为 20 = 1。
|
||||||
|
// 因此,当 n = 0 时,其格雷编码序列为 [0]。
|
||||||
|
//
|
||||||
|
// Related Topics 回溯算法
|
||||||
|
// 👍 293 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//89:格雷编码
|
||||||
|
public class GrayCode {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new GrayCode().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<Integer> grayCode(int n) {
|
||||||
|
List<Integer> result = new ArrayList<Integer>();
|
||||||
|
result.add(0);
|
||||||
|
int head = 1;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = result.size() - 1; j >= 0; j--) {
|
||||||
|
result.add(head + result.get(j));
|
||||||
|
}
|
||||||
|
head <<= 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
35
src/main/java/leetcode/editor/cn/GrayCode.md
Normal file
35
src/main/java/leetcode/editor/cn/GrayCode.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>
|
||||||
|
|
||||||
|
<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>
|
||||||
|
|
||||||
|
<p>格雷编码序列必须以 0 开头。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 2
|
||||||
|
<strong>输出:</strong> <code>[0,1,3,2]</code>
|
||||||
|
<strong>解释:</strong>
|
||||||
|
00 - 0
|
||||||
|
01 - 1
|
||||||
|
11 - 3
|
||||||
|
10 - 2
|
||||||
|
|
||||||
|
对于给定的 <em>n</em>,其格雷编码序列并不唯一。
|
||||||
|
例如,<code>[0,2,3,1]</code> 也是一个有效的格雷编码序列。
|
||||||
|
|
||||||
|
00 - 0
|
||||||
|
10 - 2
|
||||||
|
11 - 3
|
||||||
|
01 - 1</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 0
|
||||||
|
<strong>输出:</strong> <code>[0]
|
||||||
|
<strong>解释:</strong> 我们定义</code>格雷编码序列必须以 0 开头。<code>
|
||||||
|
给定</code>编码总位数为<code> <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup></code>。<code>当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。
|
||||||
|
因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</code>
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>回溯算法</li></div></div>\n<div><li>👍 293</li><li>👎 0</li></div>
|
63
src/main/java/leetcode/editor/cn/HIndex.java
Normal file
63
src/main/java/leetcode/editor/cn/HIndex.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
//给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
|
||||||
|
//
|
||||||
|
// h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引
|
||||||
|
//用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。
|
||||||
|
//
|
||||||
|
// 例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:citations = [3,0,6,1,5]
|
||||||
|
//输出:3
|
||||||
|
//解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
|
||||||
|
// 由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:如果 h 有多种可能的值,h 指数是其中最大的那个。
|
||||||
|
// Related Topics 排序 哈希表
|
||||||
|
// 👍 143 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//274:H 指数
|
||||||
|
public class HIndex {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new HIndex().new Solution();
|
||||||
|
//3
|
||||||
|
System.out.println(solution.hIndex(new int[]{3,0,6,1,5}));
|
||||||
|
//0
|
||||||
|
System.out.println(solution.hIndex(new int[]{0}));
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int hIndex(int[] citations) {
|
||||||
|
int length = citations.length;
|
||||||
|
int[] counts = new int[length + 2];
|
||||||
|
for (int count : citations) {
|
||||||
|
if (count <= length) {
|
||||||
|
counts[count]++;
|
||||||
|
} else {
|
||||||
|
counts[length + 1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int sum = counts[length + 1];
|
||||||
|
int i = length;
|
||||||
|
for (; i > 0; i--) {
|
||||||
|
sum += counts[i];
|
||||||
|
if (sum >= i) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
20
src/main/java/leetcode/editor/cn/HIndex.md
Normal file
20
src/main/java/leetcode/editor/cn/HIndex.md
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<p>给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 <em>h </em>指数。</p>
|
||||||
|
|
||||||
|
<p><a href="https://baike.baidu.com/item/h-index/3991452?fr=aladdin" target="_blank">h 指数的定义</a>:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)<strong>总共</strong>有 h 篇论文分别被引用了<strong>至少</strong> h 次。且其余的 <em>N - h </em>篇论文每篇被引用次数 <strong>不超过 </strong><em>h </em>次。</p>
|
||||||
|
|
||||||
|
<p>例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong><code>citations = [3,0,6,1,5]</code>
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>给定数组表示研究者总共有 <code>5</code> 篇论文,每篇论文相应的被引用了 <code>3, 0, 6, 1, 5</code> 次。
|
||||||
|
由于研究者有 <code>3 </code>篇论文每篇 <strong>至少 </strong>被引用了 <code>3</code> 次,其余两篇论文每篇被引用 <strong>不多于</strong> <code>3</code> 次,所以她的 <em>h </em>指数是 <code>3</code>。</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong>如果 <em>h </em>有多种可能的值,<em>h</em> 指数是其中最大的那个。</p>
|
||||||
|
<div><div>Related Topics</div><div><li>排序</li><li>哈希表</li></div></div>\n<div><li>👍 143</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user