完全被多循环Java程序困扰

时间:2012-03-05 22:32:19

标签: java

以下不是一个家庭作业问题,它只是我在练习中所经历的一系列问题,我想知道是否有其他人可以解决这个问题:

http://codingbat.com/prob/p159339

返回一个包含与给定数组完全相同的数字的数组,但重新排列,以便每3个后面紧跟一个4.不要移动3,但每个其他数字可能会移动。该数组包含相同数量的3和4,每3个后面有一个不是3或4的数字,并且在任何4之前数组中出现3。

*已解决 - 这是我的工作代码:

    public int[] fix34(int...nums) 
{
    int[] returnArray = new int[nums.length];

    //ASSIGN ARRAY
    //We know that all 3's can't be moved, and after every 3 there
    //will automatically be a 4

    for(int i = 0; i<nums.length; i++)
    {
        if(nums[i] == 3)
        {
           returnArray[i] = 3;
           returnArray[i+1] = 4;
        }
    }

    //REBUILD ARRAY - UNMOVED INDEXES
    //If a value was not moved/affected by the above, it will get placed into the array
    //in the same position

    for (int i = 0; i < nums.length; i++)
    {
        if (returnArray[i] != 3 && returnArray[i] != 4 && nums[i] != 3 && nums[i] != 4)
        {
            returnArray[i] = nums[i];
        }
    }       

    //REBUILD ARRAY - MOVED INDEXES
    //changed values = 0 in returnArray, as a result, any time we hit a 0 we
    //can simply assign the value that was in the 4's place in the nums array

    OuterLoop: for (int i = 0; i < nums.length; i++)
    {
        if (returnArray[i] == 0)
        {
            for (int n = 0; n < returnArray.length; n++)
            {
                if (returnArray[n] == 4)
                {
                    returnArray[i] = nums[n];
                    continue OuterLoop;
                }
            }
        }
    }

    return returnArray;
}

5 个答案:

答案 0 :(得分:2)

我不懂java,但也许我可以提供帮助。我不想给你解决方案,但想到这样:

你可以移动每个不是3的数字,这是我们唯一的限制。话说:

你需要改变的唯一点是3s后的点....所以....每次循环时,你的程序应该知道它是否在3之后找到了一个不是4的点。 ...

它还应该知道它是否发现任何4s前面没有3 ......

在每个循环中,一旦找到这两个东西的位置,你应该知道该怎么做。

答案 1 :(得分:0)

初始化所有变量

    for(int i = 0; i<n-1; i++)
    {
        if(arr[i] == 3)
        {
            if(arr[i+1] == 4)
                continue;
            else
            {
                temp = 0;
                while(arr[temp] != 4)
                    temp++;
                //Write your own code here
            }
         //Complete the code
    }

NOT提供了整个代码。尝试完成它,因为你说这是为了你的练习。

答案 2 :(得分:0)

public int[] fix34(int[] nums) {
    int[] arr = new int[nums.length];
            int index = 0;
            int tempVal= 0,j=0;

            for(int i=0;i<nums.length;i++){
                if(nums[i]==3){
                    arr[i] = nums[i];
                    index=i+1;
                    tempVal = nums[i+1];
                    j=index;

                    while(j<nums.length){
                        if(j<nums.length && nums[j]==4){


                            //System.out.println(j+"\t="+nums[j]);
                            nums[j]=tempVal;
                            nums[index] = 4;
                            break;

                        }
                        j++;

                    }
                    tempVal=0;
                    index=0;
                    }else{
                    arr[i] = nums[i];
                }


            }

            index =0;


            for(int i=0;i<nums.length;i++){
                if(nums[i]==3 && nums[i+1]==4){
                    i+=1;
                }else if(nums[i]==4){
                    index = i;
                    j=index;

                    while(j<nums.length){
                        if(nums[j]==3 && nums[j+1]!=4){

                            arr[index] = nums[j+1];
                            arr[j+1] = 4;

                        }

                        j++;
                    }

                }
            }

            return arr;
    }

答案 3 :(得分:0)

这是我的:有点矫枉过正,但总是对的,无论如何我做了2个额外的数组,我在循环中做了2次传递,将正确的元素放在正确的位置。见下面的逻辑。

public int[] fix34(int[] nums) {
  int index1 = 0;
  int index2 = 0;
  int index3 = 0;
  int[] only4 = fours(nums); //holds all 4's in nums
  int[] misc = new int[count4(nums)]; //will hold numbers after 3
  for(int a = 0; a < nums.length - 1; a++){
  if(nums[a] == 3){
  misc[index1] = nums[a + 1]; //get it for later use
  index1++;
  nums[a + 1] = only4[index2]; //now the number after 3 is a 4, from the 
  index2++;                    //only4 array
  }
  }
  for(int b = 1; b < nums.length; b++){
  if(nums[b] == 4  && nums[b - 1] != 3){ //finds misplaced 4's
  nums[b] = misc[index3]; //replaces lone 4's with the
  index3++; //right hand side of each 3 original values.
  }
  }
  return nums;
}
public int count4(int[] nums){
  int cnt = 0;
  for(int e : nums){
  if(e == 4){
  cnt++;
  }
  } 
  return cnt;
}
public int[] fours(int[] nums){
  int index = 0;
  int[] onlyFours = new int[count4(nums)]; //must set length
  for(int e : nums){
  if(e == 4){
  onlyFours[index] = e;
  index++;
  }
  }
  return onlyFours;
}

答案 4 :(得分:0)

我使用两个包含3&4和4的位置的ArrayLists解决了我的问题。 我希望这会有所帮助。

public int[] fix34(int[] nums) 
{
      //Create a copy of nums to manipulate.
      int[] ret = nums;
      //Create two ArrayLists which carry corresponding places of 3 and 4;
      ArrayList<Integer> threePositions = new ArrayList<Integer>();
      ArrayList<Integer> fourPositions = new ArrayList<Integer>();
      //Get the places of 3 and 4 and put them in the respective ArrayLists.
      for (int i = 0; i < ret.length; i++)
      {
        if (ret[i] == 3)
        {
          threePositions.add(i);
        }
        if (ret[i] == 4)
        {
          fourPositions.add(i);
        }
      }
      //Swap all ints right after the 3 with one of the 4s by using the referenced
      //ArrayLists values.
      for (int i = 0; i < threePositions.size(); i++)
      {
        int temp = ret[threePositions.get(i) + 1];
        ret[threePositions.get(i) + 1] = ret[fourPositions.get(i)];
        ret[fourPositions.get(i)] = temp;
      }
      //Return the ret array.
      return ret;
    }