阿里在线面试(最大字符环问题)

class Solution(object):
    def number_get(self, numbers):
        '''
        :param numbers:list[str]
        :return:
        '''
        length_number = 0
        for i in range(len(numbers)):
            if numbers[i] < numbers[i][::-1]:
                numbers[i] = numbers[i][::-1]
            length_number = max(length_number, len(numbers[i]))
        numbers = sorted(numbers,cmp=self.comparator)
        number_index = 0
        index = 0
        left = numbers[0]
        right = ""
        result = left + "".join(numbers[:index] + numbers[index + 1:]) + right
        while number_index < length_number:
            for i in range(len(numbers)):
                if numbers[i][number_index:] >= left[:len(numbers[i][number_index:])]:
                    #     left = numbers[i][number_index:]
                    #     right = numbers[i][:number_index]
                    #     index = i
                    #     result = left+"".join(numbers[:index]+numbers[index+1:])+right
                    # elif numbers[i][number_index:]==left[:len(numbers[i][number_index:])]:
                    test_left = numbers[i][number_index:]
                    test_right = numbers[i][:number_index]
                    now = test_left + "".join(numbers[:i] + numbers[i + 1:]) + test_right
                    if now > result:
                        left = test_left
                        right = test_right
                        index = i
                        result = now

                test_number = numbers[i][::-1]
                if test_number[number_index:] >= left[:len(numbers[i][number_index:])]:
                    test_left = test_number[number_index:]
                    test_right = test_number[:number_index]
                    now = test_left + "".join(numbers[:i] + numbers[i + 1:]) + test_right
                    if now > result:
                        left = test_left
                        right = test_right
                        index = i
                        result = now

                print index, numbers[index], result
            number_index += 1
        return result




    def comparator(self,a,b):
        if a+breturn 1
        return -1



test = Solution()
print test.number_get(["876","813","745"])

你可能感兴趣的:(python日常,LeetCode)