classSolution(object): deflargestInteger(self, num): """ :type num: int :rtype: int """
nums = list(str(num))
tmp = [0for i inrange(len(nums))] # 记录角标奇偶性 oushu = [] jishu = [] for i inrange(len(nums)): ifint(nums[i]) % 2 == 0: tmp[i] = 0 oushu.append(int(nums[i])) else: tmp[i] = 1 jishu.append(int(nums[i]))
# 贪心 k = 0 jishu.sort(reverse=True)
j = 0 oushu.sort(reverse=True)
res = [] for i inrange(len(nums)): if tmp[i] == 0: res.append(oushu[j]) j += 1 else: res.append(jishu[k]) k += 1 res = [str(item) for item in res] returnint(''.join(res))
for i inrange(len(left_list)): # i代表插在前边 for j inrange(len(right_list)): # j代表插在后边 # if i != 0 and j != len(right_list) - 1:
pre = int(''.join(left_list[:i])) iflen(''.join(left_list[:i])) else1 inter = (int(''.join(left_list[i:])) + int(''.join(right_list[:j+1]))) last = int(''.join(right_list[j+1:])) iflen(''.join(right_list[j+1:])) else1
tmp_res = pre * inter * last
if tmp_res < min_res: min_res = tmp_res left_res = i right_res = j
if tmp_idx == -1: # 把剩余的k均匀展开到各个地方 bias = k // len(nums) # 每个能增加几 k -= bias * len(nums) # print((nums[0] + bias + 1)**k) # print((nums[0] + bias)**(len(nums) - k))
# res = ((nums[-1] + bias + 1)**k) * ((nums[-1] + bias)**(len(nums) - k)) # 下边两行替代 res = 1 a1 = (nums[-1] + bias + 1) a2 = (nums[-1] + bias) for kl inrange(k): res = ((res % c) * (a1 % c)) % c for kl inrange(len(nums) - k): res = ((res % c) * (a2 % c)) % c
else: # print("nums: ", nums) # print("nums[tmp_idx+1:]: ", nums[tmp_idx+1:]) res = 1 for i inrange(tmp_idx+1, len(nums)): res = ((res%c) * (nums[i]%c)) % c