当前位置:网站首页>Leetcode question brushing (10) - sequential question brushing 46 to 50
Leetcode question brushing (10) - sequential question brushing 46 to 50
2022-07-02 02:22:00 【CAccept】
List of articles
46. Full Permutation
Given a No duplicate numbers
Array of nums , Back to its All possible permutations . You can In any order
Return to the answer .
Example 1:
Input :nums = [1,2,3]
Output :[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input :nums = [0,1]
Output :[[0,1],[1,0]]
Example 3:
Input :nums = [1]
Output :[[1]]
Tips :
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums All integers in Different from each other
analysis : This question is very simple dfs problem , And the elements in the array are different , We can choose to put different numbers in each position to form a tree structure to achieve full arrangement .
Code :
class Solution {
public:
vector<vector<int>>res;
vector<int>path;
bool st[7];
vector<vector<int>> permute(vector<int>& nums) {
dfs(nums,0);
return res;
}
void dfs(vector<int>nums,int u)
{
// It means that the positions are full , You can put the answer in res It's in
if( u == nums.size())
{
res.push_back(path);
return;
}
for(int i=0; i<nums.size(); i++)
{
if(!st[i])// If this number has not been used
{
st[i]=true;
path.push_back(nums[i]);
dfs(nums,u+1);
// Restore the scene
path.pop_back();
st[i]=false;
}
}
}
};
47. Full Permutation I I
Given a Can contain duplicate numbers
Sequence nums , In any order Returns all non repeating permutations .
Example 1:
Input :nums = [1,1,2]
Output :
[[1,1,2],
[1,2,1],
[2,1,1]]
Example 2:
Input :nums = [1,2,3]
Output :[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Tips :
1 <= nums.length <= 8
-10 <= nums[i] <= 10
analysis : This question is related to 46 The difference between questions is that they contain the same elements , If and 46 Using the same method will produce many repeated results , This is wrong , Since the same elements are included, we can specify the order of use of the same elements ( For example, the order to be arranged is 1 1 1 2, You can see that there are three "1", We can stipulate that there is only the first "1" Run out of , the second "1" Just can use , Empathy , Only when the second "1" Used up the third "1" Just can use ), In this way, we can avoid the repetition of tone .
Code :
class Solution {
public:
vector<int>path;
vector<vector<int>>res;
vector<bool>st;
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(),nums.end());
st = vector<bool>(nums.size());
dfs(nums,0);
return res;
}
void dfs(vector<int>& nums,int u)
{
if( u == nums.size())
{
res.push_back(path);
return;
}
for(int i = 0; i < nums.size(); i++)
{
// If nums[i] Not yet used
if(!st[i])
{
// Realize the operation of sequential use
if(i&&nums[i]==nums[i-1]&&!st[i-1])continue;
path.push_back(nums[i]);
st[i]=true;
dfs( nums, u+1);
st[i]=false;
path.pop_back();
}
}
}
};
48. Rotated image
Given a n × n Two dimensional matrix of matrix Represents an image . Please rotate the image clockwise 90 degree .
You must be there. In situ Rotated image , This means that you need to modify the input two-dimensional matrix directly . Please do not Use another matrix to rotate the image .
Example 1:
Input :matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output :[[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input :matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output :[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Tips :
n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
analysis : This question is difficult to say, but it is also difficult , But if I happen to think of it, I will actually do it ( It's kind of like a brain teaser ), Spin to the right 90° Can be broken down into Exchange positions symmetrically along the diagonal on the right + Right and left symmetry
Code :
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
// Right diagonal swap
for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
{
swap(matrix[i][j],matrix[j][i]);
}
}
// Left-right exchange
for(int i = 0; i < n; i++)
{
for(int j = 0 , k = n - 1; j < k; j++,k--)
{
swap(matrix[i][j],matrix[i][k]);
}
}
}
};
49. Letter heterotopic word grouping
Here's an array of strings , Would you please Letter heterotopic word Put together . You can return a list of results in any order . Letter heterotopic word
Is a new word obtained by rearranging the letters of the source word , Letters in all source words are usually used just once .
Example 1:
Input : strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output : [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input : strs = [""]
Output : [[""]]
Example 3:
Input : strs = ["a"]
Output : [["a"]]
Tips :
1 <= strs.length <= 104
0 <= strs[i].length <= 100
strs[i] Only lowercase letters
analysis : Find the common ground of the same group , That is, the composition of letters is the same , We can str Every... In the array string Sort them all ( Of course , We have to put the original str The elements in the array are saved ), Then we can judge whether it is in In the same group , Can pass hash Store and classify
, Finally, output the answer .
Code :
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string,vector<string>>mmap;
for(auto & item : strs){
string strTmp = item;
sort(item.begin(),item.end());// Sort
mmap[item].push_back(strTmp); // utilize hash To classify , Take the sorted elements as key
}
// Output the answer
vector<vector<string>>res;
for(auto & item : mmap){
res.push_back(item.second);
}
return res;
}
};
50. Pow(x, n)
Realization pow(x, n) , Computation x The integer of n Power function ( namely ,x^n ).
Example 1:
Input :x = 2.00000, n = 10
Output :1024.00000
Example 2:
Input :x = 2.10000, n = 3
Output :9.26100
Example 3:
Input :x = 2.00000, n = -2
Output :0.25000
explain :2^-2 = 1/2^2 = 1/4 = 0.25
Tips :
-100.0 < x < 100.0
-2^31 <= n <= 2^31-1
-10^4 <= x^n <= 10^4
analysis : This problem is a typical fast power problem , Here's the picture
Code :
class Solution {
public:
double myPow(double x, int n) {
bool is_minus = n < 0;
double res = 1;
for(long long k = abs(n); k > 0; k>>=1)
{
if(k & 1)// Bitwise AND
{
res *= x;
}
x *=x;
}
if(is_minus) return 1/res;
return res;
}
};
边栏推荐
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- OpenCASCADE7.6编译
- LFM信号加噪、时频分析、滤波
- AR增强现实可应用的场景
- JVM面试篇
- What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
- JVM interview
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- [learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
- 软件开发生命周期 --瀑布模型
猜你喜欢
Query word weight, search word weight calculation
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
A quick understanding of analog electricity
LFM signal denoising, time-frequency analysis, filtering
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
Software No.1
leetcode2312. 卖木头块(困难,周赛)
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决
JVM面试篇
随机推荐
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
What is the function of the headphone driver
Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
Vsocde has cli every time it is opened js
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Deep learning: a solution to over fitting in deep neural networks
Learning notes of software testing -- theoretical knowledge of software testing
Construction and maintenance of business websites [14]
leetcode373. Find and minimum k-pair numbers (medium)
Cesium dynamic diffusion point effect
2022 Q2 - 提昇技能的技巧總結
Webgpu (I): basic concepts
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
剑指 Offer 29. 顺时针打印矩阵
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Query word weight, search word weight calculation
The middle element and the rightmost element of the shutter
How to run oddish successfully from 0?
Design and implementation of key value storage engine based on LSM tree