字典顺序-两个char数组作为参数,寻找更好的解决方案
Lexicographical order - two char array as parameters, looking for a better solution
我必须编写一个需要 2 char[]
s 和 returns:
的函数
-1
如果第一个单词按字典顺序排在第二个单词之前
0
如果它们是同一个词
1
如果在 之后
我知道 compareTo()
方法,但这是一项作业,我需要避免使用它。到目前为止,我的代码运行良好,我用不同的词做了一些测试。
我想知道是否有另一种方法可以做到这一点,我的代码感觉没有优化,它又长又重复:
public static int lexico(char[] word1, char[] word2) {
int length1 = word1.length;
int length2 = word2.length;
if (length1 == length2) {
for (int i = 0; i < length1; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
return 0;
}
}
}
if (length1 < length2) {
for (int i = 0; i < length1; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
// If I'm here then it means that all of the characters
// from 0 to length1-1 are equals
// but since length of the first string is shorter than the second,
// the first string will be put before the second
return -1;
}
}
}
if (length1 > length2) {
for (int i = 0; i < length2; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
return 1;
}
}
}
return -999;
}
public static void main(String[] args) {
char[] share = { 's', 'h', 'a', 'r', 'e' };
char[] ship = { 's', 'h', 'i', 'p' };
System.out.println(lexico(share, ship)); // -1 share is before ship
System.out.println(lexico(ship, share)); // 1 ship is after share
System.out.println(lexico(ship, ship)); // 0 same word
}
给您的几点说明:
你只需要一个循环:从开始到两个长度中较低的一个。如果数组相同直到两个长度中的较小者并且它们的长度不同,你的分配应该告诉你什么 return (通常它会是 -1 如果左边的数组比右边的短,1否则)。
a < b
不是两个字符的有效字母比较(大多数程序员说 "lexicographic" 时的意思,"lexico" 意思是 "pertaining to words"), 这是一个 numeric 比较。现在,String
的 compareTo
claims to use "lexicographic ordering," but it really just uses numeric ordering, so that may be good enough for what you're doing. If you want alphabetic ordering, I can't think of a JDK comparison method that accepts two single chars to compare rather than strings. There may be one I don't know of, or you may have to create one-character strings to do the comparison (with a Collator
) 将(例如)正确识别 "voilà" 中的 à
应该在其中任何其他字母之前。
好吧,我放弃并写了它...没有单元测试(总是交付代码单元测试)但我认为这应该给你一个很好的功能工作想法(有像不同长度等边缘情况等。我还没有处理过,但你的想法是对的吗?)...干杯并玩得开心:=)...我想使用流可以更有效地完成...
public static void main(String[] args) {
List<Character> firstCharArr = new ArrayList<>(Arrays.asList('a', 'c'));
List<Character> secondCharArr = new ArrayList<>(Arrays.asList('x', 'c'));
BiFunction<Character, Character, Integer> charCompareFunction = new BiFunction<Character, Character, Integer>() {
@Override
public Integer apply(Character character, Character character2) {
if (character.charValue() < character2.charValue()) {
return -1;
} else if (character.charValue() == character2.charValue()) {
return 0;
} else {
return 1;
}
}
};
int i = 0;
for (Character firstLineChar : firstCharArr) {
if (charCompareFunction.apply(firstLineChar, secondCharArr.get(i++)) == 0) {
continue;
} else {
if (charCompareFunction.apply(firstLineChar, secondCharArr.get(i++)) < 0) {
System.out.println("FirstLine is Smaller");
break;
} else {
System.out.println("FirstLine is Larger");
break;
}
}
}
}
其实可以利用上面的方法,证明Java的字符字典序:
字母前的数字
大写先于小写
我必须编写一个需要 2 char[]
s 和 returns:
-1
如果第一个单词按字典顺序排在第二个单词之前0
如果它们是同一个词1
如果在 之后
我知道 compareTo()
方法,但这是一项作业,我需要避免使用它。到目前为止,我的代码运行良好,我用不同的词做了一些测试。
我想知道是否有另一种方法可以做到这一点,我的代码感觉没有优化,它又长又重复:
public static int lexico(char[] word1, char[] word2) {
int length1 = word1.length;
int length2 = word2.length;
if (length1 == length2) {
for (int i = 0; i < length1; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
return 0;
}
}
}
if (length1 < length2) {
for (int i = 0; i < length1; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
// If I'm here then it means that all of the characters
// from 0 to length1-1 are equals
// but since length of the first string is shorter than the second,
// the first string will be put before the second
return -1;
}
}
}
if (length1 > length2) {
for (int i = 0; i < length2; i++) {
if (word1[i] < word2[i]) {
return -1;
} else if (word1[i] > word2[i]) {
return 1;
} else if (i == length1 - 1) {
return 1;
}
}
}
return -999;
}
public static void main(String[] args) {
char[] share = { 's', 'h', 'a', 'r', 'e' };
char[] ship = { 's', 'h', 'i', 'p' };
System.out.println(lexico(share, ship)); // -1 share is before ship
System.out.println(lexico(ship, share)); // 1 ship is after share
System.out.println(lexico(ship, ship)); // 0 same word
}
给您的几点说明:
你只需要一个循环:从开始到两个长度中较低的一个。如果数组相同直到两个长度中的较小者并且它们的长度不同,你的分配应该告诉你什么 return (通常它会是 -1 如果左边的数组比右边的短,1否则)。
a < b
不是两个字符的有效字母比较(大多数程序员说 "lexicographic" 时的意思,"lexico" 意思是 "pertaining to words"), 这是一个 numeric 比较。现在,String
的compareTo
claims to use "lexicographic ordering," but it really just uses numeric ordering, so that may be good enough for what you're doing. If you want alphabetic ordering, I can't think of a JDK comparison method that accepts two single chars to compare rather than strings. There may be one I don't know of, or you may have to create one-character strings to do the comparison (with aCollator
) 将(例如)正确识别 "voilà" 中的à
应该在其中任何其他字母之前。
好吧,我放弃并写了它...没有单元测试(总是交付代码单元测试)但我认为这应该给你一个很好的功能工作想法(有像不同长度等边缘情况等。我还没有处理过,但你的想法是对的吗?)...干杯并玩得开心:=)...我想使用流可以更有效地完成...
public static void main(String[] args) {
List<Character> firstCharArr = new ArrayList<>(Arrays.asList('a', 'c'));
List<Character> secondCharArr = new ArrayList<>(Arrays.asList('x', 'c'));
BiFunction<Character, Character, Integer> charCompareFunction = new BiFunction<Character, Character, Integer>() {
@Override
public Integer apply(Character character, Character character2) {
if (character.charValue() < character2.charValue()) {
return -1;
} else if (character.charValue() == character2.charValue()) {
return 0;
} else {
return 1;
}
}
};
int i = 0;
for (Character firstLineChar : firstCharArr) {
if (charCompareFunction.apply(firstLineChar, secondCharArr.get(i++)) == 0) {
continue;
} else {
if (charCompareFunction.apply(firstLineChar, secondCharArr.get(i++)) < 0) {
System.out.println("FirstLine is Smaller");
break;
} else {
System.out.println("FirstLine is Larger");
break;
}
}
}
}
其实可以利用上面的方法,证明Java的字符字典序: 字母前的数字 大写先于小写