Problem:
Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.
has22({1, 2, 2}) → true
has22({1, 2, 1, 2}) → false
has22({2, 1, 2}) → false
Solution:
public boolean has22(int[] nums) { boolean found = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 2 && i > 0 && nums[i-1] == 2) { found = true; } if (nums[i] == 2 && i < nums.length-1 && nums[i+1] == 2) { found = true; } } return found; }
whats wrong with
ReplyDeletepublic boolean has22(int[] nums) {
if(nums.length<2) return false;
for(int i=0; i<nums.length-1;i++){
if(nums[i]==nums[i+1]) return true;
}
return false;
}
it will iterate over every element in the array and compare adjacent elements
public boolean has22(int[] nums) {
ReplyDeleteif(nums.length == 0) {
return false;
} else {
for(int i=0; i<nums.length-1; i++) {
if(nums[i] == 2) {
i++;
if(nums[i] == 2) {
return true;
}
}
}
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
if (i + 1 < nums.length) {
if (nums[i + 1] == 2) {
return true;
}
}
}
}
return false;
}
my sloution is
Deletepublic boolean has22(int[] nums) {
boolean twofound=false;
for (int i=0; i<nums.length; i++){
if (nums[i]== 2 && i+1<nums.length && nums[i+1]==2)
return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeleteboolean stop = false;
for(int i = 0; i < nums.length-1; i++) {
if (nums[i] == 2 && nums[i+1] == 2) {
stop = true;
}
}
return stop;
}
better)
Deletepublic boolean has22(int[] nums) {
ReplyDeletefor (int i=0; i<nums.length-1 ;i++){
if (nums[i]==2 && nums[i+1]==2) return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor (int i=1; i<nums.length; i++) {
if(nums[i-1]==2 && nums[i]==2) return true;
}
return false;
}
You don't need a loop to achieve that. Just by using indexOf, you can check the next index if 2 exists. Here is my solution with Javascript:
ReplyDeleteconst has22 = (givenArray) => {
const indexOfTwo = givenArray.indexOf(2);
if (indexOfTwo > -1 && givenArray[indexOfTwo + 1] === 2) {
return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeleteboolean found22 = false;
for(int i = 0; i<nums.length-1; i++){
if(nums[i] == 2 && nums[i+1] == 2){
found22 = true;
break;
}
else found22 = false;
}
return found22;
}
public boolean has22(int[] nums) {
ReplyDeleteboolean found22 = false;
for(int i = 0; i<nums.length-1; i++){
if(nums[i] == 2 && nums[i+1] == 2){
found22 = true;
break;
}
else found22 = false;
}
return found22;
}
public boolean has22(int[] nums) {
ReplyDeletereturn Arrays.toString(nums).contains("2, 2");
}
you are king
Deletepublic boolean has22(int[] nums) {
ReplyDeletefor(int i = 0;i < nums.length -1;i++){
if(nums[i] == 2 && nums[i+1] == 2) return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor( int i = 0 ; i < nums.length - 1 ; i++){
if(nums[i] == 2 && nums[i+1] == 2){
return true;
}
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeleteif(nums.length>0){
for(int i=0; i<nums.length-1; i++){
if(nums[i]==2 && nums[i+1]==2){
return true;
}
}
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length-1; i++){
if(nums[i]==2&&nums[i+1]==2)
return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length-1; i++) {
if(nums[i] == 2 && nums[i+1] == 2) {
return true;
}
}
return false;
}
ReplyDeleteGiven an array of ints, return true if the array contains a 2 next to a 2 somewhere.
ReplyDeleteGiven an array of ints, return true if the array contains a 2 next to a 2 somewhere.
public boolean has22(int[] nums) {
boolean twofound=false;
for (int i=0; i<nums.length; i++){
if (nums[i]== 2 && i+1<nums.length && nums[i+1]==2)
return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length;i++)
if(nums[i]==2){
if(i+1 < nums.length && nums[i+1]==2)
return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeleteboolean twoTwo = false;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 2 && i < nums.length-1 && nums[i + 1] == 2){
twoTwo = true;
}
}
return twoTwo;
}
public boolean has22(int[] nums) {
ReplyDeleteboolean twoTwo = false;
for(int i = 0; i < nums.length-1; i++){
if(nums[i] == 2 && nums[i + 1] == 2){
twoTwo = true;
}
}
return twoTwo;
}
public boolean has22(int[] nums)
ReplyDelete{
for(int i = 1; i < nums.length; i++)
{
if(nums[i-1] == 2 && nums[i] == 2) return true;
}
return false;
}
public boolean has22(int[] nums) {
ReplyDeletefor(int i = 0; i < nums.length; i++){
if(i < nums.length-1){
if(nums[i] == 2 && nums[i+1] == 2) return true;
}
}
return false;
}
OR
ReplyDeletepublic boolean has22(int[] nums) {
for(int i = 0; i < nums.length-1; i++){
if(nums[i] == 2 && nums[i+1] == 2) return true;
}
return false;
}
With Java Stream
ReplyDeletepublic static boolean has22(int[] nums) {
boolean result = IntStream.range(0, nums.length-1)
.anyMatch( i -> nums[i] == 2 && nums[i + 1] == 2);
return result;
}