gpt4 book ai didi

java - Scala 在非常长的字符串上调用 length() 会引发异常 : Maximum String literal length exceeded

转载 作者:搜寻专家 更新时间:2023-11-01 02:36:53 30 4
gpt4 key购买 nike

我为 HackerRank BigSorting 写了一个 Scala 解决方案,并在某些测试用例中得到 RuntimeException。我测试了 scastie 中的输入, 似乎问题是由在一个很长的字符串上调用 length() 引起的,但同样是 java solution没有问题,这是一个错误吗?我的解决方案代码是:

object Solution {
def stringSort(x:String, y:String):Boolean = {
var result = true

if(x.length < y.length){
result = true
}else if(x.length > y.length){
result = false
}else{
var founded = false
var i = 0
while(i < x.length && !founded){
var xi = x.charAt(i)
var yi = y.charAt(i)
if(xi != yi){
result = xi < yi
founded = true
}
i += 1
}
}
result
}

def bigSort(unsorted:Array[String]):Array[String] = {
return unsorted.sortWith(stringSort)
}

def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
var unsorted = new Array[String](n);
for(unsorted_i <- 0 to n-1) {
unsorted(unsorted_i) = sc.next();
}
print(bigSort(unsorted).mkString("\n"))
// your code goes here
}
}

异常(exception)是

java.lang.IllegalArgumentException: Maximum String literal length exceeded
at scala.tools.asm.ByteVector.putUTF8(ByteVector.java:213)
at scala.tools.asm.ClassWriter.newUTF8(ClassWriter.java:1114)
at scala.tools.asm.ClassWriter.newString(ClassWriter.java:1582)
at scala.tools.asm.ClassWriter.newConstItem(ClassWriter.java:1064)
at scala.tools.asm.MethodWriter.visitLdcInsn(MethodWriter.java:1187)
at scala.tools.asm.tree.LdcInsnNode.accept(LdcInsnNode.java:71)
at scala.tools.asm.tree.InsnList.accept(InsnList.java:162)
at scala.tools.asm.tree.MethodNode.accept(MethodNode.java:820)
at scala.tools.asm.tree.MethodNode.accept(MethodNode.java:730)
at scala.tools.asm.tree.ClassNode.accept(ClassNode.java:412)
...

Java 解决方案

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] unsorted = new String[n];
for(int i = 0; i < n; i++) unsorted[i] = in.next();

Arrays.sort(unsorted,new Comparator<String>() {
@Override
public int compare(String a, String b)
{
return StringAsIntegerCompare(a,b);
}
});

StringBuilder output = new StringBuilder("");
for(String num : unsorted)
output.append(num+"\n");
System.out.println(output);
}

static int StringAsIntegerCompare(String s1, String s2)
{
if(s1.length() > s2.length()) return 1;
if(s1.length() < s2.length()) return -1;
for(int i = 0; i < s1.length(); i++)
{
if((int)s1.charAt(i) > (int)s2.charAt(i)) return 1;
if((int)s1.charAt(i) < (int)s2.charAt(i)) return -1;
}
return 0;
}
}

最佳答案

我能看到的 Java 和 Scala 代码之间唯一的实质性区别是 Scala sortWith 创建一个新数组,这与 Arrays.sort 不同(并且可能有一些中间开销太),所以你可能会耗尽内存。你可以试试

import scala.math.Ordering
import scala.util.Sorting

val byLength: Ordering[String] = ... // your implementation, similar to Java comparator

Sorting.quickSort(unordered)(byLength) // instead of sortWith, sorts in-place

参见 http://www.scala-lang.org/api/2.12.0/scala/math/Ordering.htmlhttp://www.scala-lang.org/api/2.12.0/scala/util/Sorting $.html 用于相关文档。

关于java - Scala 在非常长的字符串上调用 length() 会引发异常 : Maximum String literal length exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300461/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com