Problem:
Given an int array length 2, return true if it contains a 2 or a 3.
has23({2, 5}) → true
has23({4, 3}) → true
has23({4, 5}) → false
Solution:
public boolean has23(int[] nums) { if (nums[0] == 2 || nums[0] == 3 || nums[1] == 2 || nums[1] == 3) return true; else return false; }
You can shorten this to:
ReplyDeletereturn (nums[0] == 2 || nums[0] == 3 || nums[1] == 2 || nums[1] == 3);
uR SO smaRT i wANTeD 2 SSSSAYAYAYA dat
Deletepublic boolean has23(int[] nums) {
ReplyDeleteif(nums[0] == 2 || nums[0] == 3 || nums[1] == 2 || nums [1] == 3)
{
return true;
}
else
{
return false;
}
}
String str = String.valueOf(nums[0]) + String.valueOf(nums[1]);
ReplyDeletereturn(str.contains("2")||str.contains("3"));
//More easy way
public boolean has23(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if ((nums[i] == 2) || (nums[i] == 3)) {
return true;
}
}
return false;
}
I did the same, cheers
Deletepublic boolean has23(int[] nums) {
ReplyDeletefor(int x : nums){
if(x == 2 || x == 3){
return true;
}
}
return false;
}
boolean isContians = false;
ReplyDeletefor(int i = 0; i<nums.length; i++){
if(nums[0]==2 || nums[0] == 3 || nums[1]==2 || nums[1] == 3) {
isContians = true;
}else{
isContians = false;
}
}
return isContians;
public boolean has23(int[] nums) {
ReplyDeleteboolean isTwoThree = false;
for(int each:nums){
if(each==2 || each==3){
isTwoThree = true;
}
}
return isTwoThree;
}
basic logic
ReplyDeletepublic boolean has23(int[] nums) {
if(nums[0]==2||nums[0]==3){
return true;
}
else
if(nums[1]==2||nums[1]==3){
return true;
}
else {
return false;
}
}