Problem:
Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}.
has77({1, 7, 7}) → true
has77({1, 7, 1, 7}) → true
has77({1, 7, 1, 1, 7}) → false
Solution:
public boolean has77(int[] nums) { boolean result = false; for (int i = 0; i < nums.length-1; i++) if ((nums[i] == 7 && nums[i+1] == 7)) result = true; for (int i = 0; i < nums.length-2; i++) if ((nums[i] == 7 && nums[i+2] == 7)) result = true;
Why pass through twice?
ReplyDeletepublic boolean has77(int[] nums) {
for(int i = 0; i<nums.length-1;i++)
{
if(nums[i]==7 && (nums[i+1]==7 || (i<nums.length-2&&nums[i+2]==7))) return true;
}
return false;
}
public boolean has77(int [] nums){
ReplyDeletefor(int x = 0; x < nums.length-2; x++){
if(int[x] == 7 && int[x+1] == 7){
return true;
}
if(int[x] == 7 && int [x+2] == 7){
return true;
}
if(int[x+1] == 7 && int [x+2] == 7){
return true;
}
}
return false;
}
public boolean has77(int[] nums) {
ReplyDeleteboolean result = false;
for (int i = 0; i < nums.length-1; i++)
if ((nums[i] == 7 && nums[i+1] == 7))
result = true;
for (int i = 0; i < nums.length-2; i++)
if ((nums[i] == 7 && nums[i+2] == 7))
result = true;
return result;
}
public boolean has77(int[] nums) {
ReplyDeleteboolean flag = false;
for(int i = 0; i < nums.length-2; i++){
if((nums[i] == 7 && nums[i +1] == 7)||
(nums[i] == 7 && nums[i +2] == 7) ||
(nums[nums.length-2] == 7 && nums[nums.length-1] == 7)){
flag = true;
}
}
return flag;
}
public boolean has77(int[] nums) {
ReplyDeletefor(int i=1;i<nums.length;i++){
if(nums[i]==7 && nums[i-1]==7){
return true;
}else if(i<nums.length-1 && nums[i-1]==7 && nums[i]!=7 && nums[i+1]==7){
return true;
}
}
return false;
}
public boolean has77(int[] nums) {
ReplyDeletefor (int i =0; i < nums.length -2; i++) {
if (nums[i] == 7 && ( nums[i+1] == 7 || nums[i+2] == 7)) return true;
if (nums[i+1] == 7 && nums[i+2] == 7) return true;
}
return false;
}
public boolean has77(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length;i++){
if(i+1<nums.length && nums[i]==7 && nums[i+1]==7 ){
return true;
}
else if(i+2<nums.length && nums[i]==7 && nums[i+2]==7 ){
return true;
}
}
return false;
}
public boolean has77(int[] nums) {
ReplyDeleteboolean bool = false;
for( int i = 0; i < nums.length - 2; i++ ) {
if( nums[i] == 7 && (7 == nums[i+2] || 7 == nums[i+1]) ){
bool = true;
}
if( nums[i+1] == 7 && nums[i+2] == 7 ){
bool = true;
}
}
return bool;
}
public boolean has77(int[] nums) {
ReplyDeleteboolean b = false;
for (int i = 0; i < nums.length - 2; i++)
if ((nums[i] == 7 && nums[i] == nums[i+1]) || ((nums[i] == 7 && nums[i] != nums[i+1]) && nums[i] == nums[i+2]) || (nums[i] != 7 && nums[i+1] == 7) && nums[i+1] == nums[i+2])
b = true;
return b;
}
public static boolean has77(int[] nums) {
ReplyDeleteint len = nums.length;
boolean sevens = false;
for(int i=0; i<len-1; i++){
if(nums[i]==7){
if(nums[i+1]==7 || (i<(len-2) && nums[i+2]==7)){
sevens = true;
}
}
}
return sevens;
}
public boolean has77(int[] nums)
ReplyDelete{
for(int i = 2; i < nums.length; i++)
{
if(nums[i-2] == 7 && nums[i-1] == 7
|| nums[i-2] ==7 && nums[i] == 7
|| nums[i-1] == 7 && nums[i] == 7) return true;
}
return false;
}
splendid
Deletepublic boolean has77(int[] nums) {
ReplyDeleteint found = -3;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 7){
if(i - found <= 2){
return true;
}
found = i;
}
}
return false;
}
This is mine. Tell me if there is a more simple way.
ReplyDeletepublic boolean has77(int[] nums) {
boolean match = false;
for (int i = 1;i<nums.length-1;i++){
if((nums[i]==7 && nums[i+1]==7)||(nums[i-1]==7 && nums[i+1]==7)||(nums[i]==7 && nums[i-1]==7)){
match =true;
}
}
return match;
}