2012-03-05 74 views
0

以下不是一个家庭作业的问题,它只是一组的,我一直在努力通过实践的问题,我想知道如果任何人能搞清楚:在多回路彻底难倒了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; 
} 

回答

2

我不知道java,但也许我可以帮忙。我不想给你解决方案,但想起来是这样的:

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

你需要改变的唯一的点是3s后的景点......所以......每当你循环时,你的程序应该知道,如果它发现一个点后3 “T A 4 ....

它也应该知道,如果发现不是由前面3任何4S ......

每个循环期间,一旦它找到的每个的那些的位置两件事,你应该知道该怎么做。

+0

另一个重要的问题是,问题规范还指出,每个输入是“可解决的”。 IE浏览器。将不会有任何解决方案。 – cdeszaq 2012-03-05 22:45:54

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提供的全部代码。尝试完成它,正如你所说的那样是为了你的练习。

+0

当前3个字符出现在最后4个字符后面时,会出现一个小问题,我不知道这是否是有意或无意的,但它需要遍历整个数组,而不仅仅是数组的其余部分。 – twain249 2012-03-05 22:48:22

+0

啊,很好的观察。我编辑它。 – noMAD 2012-03-05 22:52:04

+0

我今天下课后才得到这个!我尝试了一种不同的方法,它应该占据3之前的4。就像我说的,我还是一个新手,所以任何你可以优化或推荐的内容,只要“最佳实践”将非常感谢! – user1080519 2012-03-07 19:28:23

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; 
    } 
+0

这是我对问题的解决方案 – abhimita 2014-12-09 10:08:37

+0

请阅读下面的内容:http://stackoverflow.com/help/how-to-answer“任何能让提问者朝着正确方向行事的答案都是有帮助的,但是试图提到任何限制,在你的答案中假设或简化,简洁是可以接受的,但更全面的解释更好。“不要只提供代码,也要提供一些解释。 – 2014-12-09 10:12:54

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; 
} 
0

我用两个ArrayLists解决了我的问题,它包含3和4的地方。 我希望这可以帮助。

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; 
    } 
+0

这很简单吗? – 2016-12-31 03:57:21

+0

我可以使用数组,但我认为使用ArrayLists更好,更通用。 – 2016-12-31 04:01:28