Java核心Java中的数组
在这个问题中,如果数组中的每一对相邻元素中至少有一对是该值,则该值在数组中“无处不在”。如果给定值在数组中无处不在,则返回trueJava核心Java中的数组,java,arrays,Java,Arrays,在这个问题中,如果数组中的每一对相邻元素中至少有一对是该值,则该值在数组中“无处不在”。如果给定值在数组中无处不在,则返回true isEverywhere({1,2,1,3},1)→ 是的,因为在(1,2)和(1,3)中存在coz 1 isEverywhere({1,2,1,3},2)→ 错误,因为2在(1,2)中,但不在(1,3) isEverywhere({1,2,1,3,4},1)→ false,因为Z 1存在于2对(1,2)和(1,3)中,但4没有1对 下面是我的部分工作代码,你能帮我
isEverywhere({1,2,1,3},1)→ 是的,因为在(1,2)和(1,3)中存在coz 1
isEverywhere({1,2,1,3},2)→ 错误,因为2在(1,2)中,但不在(1,3)
isEverywhere({1,2,1,3,4},1)→ false,因为Z 1存在于2对(1,2)和(1,3)中,但4没有1对
下面是我的部分工作代码,你能帮我解决这个问题吗?我被困了很长时间
::代码::
public boolean isEverywhere(int[] nums, int val) {
boolean flag = false;
for(int i=0;i<nums.length;i++){
for(int j=i+2;j<nums.length;j++){
if(nums[i] == nums[j]){
flag = true;
}
}
}
return flag;
}
isEverywhere({1,2,1,3},1)→ 正确正确正确
isEverywhere({1,2,1,3},2)→ 假真X
isEverywhere({1,2,1,3,4},1)→ false-true X
您从未在代码中使用val试着先弄清楚这一点的逻辑
这可以通过一个循环来完成,试着找出你作为一个人应该如何应用一个算法,然后编码它。你的代码失败了,因为当第二个循环超出数组长度时,标志不会关闭。
公共类IE{
public class IE {
static boolean isEverywhere(int[] a, int val) {
for (int i = 0; i < a.length; i++) {
System.out.println("*** iteration: " + i);
System.out.println("a[" + (i-1) + "]=" + (i > 0 ? a[i-1] : "n/a"));
System.out.println("a[" + i + "]=" + a[i]);
System.out.println("a[" + (i+1) + "]=" + (i < a.length -1 ? a[i+1] : "n/a"));
if (a[i] != val && (i > 0 ? a[i-1] != val : true) && (i < a.length-1 ? a[i+1] != val : true)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] a = new int[] {1, 2, 1, 3};
System.out.println(isEverywhere(a, 1));
a = new int[] {1, 2, 1, 3};
System.out.println(isEverywhere(a, 2));
a = new int[] {1, 2, 1, 3, 4};
System.out.println(isEverywhere(a, 1));
}
}
静态布尔isEverywhere(int[]a,int val){
for(int i=0;i0?a[i-1]:“n/a”);
System.out.println(“a[“+i+”]=”+a[i]);
System.out.println(“a[”+(i+1)+“]=”+(i0?a[i-1]!=val:true)和(i
基本上,对于数组中的给定元素,如果值不相邻,则测试失败,无论是在它之前还是之后 关键是
对于阵列中的每对相邻元素
在if(nums[i]==nums[j])
之前打印出i
和j
,你就会看到发生了什么
对于长度为2的数组,需要(0,1),对于长度为3的数组,需要(0,1)、(1,2)等等。不是完整的解决方案,但如果数组的长度为奇数,请检查最后一个值是否等于目标值
...
if( !(nums.length % 2) == 0 ){
if( nums[length-1] != val){
return false;
}
}
...
试试这个:
boolean isEverywhere(int[] nums, int val) {
// use i+=2 to get start index of pair.
for(int i=0;i<nums.length;i+=2) {
// other index in the pair.
int j = i + 1;
// make sure the other index really exists.
if(j < nums.length) {
// if it exists..and val is not equal to
// either in the pair..return false.
if(nums[i] != val && nums[j] != val) {
return false;
}
} else {
// no pair..one element case.
// return true if that element is val..else return false.
return nums[i] == val;
}
}
// array has no unpaired element..and all pairs have val.
return true;
}
布尔isEverywhere(int[]nums,int val){
//使用i+=2获取对的开始索引。
对于(int i=0;i我通过简单地计算是否有空格来实现:
这就是它简单的荣耀:
public boolean isEverywhere(int[] nums, int val) {
//Create an int to count the number of spaces between each instance of val.
int counter = 0;
//Create a for loop that checks val;
for (int i = 0; i < nums.length; i++)
{
//If the number at iteration i != val;
if (nums[i] != val)
{
//We increment the counter by one.
counter++;
}
//If the number does equal val, we put the counter back to 0;
else
{
counter = 0;
}
//If the counter is greater than or equal to two,
//We know that there is a space of more than 2, and so
//We return false;
if (counter >= 2)
{
return false;
}
}
//Return true if not false;
return true;
}
public boolean isEverywhere(int[]nums,int val){
//创建一个int来计算val的每个实例之间的空格数。
int计数器=0;
//创建检查val的for循环;
对于(int i=0;i=2)
{
返回false;
}
}
//如果不为false,则返回true;
返回true;
}
此代码工作正常:-
public boolean isEverywhere(int[] nums, int val) {
for(int i=0;i<=nums.length-2;i++){
if(nums[i]!=val&&nums[i+1]!=val){
return false;
}
}
return true;
}
public boolean isEverywhere(int[]nums,int val){
对于(int i=0;icase 2和案例3为failing@Deepak:为什么失败?看起来好像作业标签应该在这里。你不需要为此嵌套循环。分解问题。首先尝试枚举你正在考虑的对,并执行System.out.println以查看你考虑的对是否正确。即使是很小的问题ms可以一步一步地进行。@Yar,谢谢你的那句话。这根本不能解决问题,你不应该只是帮他做作业。他没有说这是作业,是吗?是的,它对给定的输入有效(现在我编辑了)。@codaddict,我有一个问题,例外情况是什么({3},1)→ 真假X@Deepak:`isEverywhere({3},1)`返回false
,正如预期的那样,因为没有1
。这不是您想要的吗?是的,正确,可能我需要检查我的测试用例一次,再次感谢,很抱歉给您添麻烦@coaddict@Deepak例如像isEverywhere({1,3,4,1},1)这样的情况如何如果我没有弄错的话,这可能会返回false,但这段代码会返回true。这是因为它只会检查(1,3)和(4,1)不是对(3,4)已考虑?@shaded,只考虑邻接对并将返回true。@codaddict是正确的,我刚刚检查了上次澄清的算法,为什么u返回nums[i]==val;
public boolean isEverywhere(int[] nums, int val) {
for(int i=0;i<=nums.length-2;i++){
if(nums[i]!=val&&nums[i+1]!=val){
return false;
}
}
return true;
}