Problem:
Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.
makeLast({4, 5, 6}) → {0, 0, 0, 0, 0, 6}
makeLast({1, 2}) → {0, 0, 0, 2}
makeLast({3}) → {0, 3}
Solution:
public int[] makeLast(int[] nums) { int[] num = new int[nums.length*2]; num[nums.length*2 - 1] = nums[nums.length -1]; return num; }
or
ReplyDeletenum[num.length-1] = nums[nums.length-1];
660
ReplyDeletegive me the full code of that problem plss
ReplyDeletegive me the full code of that problem plss
ReplyDeletepublic int[] makeLast(int[] nums) {
ReplyDeleteint doub = (nums.length * 2);
int last = nums[nums.length-1];
int [ ] data = new int [ doub ];
data[data.length-1] = last;
return data;
}
public int[] makeLast(int[] nums) {
ReplyDeleteif(nums.length==4)
return new int[]{0,0,0,0,0,0,0,nums[nums.length-1]};
else if(nums.length==3)
return new int[]{0,0,0,0,0,nums[nums.length-1]};
else if(nums.length==2)
return new int[]{0,0,0,nums[nums.length-1]};
else
return new int[]{0,nums[nums.length-1]};
}