当我使用Iterator迭代 some 时TreeMap,我发现相同Map.Entry的内容会发生变化。例如:
import java.util.Map.Entry;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) {
TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(1,1);
map.put(2,2);
map.put(3,3);
System.out.println("map: " + map);
Map<Integer, Integer> fromMap = map.tailMap(2);
System.out.println("fromMap: " + fromMap);
Iterator<Entry<Integer, Integer>> iter = fromMap.entrySet().iterator();
Entry<Integer, Integer> entry = iter.next();
System.out.println(entry); // line 1
iter.remove();
System.out.println(entry); // line 2. Why does entry content change?
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
map: {1=1, 2=2, 3=3}
fromMap: {2=2, 3=3}
2=2 …Run Code Online (Sandbox Code Playgroud) 第一个代码:
List<Integer>[] array = (List<Integer>[]) new Object[size];
Run Code Online (Sandbox Code Playgroud)
它将给出以下异常:
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.util.List; ([Ljava.lang.Object; and [Ljava.util.List; are in module java.base of loader 'bootstrap')
为什么会这样呢?我只是按照有效Java第三版 132页的方法进行操作:
第二码:
E[] array = (E[]) new Object[size];
Run Code Online (Sandbox Code Playgroud)
但是我发现以下代码有效
第三码:
List<Integer>[] array = (List<Integer>[]) new List[size];
Run Code Online (Sandbox Code Playgroud)
我的问题:
例如:为什么以下代码运行良好,但是第一个代码错误?
public class Test<E>{
E[] array;
public Test(){
array = (E[]) new Object[10];
}
public E set(E x){
array[0] = x;
System.out.println(array[0]);
return array[0];
}
public static void …Run Code Online (Sandbox Code Playgroud) 例如,给定一个 Integer 列表List<Integer> list = Arrays.asList(5,4,5,2,2),如何maxHeap从该列表中获取O(n)时间复杂度的 a ?
天真的方法:
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (Integer i : list) {
maxHeap.offer(i);
}
Run Code Online (Sandbox Code Playgroud)
然而,时间复杂度为O(nlogn)。
我们可以使用以下构造函数来触发heapify方法:
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(list);
Run Code Online (Sandbox Code Playgroud)
时间复杂度为O(n). 然而,它迫使我使用自然顺序,即minHeap。
我的问题:
如何通过使用自定义比较器堆化集合来构造 PriorityQueue?
PS:@user207421
Heapify算法可以及时将任何未排序的数组转换为堆O(n),而不是O(nlogn)。关于heapify的文章有很多,也在CLRS的Introduction to Algorithms Page 159中,从任何未排序的数组构建堆是O(n)。而且堆也不是排序数组。它是一个完整的树,具有堆属性,可以用数组进行编码。
例如,给定一个数组
arr = np.array([1,2,3,2,3,7,2,3,4])
Run Code Online (Sandbox Code Playgroud)
有9元素。
我想求每个3元素的总和:
[6, 12, 9]
Run Code Online (Sandbox Code Playgroud)
有没有我可以使用的 numpy api?
我遇到了类似的问题,可以简化如下:
例如我有一个文件结构如下:
----folder
---- main.py
---- math.py
Run Code Online (Sandbox Code Playgroud)
我在 中定义了一个函数math.py,我想将其导入math.py到 中main.py。
中的代码math.py如下
# math.py
def f(x) :
return x**3
Run Code Online (Sandbox Code Playgroud)
如果我编写main.py如下代码
# main.py
import math
def main() :
print(math.f(3))
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
然后它返回AttributeError: module 'math' has no attribute 'f'
如果我编写main.py如下代码
# main.py
from . import math
def main() :
print(math.f(3))
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
然后它返回ImportError: cannot import name 'math' from '__main__' (main.py)
我的问题:
如果我只想 …
PS: 考虑到有关 JDK 实现细节的动机或权衡的讨论经常会在 StackOverflow 上遇到阻力,普遍认为 JDK 工程师有权独立做出决策(之前关于 JDK 动机的帖子的关闭证明了这一点) ,我想澄清的是,这个问题严格集中在 HashMap 的算法和结构方面,特别是在两个单独的链接实现(头插入和尾插入)之间进行选择时涉及的分析和工程考虑。
在单独链接的HashMap 中,冲突是通过链接列表中的链接条目来管理的。当发生冲突时,新元素可以插入到链表的头部或尾部。两种方法具有相同的最坏时间复杂度,因为需要对列表进行完整扫描来识别重复项或插入点。然而,传统智慧和算法教育(例如,Sedgewick 的算法来源,算法简介,CLRS p.258)表明最好在头部插入,因为较新的元素更有可能很快被访问。
奇怪的是,虽然 JDK7 HashMap(第 402、766 行和JDK 7 源代码addEntry()中的方法)通过在头部插入来遵循此约定,但JDK8 切换到尾部插入(第 611、641 行和JDK 8 源代码中的方法)。putVal()
我的问题是:在单独链接的 HashMap 中,头部插入和尾部插入之间的权衡是什么?是否存在可能影响此选择的实际工程考虑因素(例如多线程问题)?我遇到过一些讨论(例如,此博客),表明如果同步不正确,在头部插入可能会导致死循环。任何人都可以提供有关此主题的见解或更多资源吗?
注意:我正在寻求深入的技术分析以及任何相关的经验或资源。感谢你的贡献!
例如,当我使用 时pip install numpy,我可以使用pip show numpy来获取 numpy 包的位置。
当我通过 conda 安装 numpy 时,例如conda install numpy,如何获取该包的位置?
给定一个降序列表,例如[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, -1, -2, -2]和threshold = 1.2,我想从原始列表中获取子列表,其中所有元素都大于threshold
方法一:
orgin_lst = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, -1, -2, -2]
lst = [i for i in orgin_lst if i > threshold]
Run Code Online (Sandbox Code Playgroud)
这是Pythonic方式,但我们不使用降序属性,并且当找到不大于阈值的元素时无法突破。如果满足的元素很少,但原始列表很大,则性能不好。
方法二:
orgin_lst = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, -1, -2, -2]
lst = []
for i …Run Code Online (Sandbox Code Playgroud) //Definition for a binary tree node.
public class TreeNode {
int key;
TreeNode left;
TreeNode right;
TreeNode(int x) { key = x; }
}
Run Code Online (Sandbox Code Playgroud)
给定TreeNode的总数 int n,如何生成随机分布的二叉树(我的意思是二叉树的形状随机,而不是随机键值。您可以将TreeNodes的所有键值设置为1)并返回TreeNode root。
这就是如何实现以下API:
public class RandomBinaryTree{
public TreeNode binaryTreeGenerator(int n){
}
}
Run Code Online (Sandbox Code Playgroud)
PS:例如,n = 3我希望算法每次可以随机生成以下5二进制树之一:
1 1 1 1 1
/ / / \ \ \
1 1 1 1 1 1
/ \ / \
1 1 1 1 …Run Code Online (Sandbox Code Playgroud) 我是 C 新手。我无法理解以下代码中的结果。int a[N]我使用 goto 并跳转array 和的声明int x。虽然x没有初始化10,但我仍然可以访问这些变量。
#include <stdlib.h>
#include <stdio.h>
#define N 4
void printArray(int a[], int length) {
for (int i = 0; i < length; i++) {
printf("%d, ", a[i]);
}
printf("\n");
}
int main(void) {
goto done;
int a[N];
int x=10;
printf("x=%d\n", x);
done:
for (int i = 0; i < N; i++) {
a[i] = i;
}
printArray(a, N);
printf("x=%d\n", x);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
结果
0, …Run Code Online (Sandbox Code Playgroud) H x W对于仅包含True和值的形状的给定图像掩模矩阵False,我希望将所有False值转换为True它们完全被值包围时的True值。
例如
mask = np.array([
[True, True, True, True, True ],
[True, False, True, False, True ],
[True, True, True, False, True ],
[True, False, False, False, True ],
[True, True, True, True, True ]
])
Run Code Online (Sandbox Code Playgroud)
结果应该是
np.array([
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True …Run Code Online (Sandbox Code Playgroud) PS:这个 HashSet 是如何产生排序输出的? 这篇文章没有回答我的问题。我知道如果我将任何数字放入哈希集中,我将不会得到排序顺序。
但是,我发现如果我将所有 [1, 2, 3, ..., n] 放入具有任何混洗顺序的 HashSet 中并迭代 HashSet,我将得到一个guranteed sorted order。我不明白为什么它总是会发生。我已经多次测试过任何 n < 10000 ,它总是正确的,因此这不应该是巧合,应该有一些原因!即使我不应该依赖这个实现细节,请告诉我为什么它总是发生。
PS:我知道如果我将 [0,1,2, ..., n-1] 或 [1+k, 2+k, .., n+k] (k != 0) 插入 HashSet,迭代顺序未排序,我已经测试过。HashSet 的迭代顺序未排序是正常的。但是,为什么 [1,2,3,4,..,n] 的任何插入顺序都意外地总是正确的?我已经检查了实现细节。如果我跟踪路径,整个过程将包括调整桶数组的大小,以及从链表到红黑树的转换。如果我以无序的顺序插入整个 [1-n],则 HashSet 的中间状态是未排序的。但是,如果我完成所有插入,它会意外地排序。
我使用 JDK 1.8 进行了以下测试。
public class Test {
public static void main(String[] args) throws IOException {
List<Integer> res = printUnsortedCase(10000);
System.out.println(res);
}
private static List<Integer> printUnsortedCase(int n){
List<Integer> res = new …Run Code Online (Sandbox Code Playgroud)