Accepted 10ms c++ solution use backtracking, only 10 lines, easy understand.
The characteristics of C++ reference is an outstanding tool for backtracking algorithm!\n\nlet us use [1,2,3,4] as an example to explain my solution:\n\n    sub
**The characteristics of C++ reference is an outstanding tool for backtracking algorithm!**\n\nlet us use [1,2,3,4] as an example to explain my solution:\n\n    subsets([1,2,3,4]) = []\n                         // push(1)\n                         [1, subsets([2,3,4])] // if push N times in subsets([2,3,4]), the pop times is also N, so vec is also [1] after backtrack.\n                         // pop(), push(2)\n                         [2, subsets([3,4])]\n                         // pop(), push(3)\n                         [3, subsets([4])]\n                         // pop(), push(4)\n                         [4, subsets([])]\n                         // pop()\n\nAccepted 10ms c++ solution use backtracking for [Subsets ][1]\n\n    class Solution {\n    public:\n        std::vector<std::vector<int> > subsets(std::vector<int> &nums) {\n    \t\tstd::sort(nums.begin(), nums.end());\n            std::vector<std::vector<int> > res;\n    \t\tstd::vector<int> vec;\n    \t\tsubsets(res, nums, vec, 0);\n    \t\treturn res;\n        }\n    private:\n    \tvoid subsets(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) {\n    \t\tres.push_back(vec);\n    \t\tfor (int i = begin; i != nums.size(); ++i) {\n    \t\t\tvec.push_back(nums[i]);\n    \t\t\tsubsets(res, nums, vec, i + 1);\n    \t\t\tvec.pop_back();\n    \t\t}\n    \t}\n    };\n\n\nAccepted 10ms c++ solution use backtracking for [Subsets II][2]\n\n    class Solution {\n    public:\n        std::vector<std::vector<int> > subsetsWithDup(std::vector<int> &nums) {\n    \t\tstd::sort(nums.begin(), nums.end());\n            std::vector<std::vector<int> > res;\n    \t\tstd::vector<int> vec;\n    \t\tsubsetsWithDup(res, nums, vec, 0);\n    \t\treturn res;\n        }\n    private:\n    \tvoid subsetsWithDup(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) {\n    \t\tres.push_back(vec);\n    \t\tfor (int i = begin; i != nums.size(); ++i)\n    \t\t\tif (i == begin || nums[i] != nums[i - 1]) { \n    \t\t\t\tvec.push_back(nums[i]);\n    \t\t\t\tsubsetsWithDup(res, nums, vec, i + 1);\n    \t\t\t\tvec.pop_back();\n    \t\t\t}\n    \t}\n    };\n\n\n  [1]: https://leetcode.com/problems/subsets/\n  [2]: https://leetcode.com/problems/subsets-ii/
['Backtracking']