力扣题-12.13
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:442. 数组中重复的数据
解题思想:直接相除即可
class Solution(object):def optimalDivision(self, nums):""":type nums: List[int]:rtype: str"""if len(nums) == 1:return str(nums[0])if len(nums) == 2:return str(nums[0]) + "/" + str(nums[1])return str(nums[0]) + "/(" + "/".join(map(str, nums[1:])) + ")"
class Solution {
public:string optimalDivision(vector<int>& nums) {if (nums.size() == 1) {return std::to_string(nums[0]);}if (nums.size() == 2) {return std::to_string(nums[0]) + "/" + std::to_string(nums[1]);}std::ostringstream result;result << nums[0] << "/(";for (int i = 1; i < nums.size(); ++i) {result << nums[i];if (i != nums.size() - 1) {result << "/";}}result << ")";return result.str();}
};