【问题标题】:Traversing a large array遍历一个大数组
【发布时间】:2017-01-21 19:43:25
【问题描述】:

一个数组有元素(1,2,2,1,1)。我必须找到子数组中不同元素的数量应该等于给定数组中不同元素的数量,即数组中 distinct 元素的数量是 2(1 和 2)。

所有可能的子数组是 [1,2] , [1,3] , [1,4] , [1,5] , [2,4] , [2,5] , [3,4] , [3,5]

Distinct 表示没有不同的元素,它 2 {1,2,2} 有 2 个不同的元素。 在这个问题中,1,4 并不意味着我们包含第 1 个元素和第 4 个元素,这意味着我们的子数组从 1 开始并以 4 结束 因此子数组是 [1,2,2,1],它有 2 个不同的元素,它满足整个数组有 2 个不同的元素。

问题的问题是我得到了 2 lacs 的 array size 的测试用例,我必须在 1 秒内得到输出,并且每次超出时间限制时都会发生。

看到答案后,我必须使用缓冲区阅读器(而不是扫描仪 {由于性能问题})和 hashmap。

首先,我使用了扫描仪和缓冲区阅读器,两者都在 2 分 17 秒内给出了输出(对于 2 lac 输入)。那么为什么需要使用缓冲区阅读器(使用扫描仪减少了代码的长度)。

其次,我在网站上测试了两个代码,两个代码都在 1 秒内给出了输出,而在我的本地机器上花了 2 分 17 秒。我不明白为什么会有这么大的区别。

三、这段代码是什么意思: 最终 int mod = (int)1e9+7; (虽然我在使用大数时见过很多次)

第四,下面的代码与缓冲阅读器一起使用有什么用。

我是java新手,所以请给出简单的答案,很抱歉很长的帖子

class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;

public InputReader(InputStream stream)
{
    this.stream = stream;
}

public int read()
{
    if (numChars == -1)
        throw new InputMismatchException();
    if (curChar >= numChars)
    {
        curChar = 0;
        try
        {
            numChars = stream.read(buf);
        } catch (IOException e)
        {
            throw new InputMismatchException();
        }
        if (numChars <= 0)
            return -1;
    }
    return buf[curChar++];
}

public int readInt()
{
    int c = read();
    while (isSpaceChar(c))
        c = read();
    int sgn = 1;
    if (c == '-')
    {
        sgn = -1;
        c = read();
    }
    int res = 0;
    do
    {
        if (c < '0' || c > '9')
            throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
    } while (!isSpaceChar(c));
    return res * sgn;
}

public String readString()
{
    int c = read();
    while (isSpaceChar(c))
        c = read();
    StringBuilder res = new StringBuilder();
    do
    {
        res.appendCodePoint(c);
        c = read();
    } while (!isSpaceChar(c));
    return res.toString();
}
public double readDouble() {
    int c = read();
    while (isSpaceChar(c))
        c = read();
    int sgn = 1;
    if (c == '-') {
        sgn = -1;
        c = read();
    }
    double res = 0;
    while (!isSpaceChar(c) && c != '.') {
        if (c == 'e' || c == 'E')
            return res * Math.pow(10, readInt());
        if (c < '0' || c > '9')
            throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
    }
    if (c == '.') {
        c = read();
        double m = 1;
        while (!isSpaceChar(c)) {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, readInt());
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            m /= 10;
            res += (c - '0') * m;
            c = read();
        }
    }
    return res * sgn;
}
public long readLong() {
    int c = read();
    while (isSpaceChar(c))
        c = read();
    int sgn = 1;
    if (c == '-') {
        sgn = -1;
        c = read();
    }
    long res = 0;
    do {
        if (c < '0' || c > '9')
            throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
    } while (!isSpaceChar(c));
    return res * sgn;
}
public boolean isSpaceChar(int c)
{
    if (filter != null)
        return filter.isSpaceChar(c);
    return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}

public String next()
{
    return readString();
}

public interface SpaceCharFilter
{
    public boolean isSpaceChar(int ch);
}
}

class OutputWriter
{
private  PrintWriter writer;

public OutputWriter(OutputStream outputStream)
{
    writer = new PrintWriter(new BufferedWriter(new      OutputStreamWriter(outputStream)));
}

public OutputWriter(Writer writer)
{
    this.writer = new PrintWriter(writer);
}

public void print(Object... objects)
 {
    for (int i = 0; i < objects.length; i++)
    {
        if (i != 0)
            writer.print(' ');
        writer.print(objects[i]);
    }
 }

public void printLine(Object... objects)
{
    print(objects);
    writer.println();
}

public void close()
{
    writer.close();
}

public void flush()
{
    writer.flush();
}
}

【问题讨论】:

  • 我看到了为这么小的任务编写大量代码的方法。
  • 使用扫描仪将删除整个代码,虽然我不明白代码的使用,这也是我第三次看到这个代码,这就是为什么我必须发布这个代码,每当我看到这个代码它用于处理大量数字。

标签: java arrays large-data


【解决方案1】:

回答您的第四个查询:该代码比使用普通的 Scanner 类更快。 因此,您可以在编码比赛中使用它。 我在约 55 MB 的文本文件“test.txt”上使用了以下代码。

package so;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class UseIR {
public static void main(String[] args) throws IOException {

//checked using the class provided 'InputReader'
InputReader ob=new InputReader(new FileInputStream(new File("src\\so\\test.txt")));
long str=0;
StringBuilder sb=new StringBuilder();
long curTime=System.currentTimeMillis();
while((str=ob.read())!=-1){
    sb.append(((char)str));
}
System.out.println("done "+(System.currentTimeMillis()-curTime));

//checked using the Scanner class

curTime=System.currentTimeMillis();
Scanner s=new Scanner(new File("src\\so\\test.txt"));
sb=new StringBuilder();
while(s.hasNext()){
    sb.append(s.next());
}
System.out.println("done "+(System.currentTimeMillis()-curTime));
}
}

输出如下:

done 447
done 2061

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多