Java:检查数组中是否存在三个或更多匹配项
我想做一个函数,检查数组中是否有任何数字存在三次或三次以上 因此,此阵列示例: 4-6-14-8-6-15-14-15-13 -十,- 不应输出任何内容,但以下内容除外: 4-6-14-8-6-15-14-15-14 -十,- 系统应输出数字14存在三次的输出 如何做到这一点?我开始做一个for循环Java:检查数组中是否存在三个或更多匹配项,java,arrays,Java,Arrays,我想做一个函数,检查数组中是否有任何数字存在三次或三次以上 因此,此阵列示例: 4-6-14-8-6-15-14-15-13 -十,- 不应输出任何内容,但以下内容除外: 4-6-14-8-6-15-14-15-14 -十,- 系统应输出数字14存在三次的输出 如何做到这一点?我开始做一个for循环 for(int i=0; i<array.length; i++){ } for(int i=0;i 输出: 1 exists 4 times. 2 exists 3 times. 3 ex
for(int i=0; i<array.length; i++){
}
for(int i=0;i
输出:
1 exists 4 times.
2 exists 3 times.
3 exists 3 times.
因此,首先将数组复制到整数[]数组中。后一种算法也解决了输出的问题。只需制作一个数字的hashmap
,其中数字值作为键,出现时间作为映射值。
在for循环之后,您可以在地图上迭代并打印出现值大于3的数字如果一个数字存在4次怎么办
- 没有输出,因为3不是4,所以
正好要3个
- 1x输出,因为
你搜索至少3个点击
- 2倍输出,因为您可以显示3
三倍
您可以对数组进行排序,然后检查每个新元素下两个元素是否相同。如果相同,则保存元素。如果不重置计数器并转到下一个元素您可以开始排序数组,然后开始检查i+1,如果相等,则递增计数器,否则将其设置为0,则重复此操作,直到计数器不等于2
private static int findTree(int[] array){
Arrays.sort(array);
int i = 0;
int counter = 0;
while (counter != 2 && i < array.length - 1) {
if(array[i] == array[i+1]){
counter++;
} else {
counter =0;
}
i++;
}
if(counter == 2){
return array[i];
}
return -1;
}
私有静态int findTree(int[]数组){
数组。排序(数组);
int i=0;
int计数器=0;
while(计数器!=2&&i
编辑:
更复杂的alg,找到给定数量的n个匹配项
假设你必须找到至少n个相等的数字
如果我们首先对数组进行排序,那么所有元素都将按顺序排列,因此如果我们需要找到四(n)个相等的元素,我们可以取一(i)并检查距离他三个空格的元素(i+n-1)。如果这个元素相等,我们在数组中的i元素下找到了解决方案。如果不相等,我们可以通过将i增加1(i++)来移动到下一个元素
事情是这样的:
private static int findN(int[] array, int n){
if(n == 0) {
return -1;
}
Arrays.sort(array);
n--; //We reduce our n searched element because in i we already have one
int i = 0;
boolean found = false;
while (found == false && i < array.length - n) {
if(array[i] == array[i+n]) { //remember that we have reduced the n by one.
found = true;
} else {
i++;
}
}
if(found) {
return array[i];
}
return -1;
}
私有静态int findN(int[]数组,int n){
如果(n==0){
返回-1;
}
数组。排序(数组);
n--;//我们减少了n个搜索元素,因为在i中我们已经有了一个
int i=0;
布尔值=false;
while(found==false&&i
如果一个以上的数字存在3次或3次以上怎么办
- 报告每个号码
- 只报告第一个
对于一般情况,这里有一个解决方案:在列表中搜索每个元素是否有X个出现
public class TripleTest
{
public static void main (String[] args)
{
int[] list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
findCount (list, 3);
}
public static void findCount (int[] list, int count)
{
for (int i = 0; i <= list.length - count; ++i)
{
int elem = list [i];
boolean hit = findWhatCountFrom (list, i + 1, elem, count - 1);
if (hit)
System.out.println ("hit: " + elem);
}
}
public static boolean findWhatCountFrom (int[] list, int idx, int what, int count)
{
if (count == 0)
return true;
if (idx == list.length)
return false;
return findWhatCountFrom (list, idx + 1, what, count - ((list [idx] == what) ? 1 : 0));
}
}
公共类三重测试
{
公共静态void main(字符串[]args)
{
int[]list={1,2,2,3,3,3,4,4,4,4};
findCount(列表,3);
}
公共静态void findCount(int[]列表,int计数)
{
对于(int i=0;i)你认为你应该怎么做?你听说过直方图吗?没有,我没有。我想我应该做一个if语句,以某种方式检查和存储一些东西。@vidstige作业?你知道数组中最大的数字是多少吗?这很好,但问题是“三次或更多次”,所以你也许应该调整一下以满足这个要求。这个算法的计算复杂度是多少?应该是O(n^2),其中n是数组元素的数目。(你可以做得更好,我知道:)@Simon谢谢你的回答。我测试过这个,唯一的问题是它输出文本,“x存在y次”,y次..所以我得到,如果一个数字存在4次,它会显示这个输出4次..我只想让它说一次。你可以用两个循环(显示三个循环)来实现这一点,但不是嵌套的。我认为hashmap在这种情况下是开销,Simons解决方案似乎是这样的fine@cOmrade-@Manoj的解决方案是O(N)
在时间上,O(N)
在空间中。@Simon的解决方案是时间上的O(N^2)
,空间上的O(1)
。Vash的解决方案是O(N log N)时间O(1)空间。
private static int findN(int[] array, int n){
if(n == 0) {
return -1;
}
Arrays.sort(array);
n--; //We reduce our n searched element because in i we already have one
int i = 0;
boolean found = false;
while (found == false && i < array.length - n) {
if(array[i] == array[i+n]) { //remember that we have reduced the n by one.
found = true;
} else {
i++;
}
}
if(found) {
return array[i];
}
return -1;
}
public class TripleTest
{
public static void main (String[] args)
{
int[] list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
findCount (list, 3);
}
public static void findCount (int[] list, int count)
{
for (int i = 0; i <= list.length - count; ++i)
{
int elem = list [i];
boolean hit = findWhatCountFrom (list, i + 1, elem, count - 1);
if (hit)
System.out.println ("hit: " + elem);
}
}
public static boolean findWhatCountFrom (int[] list, int idx, int what, int count)
{
if (count == 0)
return true;
if (idx == list.length)
return false;
return findWhatCountFrom (list, idx + 1, what, count - ((list [idx] == what) ? 1 : 0));
}
}