LintCode 交錯(cuò)正負(fù)數(shù)

題目

給出一個(gè)含有正整數(shù)和負(fù)整數(shù)的數(shù)組,重新排列成一個(gè)正負(fù)數(shù)交錯(cuò)的數(shù)組。

注意事項(xiàng)

不需要保持正整數(shù)或者負(fù)整數(shù)原來(lái)的順序。

樣例
給出數(shù)組[-1, -2, -3, 4, 5, 6],重新排序之后,變成[-1, 5, -2, 4, -3, 6]或者其他任何滿足要求的答案

分析

最簡(jiǎn)單的思路顯然是用兩個(gè)數(shù)組記錄正數(shù)和負(fù)數(shù),最后在遍歷一遍即可

要求原地完成的思路:
兩根指針,首先判斷正數(shù)多還是負(fù)數(shù)多,并把多的那一部分移到后半部分,最后兩根指針?lè)謩e遞增二交換即可
具體思路看代碼注釋

代碼

class Solution {
    /**
     * @param A: An integer array.
     * @return: void
     */
    public int[] rerange(int[] A) {
        // Check the input parameter.
        if(A == null || A.length < 3)
            return A;
        
        int n = A.length;
        
        int countPositive = 0;//計(jì)算正數(shù)的個(gè)數(shù)
        
        
        
        // store the positive numbers index.
        int positiveIndex = 0;
        int pos = 1;
        int neg = 0;
        for(int i=0;i<n;i++) {
                if(A[i] > 0) {
                // Put all the positive numbers at in the left part.
                    swap(A,positiveIndex++,i);
                    countPositive++;
                }
        }
        
        if(countPositive > n/2) {
        // If positive numbers are more than negative numbers,
        // Put the positive numbers at first.
            pos = 0;
            neg = 1;
            // Reverse the array.
            
            int left = 0;
            int right = n-1;
            while(left < right) {
                swap(A,left,right);
                left++;
                right--;
            }
        }
        
        while(pos < n && neg <n) {
            while(pos<n && A[pos]>0)
                pos +=2;
            while(neg<n && A[neg]<0)
                neg +=2;
            if(neg >= n || pos>=n)
                break;
            swap(A,pos,neg);
        }
        
        // Reorder the negative and the positive numbers.
       
            // Should move if it is in the range.
            
            // Should move if it is in the range.
       return A; 
   }
   
   public void swap(int[] A, int l, int r) {
       int temp = A[l];
       A[l] = A[r];
       A[r] = temp;
   }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 第5章 引用類型(返回首頁(yè)) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,270評(píng)論 0 4
  • 刷題啦刷題啦,劍指offer好像比較有名,所以就在牛客網(wǎng)上刷這個(gè)吧~btw,刷了一些題發(fā)現(xiàn)編程之美的題好典型啊!!...
    Cracks_Yi閱讀 432評(píng)論 0 1
  • 指針是C語(yǔ)言中廣泛使用的一種數(shù)據(jù)類型。 運(yùn)用指針編程是C語(yǔ)言最主要的風(fēng)格之一。利用指針變量可以表示各種數(shù)據(jù)結(jié)構(gòu); ...
    朱森閱讀 3,479評(píng)論 3 44
  • 多線程的優(yōu)點(diǎn) 資源利用率更好(等待IO的時(shí)間) 程序設(shè)計(jì)在某些情況下更簡(jiǎn)單(一個(gè)線程對(duì)應(yīng)一個(gè)任務(wù)) 程序相應(yīng)更快(...
    進(jìn)擊的勇士閱讀 337評(píng)論 0 0
  • 在和新入門的壺友交流時(shí),我經(jīng)常會(huì)被問(wèn)到:買什么壺好,你看看這把壺怎么樣,看中一把壺多少錢買合適...等等,針對(duì)這些...
    賞壺堂閱讀 234評(píng)論 0 0