leetcode(46-50)

46. 全排列

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        dfs(0,nums,res);
        return res;
    }
    void dfs(int cur,vector<int> &nums,vector<vector<int>> &res){
        if(cur == nums.size()-1){
            res.push_back(nums);
            return;
        }
        for(int i=cur;i<nums.size();i++){
            swap(nums[cur], nums[i]);
            dfs(cur+1,nums,res);
            swap(nums[cur], nums[i]);
        }
    }
};

 47. 全排列 II

给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

输入: [1,1,2]
输出:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> tmp;
        vector<bool> index(nums.size(), true);
        sort(nums.begin(),nums.end());
        dfs(res,tmp,nums,index);
        return res;
    }
    
    void dfs(vector<vector<int>> &res, vector<int> &tmp, vector<int> &nums, vector<bool> &index){
        if(tmp.size() == nums.size()){
            res.push_back(tmp);
            return;
        }
        for(int i=0; i<nums.size(); i++){
            if(index[i] == false) continue;
            if(i>0 && nums[i]==nums[i-1] && index[i-1]==true) continue;
            tmp.push_back(nums[i]);
            index[i]=false;
            dfs(res,tmp,nums,index);
            tmp.pop_back();
            index[i]=true;
        }
    }
};

48. 旋转图像

给定一个 × n 的二维矩阵表示一个图像。

将图像顺时针旋转 90 度。

说明:

你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

示例 1:

给定 matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

原地旋转输入矩阵,使其变为:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

示例 2:

给定 matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

原地旋转输入矩阵,使其变为:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        for(int i=0; i<matrix.size(); i++){
            for(int j=i; j<matrix[0].size(); j++){
                swap(matrix[i][j],matrix[j][i]);
            }
        }
        for(int i=0; i<matrix.size(); i++){
            for(int j=0; j<matrix.size()/2; j++){
                swap(matrix[i][j],matrix[i][matrix.size()-1-j]);
            }
        }
    }
};

49. 字母异位词分组

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:

输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

说明:

  • 所有输入均为小写字母。
  • 不考虑答案输出的顺序。
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        if(strs.size() <= 0) return res;
        unordered_map<string,vector<string>> hash;
        for(auto str:strs){
            string st=str;
            sort(st.begin(),st.end());
            hash[st].push_back(str);
        }
        for(auto s:hash){
            res.push_back(s.second);
        }
        return res; 
    }
};

50. Pow(x, n)

实现 pow(xn) ,即计算 x 的 n 次幂函数。

示例 1:

输入: 2.00000, 10
输出: 1024.00000

示例 2:

输入: 2.10000, 3
输出: 9.26100

示例 3:

输入: 2.00000, -2
输出: 0.25000
解释: 2-2 = 1/22 = 1/4 = 0.25

说明:

  • -100.0 < x < 100.0
  • n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。
class Solution {
public:
    double myPow(double x, int n) {
        if(n==0) return 1;
        if(n<0 && n>INT_MIN){
            n=-n;
            x=1/x;
        }
        return (n%2==0) ? pow(x*x, n/2) : x*pow(x*x, n/2);
    }
};

(以上题目均摘自leetcode)

猜你喜欢

转载自blog.csdn.net/github_37002236/article/details/83117448
今日推荐