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 2524
醉梦人生
醉梦人生 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:23

    Here is my solution in Python, using recursion (I had an assignment like this, where the array should be sorted relatively to a number K. If You put K = 0, You've got Your solution, without retaining the order of appearance):

    def kPart(s, k):
        if len(s) == 1:
            return s
        else:
            if s[0] > k:
                return kPart(s[1:], k) + [s[0]]
            else:
                return [s[0]] + kPart(s[1:], k)
    

提交回复
热议问题