Problem:
Given a non-empty array of ints, return a new array containing the elements from the original array that come before the first 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0.
pre4({1, 2, 4, 1}) → {1, 2}
pre4({3, 1, 4}) → {3, 1}
pre4({1, 4, 4}) → {1}
Solution:
public int[] pre4(int[] nums) { for (int i = 0; i < nums.length; i++) { if (nums[i] == 4 && i > 0) { int[] foo; foo = new int[i]; for (int j = 0; j < foo.length; j++) { foo[j] = nums[j]; } if (nums[0] != 4) return foo; } } int[] bar; bar = new int[0]; return bar; }
int[] t = new int[0];
ReplyDeletefor (int i = 0; i < nums.length; i++)
{
if (nums[i] == 4)
{
int[] tab = new int[i];
for (int j = 0; j < i; j++)
{
tab[j] = nums[j];
}
return tab;
}
}
return t;
at what do you initialized t
DeleteHow does this work? If array t is size zero and it appears to me that t does not interact with any other variables, how is the code valid?
Delete(I am very new to programming btw.)
see the t array is for other test cases to follow up where the value would be 0 array as a result.
Deleteany dork would do it with two cycles, try with one
ReplyDeleteman can you show solution with one cycle, if you can?
Deletefor(int i=0; i<nums.length; i++){
ReplyDeleteif(nums[i] == 4){
return Arrays.copyOfRange(nums, 0, i);
}
}
int[] temparr = new int[0];
return temparr;
public int[] pre4(int[] nums) {
ReplyDeleteint[] t = new int[nums.length];
for(int i = 0;i<nums.length;i++){
if(nums[i]==4){
if(t.length==nums.length){
t = new int[i];
i=0;
}
else return t;
if(t.length==0)
return t;
}
t[i]=nums[i];
}
return t;
}
it works
Deletesmth. like this:
ReplyDeletepublic int[] pre4(int[] nums) {
int[] res = new int[0];
int resLen = 0;
boolean found = false;
for (int i = 0; i < nums.length; i++) {
if (!found && nums[i] == 4) {
found = true;
resLen = i;
res = new int[resLen];
i = 0;
}
if (found && i < resLen)
res[i] = nums[i];
}
return res;
}
public int[] pre4(int[] nums) {
ReplyDeleteint len =0;
for (int i=0; i<nums.length; i++) {
if(nums[i]==4) {
len=i;
break;
//this break is important, can only get the first 4.
}
}
int[] array = new int[len];
for (int i =0; i<len; i++) {
array[i]=nums[i];
}
return array;
}
public int[] pre4(int[] nums) {
ReplyDeleteint i = 0;
while (nums[i] != 4) {
i++;
}
return Arrays.copyOf(nums,i);
}
public int[] pre4(int[] nums)
ReplyDelete{
int fourI = -1;
int[] result = new int[nums.length];
for(int i = 0; i < nums.length; i++)
{
if(nums[i] == 4 && fourI == -1)
{
fourI = i;
i = -1;
result = new int[fourI];
}
else if(fourI != -1 && i < fourI)
{
result[i] = nums[i];
}
}
return result;
}
public int[] pre4(int[] nums) {
ReplyDeleteint[] pre4;
int counter = 0;
for ( int i = 0; nums[i] != 4 ; i++) {
counter++;
}
pre4 = new int[counter];
for ( int i = 0; i < counter; i++) {
pre4[i] = nums[i];
}
return pre4;
}
Great solution!
Deletepublic int[] pre4(int[] nums) {
ReplyDeleteString res="";
for(int i=0;i<nums.length;i++) {
res+=nums[i];
}
res=res.substring(0,res.indexOf("4"));
int[] re=new int[res.length()];
for(int i=0;i<res.length();i++)
re[i]=Integer.valueOf(res.charAt(i)+"");
return re;
}
public int[] pre4(int[] nums) {
ReplyDeleteint i = 0;
while(nums[i++] != 4);
return Arrays.copyOf(nums, i-1);
}
public int[] pre4(int[] nums) {
ReplyDeleteint i = 0;
while(nums[i]!=4){
i++;
}
int[] result = new int[i];
for (int j = 0; j < i; j++) {
result[j] = nums[j];
}
return result;x
}
public int[] pre4(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length; i++){
if(nums[i] == 4){
return Arrays.copyOfRange(nums, 0, i);
}
}
return new int[0];
}
public int[] pre4(int[] nums) {
ReplyDeleteArrayList arrs=new ArrayList();
for(int num:nums){
if(num%4==0){
break;
}
arrs.add(num);
}
return arrs.stream().mapToInt(Integer::intValue).toArray();
}
public static int[] pre4(int[] nums) {
ReplyDeleteint[] newNums = new int[0];
for(int i=0; i<nums.length; i++){
if(nums[i]==4){
newNums = new int[i];
for(int j=0; j<i; j++){
newNums[j]=nums[j];
}
break;
}
}
return newNums;
}
My solution is working fine, but how to do it with one loop, as CodingBat tells to in Array2 Chapter?
ReplyDeletepublic int[] pre4(int[] nums) {
int index4 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 4) {
index4 = i;
break;
}
}
int[] pre4 = new int[index4];
for (int i = 0; i < pre4.length; i++) {
pre4[i] = nums[i];
}
return pre4;
}
public int[] pre4(int[] nums) {
ReplyDeleteString be4 = "";
for(int i= 0; i< nums.length; i++){
if(nums[i] != 4){
be4 += String.valueOf(nums[i]);
}else{
break;
}
}
int[] pre4 = new int[be4.length()];
for(int i = 0; i < be4.length(); i++) {
pre4[i] = Integer.parseInt(String.valueOf(be4.charAt(i)));
}
return pre4;
}
public int[] pre4(int[] nums) {
ReplyDeleteint n = 0, brojac=0;;
while(n<nums.length){
if(nums[n]!=4){
brojac++;
}else{
break;
}
n++;
}
int no[] = new int[brojac];
int i = 0;
while(i<nums.length){
if(nums[i]!=4){
no[i]=nums[i];
}else{
break;
}
i++;
}
return no;
}
//i did it this way..it is easier to read it!
With Java Stream
ReplyDeletepublic static int[] pre4(int[] nums) {
return Arrays.stream(nums).takeWhile(i -> i != 4).toArray();
}
public int[] pre4(int[] nums) {
ReplyDeleteint index = 0;
while(nums[index]!=4){
index++;
}
int a[] = new int[index];
for(int i=0; i<index ; i++){
a[i] = nums[i];
}
return a;
}
In one loop:
ReplyDeletepublic int[] pre4(int[] nums) {
int length = nums.length;
int[] result = new int[0];
boolean bool = false;
for (int i = 0; i < length; i++) {
if (nums[0] == 4) {
return result;
}
if (nums[i] == 4) {
result = new int[i];
bool = true;
length = i;
i = 0;
}
if (bool) {
result[i] = nums[i];
}
}
return result;
}