Problem:
Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
reverse3({1, 2, 3}) → {3, 2, 1}
reverse3({5, 11, 9}) → {9, 11, 5}
reverse3({7, 0, 0}) → {0, 0, 7}
Solution:
public int[] reverse3(int[] nums) { int[] myArray = new int[3]; myArray[0] = nums[2]; myArray[1] = nums[1]; myArray[2] = nums[0]; return myArray; }
public int[] reverse3(int[] nums) {
ReplyDeleteint[] a = new int[] {nums[2],nums[1],nums[0]};
return a;
}
public int []reverse3(int[] nums){
ReplyDeletereturn new int[] {nums[2],nums[1],nums[0]};}
int temp = nums[0];
ReplyDeletenums[0] = nums[2];
nums[2] = temp;
return nums;
my dad beats me
ReplyDeleteare you ok
Deletepublic int[] reverse3(int[] nums) {
ReplyDeleteint[] a = new int[3];
for (int i = nums.length-1, j = 0; j < nums.length; i--, j++){
a[j]=nums[i];
}
return a;
}
👍
Deletereturn new int[] {nums [2], nums[1], nums[0]};
ReplyDeletepublic int[] reverse3(int[] b) {
ReplyDeleteint[] a=new int[3];
a[0]= b[2];
a[1]= b[1];
a[2]= b[0];
return a;
}
int[] newReverse3 = new int[nums.length];
ReplyDeletefor (int i = 0; i < nums.length; ++i)
newReverse3[i] = nums[nums.length-1-i];
return newReverse3;
This is simple and easy to remember.
Deletepublic int[] reverse3(int[] nums) {
ReplyDeleteint i ;
i=nums[0];
nums[0]=nums[2];
nums[2]=i;
return nums;
}
why u guys chatting in google
ReplyDeletepublic int[] reverse3(int[] nums) {
ReplyDeleteint j = 0;
for(int i = nums.length-1; i > 0; i--){
int temp = nums[j];
nums[j++] = nums[i];
nums[i] = temp;
}
return nums;
}