- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的算法构造一个词并在 TST 中查找与该词关联的值。
private Node get(Node x, String key, int index) {
if (key.isEmpty()) {
return root;
}
if (x == null) {
return null;
}
char c = key.charAt(index);
if (c < x.val) {
return get(x.left, key, index);
} else if (c > x.val) {
return get(x.right, key, index);
} else if (index < key.length() - 1) {
return get(x.mid, key, index + 1);
} else {
return x;
}
}
每个节点都是这样构造的:
private class Node {
private char val;
private Node left, mid, right;
private Double selfWeight;
private double maxWeight;
/**
* Node constructor.
*/
private Node(char c) {
val = c;
maxWeight = 0.0;
selfWeight = null;
}
}
单词的最大权重在构造时设置,这是 TST 标准构造的修改版本:
private Node put(Node x, String key, Double weight, int index) {
char c = key.charAt(index);
if (x == null) {
x = new Node();
x.val = c;
}
if (c < x.val) {
x.left = put(x.left, key, weight, index);
} else if (c > x.val) {
x.right = put(x.right, key, weight, index);
} else if (index < key.length() - 1) {
x.mid = put(x.mid, key, weight, index + 1);
} else {
x.selfWeight = weight;
}
if (weight > x.maxWeight) {
x.maxWeight = weight;
}
return x;
}
运行我的算法时,如果我插入,例如权重为 20 的“hello”,我在 get("hello"+ '\u0000');
上搜索该方法将返回 null,就像我调用 get("hello")
该方法将返回 20。这是为什么?
我的逻辑是添加 'null' 字符不会更改字符串,打印出 "hello"+ '\u0000'
证实了这一点。发生了什么事?
最佳答案
它们不是同一个字符串,因为它们不包含相同的字符。您看不到某个字符并不意味着它不存在。
如果你转换了hello
到 unicode 那么你所声称的是
0068 0065 006C 006C 006F 0000
与0068 0065 006C 006C 006F
相同
如果您需要进一步解释,请检查 String
的 equals 方法
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
关于java - 为什么 String + '\u0000' 与 String 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997529/
我想显示阿拉伯文字,但字符 المملك 显示。 例如,在 المملكة العربية السعودية 上显示单词 المملك٩ ا
我的应用程序正在 java + MSSQL 和 MySQL 上运行。下面的查询适用于 MSSQL,但不适用于 MySql。在 mysql 中,当我编写如下查询时,它给出了错误的结果 查询 select
根据 is_destructible 的定义( http://eel.is/c++draft/meta.unary.prop#lib:is_destructible ), is_destructib
我有一个数据库表,其中有一列我对波斯字母进行了分类,以便稍后使用 MySQL WHERE 选择。一切都适用于所有字母,但我在选择在数据库中存储为 (Ù†) 的字母 (?) 和存储为 (Ú†) 的 (?
我知道像这样的可选链接: someOptional?.someProperty 基本上是 someOptional.map { $0.someProperty } 但是,我发现同时做这两件事是不可能的
u-boot配置文件中的以下配置我看不懂 CONFIG_SYS_EXTRA_OPTIONS="SYS_SDRAM_SIZE=0x20000000" 好像在 u-boot 代码中设置 DRAM 大小。但
我对以下两个方法声明感到困惑: private T funWorks(T child, U parent) { // No compilation errors }
给定如下 API: class Bar { ... } class Foo extends Bar { ... } 在 Java 的 Optional 类型中,我们可以说: Optional fooO
我有一个文件,每行有两个字符: $ cat roman Ⅱ Ⅲ nut 当我用 sort -u 对这个文件进行排序时,只显示一行: $ sort -u roman Ⅱ Ⅱ是代码点U+2161,Ⅲ是代码
我正在尝试将 C 数组分配给 C++ std::array。 我该如何做到这一点,最干净的方式并且不制作不需要的拷贝等? 什么时候做 int X[8]; std::array Y = X; 我得到一个
我有以下案例类: case class [Q Length[T] 但是,我收到一条错误消息,说 需要三个参数,而我只给出了两个。我希望它像这样工作: type Area[T] = [Length[T
它是 well documented那个[T; n]可以强制到[T] .下面的代码也是well-formed : fn test(){ let _a: &[i32] = &[1, 2, 3];
我正在尝试使用 tweepy 在 Tkinter 窗口上显示我的 Twitter 时间线。这是代码 import tweepy import tkinter consumer_key = 'xxxxx
我正在使用以下包含 letter ü 的文本片段: test für fur test 代码如下: import re for m in re.finditer(r, line, re.IGNOREC
我对 USQL 很陌生,想知道如何在 select 语句中将“日期时间”转换为“日期”。另外,我如何摆脱毫秒和上午/下午?我真的很感激这方面的任何帮助。谢谢你们。 最佳答案 下面是有效的代码。注意括号
在 U-SQL 自定义代码(代码隐藏或程序集)中可以调用外部服务,例如bing搜索或 map 。 谢谢, 纳西尔 最佳答案 由于以下原因,目前不支持此功能: 想象一下,您编写了一个 UDF 或 UDO
我想 ping 出多个以太网端口。 u-boot 仅支持单个以太网端口是否存在固有限制? 最佳答案 Can u-boot support more than one ethernet port? 是的
我最近开始学习Prolog,但无法解决如何将三个列表合并的问题。 我能够合并两个列表: %element element(X,[X|_]). element(X,[_|Y]):-
我们使用 Beaglebone 黑色定制板。我编辑了一个链接器脚本文件以添加内存部分以在其中记录一些信息: . = ALIGN(4); .logging : { _log_begin
我们使用 Beaglebone 黑色定制板。我编辑了一个链接器脚本文件以添加内存部分以在其中记录一些信息: . = ALIGN(4); .logging : { _log_begin
我是一名优秀的程序员,十分优秀!