leetcode +找到所有出现两次的数字,标记。很巧妙

点击打开链接
class Solution {
public:
    vector findDuplicates(vector& nums) {
        vector result;
        int index;
        for (int i = 0;i 0) {
                nums[index] = -nums[index];
            } else {
                result.push_back(index + 1);
            }
        }
        return result;
    }
};

int main()
{
    Solution s1;
    vector tmp;
    tmp.push_back(4), tmp.push_back(3), tmp.push_back(2), tmp.push_back(7);
    tmp.push_back(8), tmp.push_back(2), tmp.push_back(3), tmp.push_back(1);
    s1.findDuplicates(tmp);
    return 0;
}

你可能感兴趣的:(Leetcode)