遍历一个大数组
Traversing a large array
数组有元素(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,2,2,1],它有 2 个不同的元素,它满足整个数组有 2 个不同的元素。
que 的问题是我得到了 array size of 2 lacs 的测试用例,我必须在 1 秒内得到输出,每次我得到的时间限制都超过了发生。
看到答案后,我必须使用缓冲区 reader(而不是扫描器{由于性能问题})和 hashmap。
首先,我使用了扫描仪和缓冲区 reader,两者都在 2 分 17 秒内给出了输出(对于 2 个 lac 输入)。那么为什么需要使用缓冲区reader(使用扫描器减少了代码的长度)。
其次,我在网站上测试了两个代码,两个代码都在 1 秒内给出了输出,而在我的本地机器上则需要 2 分 17 秒。我不明白为什么会有这么大的差异
三、这段代码是什么意思:
最终整数 mod = (int)1e9+7;
(虽然我见过很多次用大数)
第四,下面的代码与缓冲reader一起使用有什么用。
我是java的新手,所以请简单回答,抱歉这么长post
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();
}
}
第四个查询的答案:该代码比使用普通扫描仪更快 class。
因此,您可以在编码竞赛中使用它。
我在 ~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
希望对您有所帮助:)
数组有元素(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,2,2,1],它有 2 个不同的元素,它满足整个数组有 2 个不同的元素。
que 的问题是我得到了 array size of 2 lacs 的测试用例,我必须在 1 秒内得到输出,每次我得到的时间限制都超过了发生。
看到答案后,我必须使用缓冲区 reader(而不是扫描器{由于性能问题})和 hashmap。
首先,我使用了扫描仪和缓冲区 reader,两者都在 2 分 17 秒内给出了输出(对于 2 个 lac 输入)。那么为什么需要使用缓冲区reader(使用扫描器减少了代码的长度)。
其次,我在网站上测试了两个代码,两个代码都在 1 秒内给出了输出,而在我的本地机器上则需要 2 分 17 秒。我不明白为什么会有这么大的差异
三、这段代码是什么意思: 最终整数 mod = (int)1e9+7; (虽然我见过很多次用大数)
第四,下面的代码与缓冲reader一起使用有什么用。
我是java的新手,所以请简单回答,抱歉这么长post
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();
}
}
第四个查询的答案:该代码比使用普通扫描仪更快 class。 因此,您可以在编码竞赛中使用它。 我在 ~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
希望对您有所帮助:)