Given an array of positive and negative integers, re-arrange it so that you have positive integers on one end and negative integers on other

后端 未结 30 2538
醉梦人生
醉梦人生 2020-12-07 07:37

I recently came across a Microsoft Interview Question for Software Engineer.

Given an array of positive and negative integers, re-arrange it so that you

30条回答
  •  旧巷少年郎
    2020-12-07 08:34

    Here is a JavaScript implementation of qiwangcs's solution:

    function specialSort(A){
        let min = Number.MAX_SAFE_INTEGER, max = -Number.MAX_SAFE_INTEGER;
        for(let i=0; i max)
                max = A[i];
            if(A[i] < min)
                min = A[i];
        }
        //Change all values to Positive
        for(let i=0; i (-min))
                A[currPositiveIndex++] += (A[i]%newMax)*newMax;
        //Recover to original value 
        for(let i=0; i

提交回复
热议问题