Problem:
Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid. (See also: FizzBuzz Code)
fizzArray3(5, 10) → {5, 6, 7, 8, 9}
fizzArray3(11, 18) → {11, 12, 13, 14, 15, 16, 17}
fizzArray3(1, 3) → {1, 2}
Solution:
public int[] fizzArray3(int start, int end) { int n = end - start; int[] result = new int[n]; for (int i = 0; i < n; i++) result[i] = start++; return result; }
ReplyDeleteint dif = end-start;
int[] erg = new int[dif];
for(int i=0; i < dif; i++){
erg[i] = start+i;
}
return erg;
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] newArray = new int[end-start];
int count = 0;
for(int i=start; i<end; i++){
newArray[count] = i;
count++;
}
return newArray;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] arr = new int[end-start];
for (int i=start; i<end; i++){
//if (i==start)
arr[i-start]=i;
}
return arr;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] newArray = new int[end - start];
int addOn = 0;
for(int i = 0; i < end - start; i++) {
newArray[i] = start + addOn;
addOn++;
}
return newArray;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] fizzArray = new int[end - start];
for (int i = 0, j = start; j < end; i++, j++) {
fizzArray[i] = j;
}
return fizzArray;
public int[] fizzArray3(int start, int end) {
ReplyDeleteif(start >= end) return new int[0];
int[] out = new int[end - start];
for(int i = 0; i < out.length; i++) {
out[i] = start + i;
}
return out;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] q=new int[end-start];
int a=end-start;
for(int i=0;i<a;i++){
q[i]=start++;
}return q;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint diff = end-start;
int[] result = new int[diff];
for(int i=0; i<diff; i++)
{
result[i] = start;
start++;
}return result;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint size=end-start;
int a[]=new int[(size)];
for(int i=0;i<size;i++){
a[i]=start++;
}
return a;
}
int len= end-start;
ReplyDeleteint [] newArray= new int [len];
for (int i=0; i<len ; i++){
newArray [i] = start+i;
}
return newArray;
With Java Stream
ReplyDeletepublic int[] fizzArray3(int start, int end) {
return java.util.stream.IntStream.range(start, end).toArray();
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint s = end - start;
int[] nums = new int[s];
for(int i = start,j=0; i < end; i++,j++){
nums[j] += i;
}
return nums;
}
public int[] fizzArray3(int start, int end) {
ReplyDeleteint[] b=new int[end-start];
for(int i=start,j=0;i<end;i++,j++){
b[j]=i;
}
return b;
}