Mauritus national flag problem

前端 未结 6 748
时光说笑
时光说笑 2021-02-14 16:52

I\'ve made a solution for the Dutch national flag problem already.

But this time, I want to try something more difficult: the Mauritus national flag problem - 4 colours,

6条回答
  •  失恋的感觉
    2021-02-14 17:27

    I do have a similar kind of code but insted of

    function sort(a:string[]){    
             let low = 0;
             let mid1 = 0;
             let mid2 = a.length-1;
             let high = a.length-1;
    
            while(mid1 <= mid2){
                 switch(a[mid1]){
    
                     case '0':
    
                            [a[mid1],a[low]] = [a[low],a[mid1]];
                            mid1++;
                            low++;
                            break;
    
                     case '1':mid1++;break;
    
                     case '2':
                     case '3':[a[mid1],a[mid2]] = [a[mid2],a[mid1]];
                                mid2--;
                                break;
    
                     }
             }
        //sort 2 and 3
             while(mid1 <= high){
              switch(a[mid1]){
                     case '2': mid1++; break;
                     case '3': [a[mid1],a[high]] = [a[high],a[mid1]]
                                high--;
                                break;
    
               }
            }
    
    }
    

提交回复
热议问题