- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图解决 Project Euler 中的问题。我知道我的方法符合逻辑(它几乎立即返回小规模问题的答案)。然而,它的规模非常可怕。我已经尝试更改 .ini 文件,但无济于事。
这是我的代码:
public class Number28 {
static int SIZE = 101; //this should be an odd number, i accidentally posted 100
/**
* @param args
*/
public static void main(String[] args) {
double start = System.currentTimeMillis();
long spiral[][]= spiral(SIZE);
long sum = 0;
for(int i = 0; i < SIZE; i++)
{
sum += spiral[i][i];
sum += spiral[i][SIZE - 1 - i];
}
System.out.println(sum - 1);
double time = System.currentTimeMillis() - start;
System.out.println(time);
}
public static long[][] spiral(int size){
long spiral[][]= new long[size][size];
if(size == 1){
spiral[0][0] = 1;
return spiral;
}
else{
long subspiral[][]= new long[size - 2][size - 2];
subspiral = spiral(size - 2);
for(int r = 0; r < size - 2; r++){
for(int c = 0; c < size - 2; c++){
spiral[r + 1][c + 1] = subspiral[r][c];
}
}
long counter = subspiral[0][size - 3];
for(int r = 1; r < size ; r++){
counter++;
spiral[r][size - 1] = counter;
}
for(int c = size - 2; c >= 0; c--){
counter++;
spiral[size - 1][c] = counter;
}
for(int r = size - 2 ; r >= 0 ; r--){
counter++;
spiral[r][0] = counter;
}
for(int c = 1; c < size ; c++){
counter++;
spiral[0][c] = counter;
}
return spiral;
}
}
}
这是经过编辑的代码,工作起来就像一个 gem :
public class Number28 {
static int SIZE = 1001;
static long spiral[][]= new long[SIZE][SIZE];
/**
* @param args
*/
public static void main(String[] args) {
double start = System.currentTimeMillis();
long spiral[][]= spiral(SIZE);
long sum = 0;
for(int i = 0; i < SIZE; i++)
{
sum += spiral[i][i];
sum += spiral[i][SIZE - 1 - i];
}
System.out.println(sum - 1);
double time = System.currentTimeMillis() - start;
System.out.println(time);
}
public static long[][] spiral(int size){
if(size == 1){
spiral[SIZE / 2][SIZE / 2] = 1;
return spiral;
}
else{
long subspiral[][]= spiral(size - 2);
int edge = (SIZE - size) / 2;
long counter = subspiral[edge + 1][edge + size - 2];
for(int r = 1; r < size ; r++){
counter++;
spiral[edge + r][edge + size - 1] = counter;
}
for(int c = size - 2; c >= 0; c--){
counter++;
spiral[edge + size - 1][edge + c] = counter;
}
for(int r = size - 2 ; r >= 0 ; r--){
counter++;
spiral[edge + r][edge] = counter;
}
for(int c = 1; c < size ; c++){
counter++;
spiral[edge][edge + c] = counter;
}
return spiral;
}
}
}
最佳答案
作为 Project Euler 的一般建议,如果您的解决方案无法扩展,几乎可以肯定有更好的方法来解决它。如果您在较早的问题上使用了相同类型的解决方案,您可以浏览其他用户关于较早的问题的帖子,以获取以不同方式解决问题的想法。
关于java - 克服堆溢出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/883966/
据我所知,Marketo 将 REST API 请求的数量限制为每天 10,000 次。有没有办法克服这个限制?我可以支付并获得更多吗? 我发现 REST API 请求和 SOAP API 请求是分开
有谁知道有什么方法可以克服 NotSupportedException?我有一个针对用户的方法: public virtual bool IsAbove(User otherUser) {
我们最近从关系型 (MySQL) 转向 NoSQL (couchbase)。基本上它是社交手机游戏的后端。我们在扩展后端以处理越来越多的用户时面临着很多问题。当使用 MySQL 时,由于多个表之间存在
我当前用来收集文本的函数 InputBox 显然不能接受超过 255 个字符,并且我需要能够收集更多字符?我可以使用参数或不同的函数来增加此限制吗? 最佳答案 说句迂腐的话,输入框允许您输入最多 25
我是近似字符串匹配领域的新手。 我正在探索 Bitap algorithm 的用途,但到目前为止,它有限的模式长度让我很困扰。我正在使用 Flash,处理 32 位无符号整数和一个 IEEE-754
克服 PHP 中的大海捞针困惑的最实用方法是什么? 这里 $needle 是第一个参数 bool in_array ( mixed $needle , array $haystack [, bool
我有一个无法更改的客户端类。 List list1= Arrays.asList(1,2,3); System.out.println("Total sum is:"+sum(list1)); pri
我们都知道,在 Java 中,如果在 Abstract 类中声明一个 static 方法,该方法将属于 Abstract 类,而不是它的 child 。 (没有抽象静态) 我有一个简单的数据库系统,它
我有 4 个表和这个查询 SELECT tag.id, title FROM tag LEFT JOIN tag_map ON ( tag.id = tag_map.tag_id ) LEFT JOI
我有 4 个表和这个查询 SELECT tag.id, title FROM tag LEFT JOIN tag_map ON ( tag.id = tag_map.tag_id ) LEFT JOI
在 Swift 中,一切都以结构而不是类的形式实现。正在传入值,但内存地址不存储它们。这是一个例子: import UIKit var array = Array() array = [6,2,9,1
在我编写的 C++ 程序中: #include #include using namespace std; int main() { vector a; a.resize(1); f
我正在尝试使用 php_ldap(使用 ADLdap)从 Ubuntu(hardy 8.04LTS)主机绑定(bind)到 AD。在端口 389 上绑定(bind)到 ldap://myserver
这个问题在这里已经有了答案: Low picture/image quality when capture from camera (3 个答案) 关闭 5 年前。 我想从相机拍照并上传到服务器,但
我想将 unsigned char[8] 分配给 uint64(c 语言),使用 RPC 传递此值,并将 uint64 转换回具有相同字节顺序的 unsigned char[8](cpp 语言)。问题
Python 似乎对实例方法有一些限制。 无法复制实例方法。 不能 pickle 实例方法。 这对我来说是有问题的,因为我的工作是非常面向对象的 project我在其中引用了实例方法,并且同时使用了深
我理解这个问题,根据答案 here ,但是,我真的可以使用帮助或更详细的代码解释来说明它是如何克服的。 我的情况是这样的:我曾经将模型和 Controller 分开,在我的模型包中我有一个包含所有模型
假设我从这样的 Akka Persistence 系统开始: case class MyMessage(x: Int) class MyProcessor extends Processor {
我正在寻找在构建重量级 Windows 界面时处理用户对象句柄限制的高级策略。请说明您如何使用 SWT 或直接 Windows GUI API 克服或绕过此问题。我唯一不感兴趣的是优化小部件使用的策略
我有一个我知道过去的日期列表,但格式为 28/MAY/13 .用它们制作日期类的最接近的方法是基本的 dates as.Date("2013-12-31")] as.Date("2013-12-3
我是一名优秀的程序员,十分优秀!