我最近在面试期间进行了编码测试。有人告诉我:
有一个一百万个的大型未排序数组
int
。用户想要检索K
最大的元素。你会实现什么算法?
在此期间,我强烈暗示我需要对数组进行排序。
因此,如果性能确实很重要,我建议使用内置sort()
或自定义实现。然后我被告知,使用Collection
or数组来存储k
最大的元素和 for 循环可以实现大约O(N)
,事后看来,我认为这是O(N*k)
因为每次迭代都需要与大小数组进行比较K
以找到要替换的最小元素,而需要对数组进行排序将导致代码至少为O(N log N)
.
然后我回顾了 SO 上的这个链接,它建议K
数字的优先级队列,每次找到较大的元素时删除最小的数字,这也会给出O(N log N)
. 编写一个程序,从 10 亿个数字的数组中找出 100 个最大的数字
for循环方法不好吗?我应该如何证明使用 for 循环或优先级队列/排序方法的优点/缺点?我认为,如果数组已经排序,则不需要再次迭代整个数组,即如果对排序数组调用其他检索方法,则它应该是恒定时间。运行实际代码时是否存在一些我在理论化伪代码时没有考虑到的性能因素?
我目前正在关注Spring 文档和一些有关 Web 安全的教程。但现在我有一个问题,我无法调用该方法antMatchers
。这是我在构建项目时遇到的错误:
java: cannot find symbol
symbol: method antMatchers(java.lang.String)
location: variable requests of type org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer<org.springframework.security.config.annotation.web.builders.HttpSecurity>.AuthorizationManagerRequestMatcherRegistry
Run Code Online (Sandbox Code Playgroud)
根据我的理解,我应该能够使用这个方法,这样我就可以允许或不允许对某个URL的HTTP请求。所以我的问题是,为什么我不能使用antMatchers()
方法?
SecurityConfiguration
班级:
java: cannot find symbol
symbol: method antMatchers(java.lang.String)
location: variable requests of type org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer<org.springframework.security.config.annotation.web.builders.HttpSecurity>.AuthorizationManagerRequestMatcherRegistry
Run Code Online (Sandbox Code Playgroud)
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.gabriel</groupId>
<artifactId>vertretungsplan</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vertretungsplan</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency> …
Run Code Online (Sandbox Code Playgroud) 简而言之,Java 17 中的最终类和记录有什么区别?
什么情况下应该使用记录?
在 JavaList<char[]>
中将a 转换/扁平化 的简单方法是什么?char[]
我知道我可以通过迭代List
和 using来做到这一点System.arraycopy
,但我想知道是否有更简单的方法来使用 Java 8 流来做到这一点?
也许是这样的,但不必将原语装箱char
为Character
:
List<char[]> listOfCharArrays = ...
Character[] charArray =
Stream.of(listOfCharArrays )
.flatMap(List::stream)
.toArray(Character[]::new);
Run Code Online (Sandbox Code Playgroud) 给定一个字符串列表:
ArrayList<String> strList = new ArrayList<String>();
strList.add("Mary had a little lamb named Willy");
strList.add("Mary had a little ham");
strList.add("Old McDonald had a farm named Willy");
strList.add("Willy had a little dog named ham");
strList.add("(abc)");
strList.add("(xyz)");
strList.add("Visit Target Store");
strList.add("Visit Walmart Store");
Run Code Online (Sandbox Code Playgroud)
HashMap<String, Integer>
prefixMap
这应该产生 a和形式的输出suffixMap
:
字首:
Mary had a -> 2
Mary had a little -> 2
( -> 2
Visit -> 2
Run Code Online (Sandbox Code Playgroud)
后缀:
named Willy -> 2
ham -> 2
) -> 2
Store -> …
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个List
实现特定接口的类Interface
:
List<Class<? extends Interface>> myList= myMap.entrySet().stream()
.filter(entry -> entry.getValue().equals(myValue))
.map(Map.Entry::getKey) // Stream<Interface>
.map(Interface::getClass) // Stream<Class<capture of ? extends Interface>>
.distinct()
.toList();
Run Code Online (Sandbox Code Playgroud)
map()
我添加了调用后 Stream 中元素的类型作为注释。
该代码迭代映射中的所有条目,如果它们的值等于myValue
,则:
Interface
(这是条目的键)Class
实施Interface
。myMap
定义为:
Map<Interface, Integer> myMap = new HashMap<>()
Run Code Online (Sandbox Code Playgroud)
我收到的错误:
Incompatible types.
Found: 'java.util.List<java.lang.Class<capture<? extends application.interfaces.Interface>>>',
required: 'java.util.List<java.lang.Class<? extends application.interfaces.Interface>>'
Run Code Online (Sandbox Code Playgroud)
我显然错过了关于泛型在 Java 中如何工作的一些内容,但我在这里不知所措。我想这与编译器无法正确具体化我的通配符这一事实有关?
。
我正在创建一种方法,该方法采用 2D 数组并扫描整个数组以查找完全被零包围的数字“块”,并将这些块(我称之为岛)转换为零。
我正在尝试删除除最大的“岛屿”之外的所有“岛屿”。
例如,对于这个二维数组
1 2 3 2 2 1
3 2 2 1 2 3
3 2 2 1 3 2
2 3 2 3 2 2
2 2 3 1 1 2
3 2 1 2 3 2
2 3 1 2 3 2
2 2 0 0 0 0
0 0 0 1 2 0
0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
在该方法之后,二维数组现在应该是:
1 2 3 2 2 1
3 2 2 1 2 3
3 2 …
Run Code Online (Sandbox Code Playgroud) 我有一个点列表,其中每个点都是一个很小的size列表2
。我想按 的升序对点列表进行排序x
,如果x
值相等,我通过按 的降序排序来打破平局y
。
我编写了一个自定义比较器来对点进行排序,如下所示:
Collections.sort(points, (a, b) -> {
if (a.get(0) != b.get(0)) {
return a.get(0) - b.get(0);
} return b.get(1) - a.get(1);
});
Run Code Online (Sandbox Code Playgroud)
这是排序之前的输入:
(2, 1000)
(9, -1000)
(3, 15)
(9, -15)
(5, 12)
(12, -12)
(5, 10)
(10001, -10)
(19, 8)
(10001, -8)
Run Code Online (Sandbox Code Playgroud)
这是使用上述比较器排序后产生的结果:
(2, 1000)
(3, 15)
(5, 12)
(5, 10)
(9, -15)
(9, -1000)
(12, -12)
(19, 8)
(10001, -10) …
Run Code Online (Sandbox Code Playgroud) 有没有办法过滤掉所有大于可以存储在Long
使用 Stream API 中的最大值的值?
目前的情况是,您可以通过一些客户的 ID 在前端通过简单的搜索栏进行搜索。
例如:123456789, 10987654321.
如果您在这两个 ID 之间放置一个“分隔符”,则一切正常。但是,如果您忘记了“分隔符”,我的代码将尝试解析12345678910987654321
为 Long,我想就存在问题了。
这会导致NumberFormatException
尝试搜索后。Long
有没有办法过滤掉这些因太大而无法解析为 a 的数字?
String hyphen = "-";
String[] customerIds = bulkCustomerIdProperty.getValue()
.replaceAll("[^0-9]", hyphen)
.split(hyphen);
...
customerFilter.setCustomerIds(Arrays.asList(customerIds).stream()
.filter(n -> !n.isEmpty())
.map(n -> Long.valueOf(n)) // convert to Long
.collect(Collectors.toSet()));
Run Code Online (Sandbox Code Playgroud) 如何使用 Java Stream API 编写以下方法?
public Map<String, String> getMap() {
Map<String, String> mapB = new HashMap<>();
for (String parameterKey : listOfKeys) {
String parameterValue = mapA.get(parameterKey);
mapB.put(parameterKey, Objects.requireNonNullElse(parameterValue, ""));
}
return ImmutableMap.copyOf(mapB);
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
return listOfKeys.stream()
.map(firstMap::get)
.collect( ? )
Run Code Online (Sandbox Code Playgroud)
但我不知道如何从这里继续。
java ×10
java-stream ×4
algorithm ×3
arrays ×3
sorting ×2
char ×1
collectors ×1
comparator ×1
exception ×1
final-class ×1
for-loop ×1
generics ×1
guava ×1
hashmap ×1
java-17 ×1
list ×1
math ×1
record ×1
spring ×1
spring-boot ×1
spring-mvc ×1
string ×1