Java 查找arraylist中的最大序列
一些背景 上周我在课本上做了一道题,它告诉我生成20个随机数,然后在相等的连续数周围放上括号 考虑下面我的程序输出Java 查找arraylist中的最大序列,java,arraylist,Java,Arraylist,一些背景 上周我在课本上做了一道题,它告诉我生成20个随机数,然后在相等的连续数周围放上括号 考虑下面我的程序输出 697342(33)(666)(44)69(66)1(88) 我需要做什么 下一个问题是基本上获得这些单词的最长序列,并在它们周围放上括号。如果你有 1122345(6666) 基本上,你需要在四个6的周围放上括号,因为它们经常出现。 我已经完成了我正在学习的一章中的所有其他问题(数组和数组列表),但是我似乎无法解决这个问题 以下是我为在连续数字周围加括号而提出的解决方案: c
697342(33)(666)(44)69(66)1(88)
我需要做什么
下一个问题是基本上获得这些单词的最长序列,并在它们周围放上括号。如果你有
1122345(6666)
基本上,你需要在四个6的周围放上括号,因为它们经常出现。
我已经完成了我正在学习的一章中的所有其他问题(数组和数组列表),但是我似乎无法解决这个问题
以下是我为在连续数字周围加括号而提出的解决方案:
class Seq
{
private ArrayList<Integer> nums;
private Random randNum;
public Seq()
{
nums = new ArrayList<Integer>();
randNum = new Random();
}
public void fillArrList()
{
for (int i = 0 ; i < 20 ; i++)
{
int thisRandNum = randNum.nextInt(9)+1;
nums.add(thisRandNum);
}
}
public String toString() {
StringBuilder result = new StringBuilder();
boolean inRun = false;
for (int i = 0; i < nums.size(); i++) {
if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) {
if (!inRun) {
result.append("(");
}
result.append(nums.get(i));
inRun = true;
} else {
result.append(nums.get(i));
if (inRun) {
result.append(")");
}
inRun = false;
}
}
return result.toString();
}
}
你所建议的方法可以奏效。然后,若newcount大于oldcount,则需要在另一个变量中存储一个额外的数字—最长序列开始处的索引 然后,您可以在该索引的位置插入 i、 e.如果您有1122345666
最大的序列从第一个数字6开始。这是在索引7处,因此将该7存储在一个变量中。我认为您需要迭代整个列表,即使当前计数低于旧计数,例如111224444如何
在迭代列表时保留4个变量:highestStartIndex、HighestIndex、highestCount和currentCount。迭代整个列表并使用currentCount对相等的相邻数字进行计数。当完成的currentCount高于highestCount时,更新最高*变量。最后使用*索引va写出数字变量。只有遍历所有元素后,才能知道最长的子序列
11222333333444555
11222(333333)444555
因此,只有在循环之后才能插入两个括号
所以你必须保持一个局部最优:起始索引加上长度或最优的最后一个索引。
然后为每个序列指定当前序列的起始索引
按要求: 最佳状态(序列)和当前状态是两件事,不能预先说任何当前状态都是最终的最佳状态
public String toString() {
// Begin with as "best" solution the empty sequence.
int startBest = 0; // Starting index
int lengthBest = 0; // Length of sequence
// Determine sequences:
int startCurrent = 0; // Starting index of most current/last sequence
for (int i = 0; i < nums.size(); i++) {
// Can we add the current num to the current sequence?
if (i == startCurrent || nums.get(i).equals(nums.get(i - 1)))) {
// We can extend the current sequence with this i:
int lengthCurrent = i - startCurrent + 1;
if (lengthCurrent > lengthBest) { // Current length better?
// New optimum:
startBest = startCurrent;
lengthBest = lengthCurrent;
}
} else {
// A different num, start here.
// As we had already a real sequence (i != 0), no need for
// checking for a new optimum with length 1.
startCurrent = i;
}
}
// Now we found the best solution.
// Create the result:
StringBuilder result = new StringBuilder();
for (int i = 0; i < nums.size(); i++) {
result.append(nums.get(i));
}
// Insert the right ')' first as its index changes by 1 after inserting '('.
result.insert(startBest + lengthBest, ")");
result.insert(startBest, "(");
return result.toString();
}
公共字符串toString(){
//以空序列作为“最佳”解决方案开始。
int startBest=0;//开始索引
int lengthBest=0;//序列的长度
//确定顺序:
int startCurrent=0;//最新/最后序列的起始索引
对于(int i=0;ilengthBest){//当前长度更好?
//新优化:
startBest=startCurrent;
长度最佳=长度电流;
}
}否则{
//另一个num,从这里开始。
//因为我们已经有了一个真正的序列(i!=0),所以不需要
//检查长度为1的新优化。
起始电流=i;
}
}
//现在我们找到了最好的解决方案。
//创建结果:
StringBuilder结果=新建StringBuilder();
对于(int i=0;i
第一个问题是如何找到序列的结尾,并设置序列的正确开头
原始算法的问题是只处理一个序列(一个子序列开始).这就是问题所在,我想首先找出哪个数字最为相同,在您的示例中333333计数应该是6。但是我的计数变量在循环中每次都会重置。如何使它在序列中的最大计数处停止。您能添加一些注释或解释更多关于for循环中的细节吗?我知道ed在纸上写了一些东西,但我现在真的迷路了:/尤其是我
-lengthBest
不是真的!谢谢,只有在else分支中,当一个新序列开始时,一个循环才能中断,而剩余的长度永远不能超过lengthBest
。有更好的优化方法。我注意到这有时会在第一个字符上加括号。例如我得到了这个输出(8)526729248583145494是的,我正在尝试,但是我的计数变量正在重置,请看我编辑的示例我遇到了问题,因为我的计数总是重置,并且与旧计数的值相同,你能看一下我编辑的代码段吗
public String toString() {
// Begin with as "best" solution the empty sequence.
int startBest = 0; // Starting index
int lengthBest = 0; // Length of sequence
// Determine sequences:
int startCurrent = 0; // Starting index of most current/last sequence
for (int i = 0; i < nums.size(); i++) {
// Can we add the current num to the current sequence?
if (i == startCurrent || nums.get(i).equals(nums.get(i - 1)))) {
// We can extend the current sequence with this i:
int lengthCurrent = i - startCurrent + 1;
if (lengthCurrent > lengthBest) { // Current length better?
// New optimum:
startBest = startCurrent;
lengthBest = lengthCurrent;
}
} else {
// A different num, start here.
// As we had already a real sequence (i != 0), no need for
// checking for a new optimum with length 1.
startCurrent = i;
}
}
// Now we found the best solution.
// Create the result:
StringBuilder result = new StringBuilder();
for (int i = 0; i < nums.size(); i++) {
result.append(nums.get(i));
}
// Insert the right ')' first as its index changes by 1 after inserting '('.
result.insert(startBest + lengthBest, ")");
result.insert(startBest, "(");
return result.toString();
}