2578. Split With Minimum Sum 發表於 2023-03-19 | 分類於 leetcode problemsolutionC++ solution12345678910111213141516171819class Solution {public: int splitNum(int num) { int ret = 0; vector<int> nums; while(num ){ nums.push_back(num%10); num/=10; } sort(nums.begin(), nums.end()); if(nums.size()%2!=0) nums.insert(nums.begin(), 0); int n = nums.size(); for(int i=0;i< n;++i) { if(i%2==0) ret*=10; ret+=nums[i]; } return ret; }}; python solution1234567891011121314151617class Solution: def splitNum(self, num: int) -> int: nums = list() ret = 0 while(num): nums.append(num%10) num//=10 nums.sort() if(len(nums)%2!=0): nums = [0] + nums for i, v in enumerate(nums): if(i%2==0): ret*=10 ret+=v return ret analysis time complexity O(nlogn) space complexity O(n)