LeetCode Algorithm 0046 - 0050
文章目录
0046 - Permutations (Medium)
Problem Link: https://leetcode.com/problems/permutations/description/
Description
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
Solution C++
#pragma once
#include "pch.h"
// Problem: https://leetcode.com/problems/permutations/description/
namespace P46Permutations
{
class Solution
{
public:
vector<vector<int>> permute(vector<int>& nums)
{
if (nums.empty())
{
return vector<vector<int>>();
}
#if false // STL 有内置函数 `next_permutation` 可以轻松获取全排列,但题目显然不是想直接使用
vector<vector<int>> results;
sort(nums.begin(), nums.end());
do
{
results.push_back(nums);
} while (next_permutation(nums.begin(), nums.end()));
return results;
#endif
// 递归 Recursive
vector<vector<int>> results;
Permuting(nums, 0, results);
return results;
}
private:
void Permuting(vector<int>& nums, int startIndex, vector<vector<int>>& results)
{
if (startIndex == nums.size())
{
results.push_back(nums);
return;
}
for (int i = startIndex; i < nums.size(); i++)
{
swap(nums[startIndex], nums[i]);
Permuting(nums, startIndex + 1, results);
swap(nums[startIndex], nums[i]);
}
}
};
}
0047 - Permutations II (Medium)
Problem Link: https://leetcode.com/problems/permutations-ii/description/
Description
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
Solution C++
#pragma once
#include "pch.h"
// Problem: https://leetcode.com/problems/permutations-ii/description/
namespace P47PermutationsII
{
class Solution
{
public:
vector<vector<int>> permuteUnique(vector<int>& nums)
{
if (nums.empty())
{
return vector<vector<int>>();
}
#if false // STL 有内置函数 `next_permutation` 可以轻松获取全排列,但题目显然不是想直接使用
vector<vector<int>> results;
sort(nums.begin(), nums.end());
do
{
results.push_back(nums);
} while (next_permutation(nums.begin(), nums.end()));
return results;
#endif
// 递归 Recursive
vector<vector<int>> results;
vector<int> result;
vector<bool> used = vector<bool>(nums.size(), false);
sort(nums.begin(), nums.end());
Permuting(nums, results, result, used);
return results;
}
private:
void Permuting(vector<int>& nums, vector<vector<int>>& results, vector<int>& result, vector<bool>& used)
{
if (result.size() == nums.size())
{
results.push_back(result);
return;
}
for (int i = 0; i < nums.size(); i++)
{
if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]))
{
continue;
}
used[i] = true;
result.push_back(nums[i]);
Permuting(nums, results, result, used);
result.pop_back();
used[i] = false;
}
}
};
}
0048 - Rotate Image (Medium)
Problem Link: https://leetcode.com/problems/rotate-image/description/
Description
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
Solution C++
#pragma once
#include "pch.h"
// Problem: https://leetcode.com/problems/rotate-image/description/
namespace P48RotateImage
{
class Solution
{
public:
void rotate(vector<vector<int>>& matrix)
{
// 顺时针旋转90度
// 要求不能使用另外一个矩阵作为媒介
// 不用内置的 reverse
if (matrix.empty())
{
return;
}
// 1 2 3
// 4 5 6
// 7 8 9
int side = matrix.size();
for (int r = 0; r < side; r++)
{
// 对角线翻转
// 1 4 7
// 2 5 8
// 3 6 9
for (int c = r + 1; c < side; c++)
{
swap(matrix[r][c], matrix[c][r]);
}
}
for (int r = 0; r < side; r++)
{
// 水平翻转
// 7 4 1
// 8 5 2
// 9 6 3
// reverse(matrix[r].begin(), matrix[r].end());
for (int c = 0; c < (side + 1) / 2; c++)
{
swap(matrix[r][c], matrix[r][side - 1 - c]);
}
}
}
};
}
0049 - Group Anagrams (Medium)
Problem Link: https://leetcode.com/problems/group-anagrams/description/
Description
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
-
All inputs will be in lowercase.
-
The order of your output does not matter.
Solution C++
#pragma once
#include "pch.h"
// Problem: https://leetcode.com/problems/group-anagrams/description/
namespace P49GroupAnagrams
{
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
unordered_map<string, vector<string>> map;
for (int i = 0; i < strs.size(); i++)
{
string str = strs[i];
sort(str.begin(), str.end());
map[str].push_back(strs[i]);
}
vector<vector<string>> results;
for (pair<string, vector<string>> p : map)
{
results.push_back(p.second);
}
return results;
}
};
}
0050 - Pow(x, n) (Medium)
Problem Link: https://leetcode.com/problems/powx-n/description/
Description
Implement pow( x, n ), which calculates x raised to the power n (xn).
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2^(-2) = 1/(2^2) = 1/4 = 0.25
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
Solution C++
#pragma once
#include "pch.h"
// Problem: https://leetcode.com/problems/powx-n/description/
namespace P50PowXN
{
class Solution
{
public:
double myPow(double x, int n)
{
if (x == 0.0)
{
return 0.0;
}
if (x == 1.0 || n == 0.0)
{
return 1.0;
}
if (x == -1.0)
{
return n % 2 == 0 ? 1.0 : -1.0;
}
long long times = n;
if (times < 0)
{
x = 1.0 / x;
times = -times;
}
double result = x;
// 能用循环,尽量不用递归
#if false // 方法一,单循环,时间 O(abs(n))
for (long long i = 1; i < times; i++)
{
result *= x;
if ((x > 0 && x < 1.0 && result <= numeric_limits<double>::min())
|| (x < 0 && x > -1.0 && result >= -numeric_limits<double>::min()))
{
break;
}
}
return times < 0 ? 1.0 / result : result;
#endif
#if true // 方法二,震荡
for (long long it1 = 1, it2 = 1; it1 != times; )
{
double tmp = x;
it2 = 1;
// result * x * x^2 * x^4 ...
for (; it1 < times; it1 += it2, it2 *= 2)
{
result *= tmp;
tmp *= tmp;
}
tmp = x;
it2 = 1;
// result / x / x^2 / x^4 ...
for (; times < it1; it1 -= it2, it2 *= 2)
{
result /= tmp;
tmp *= tmp;
}
}
return result;
#endif
}
};
}