LeetCode的medium题集合(C++实现)七

1 Rotate Image

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).

对于 n x n 的矩阵按顺时针旋转90度,相当于先将矩阵上下翻转,然后将矩阵装置。

void rotate(vector<vector<int>>& matrix) {
        if (matrix.empty()) return;
        int len=matrix.size(),temp;
        for(int i=0;i<len/2;i++)
        {
            for(int j=0;j<len;j++)
            {
                temp=matrix[len-1-i][j];
                matrix[len-1-i][j]=matrix[i][j];
                matrix[i][j]=temp;
            }
        }
        for(int i=0;i<len;i++)
        {
            for(int j=0;j<i;j++)
            {
               temp=matrix[i][j];
               matrix[i][j]=matrix[j][i];
               matrix[j][i]=temp;
            }
        }
        return;
    }

对于长宽不等的图像旋转任意角度,常规方法是利用点的旋转变换公式:

[x′;y′]=[cos(t)sin(t);?sin(t)cos(t)][x;y]. 变换后图片中的每个像素点需要平移到相对旋转中心的新坐标,计算完成之后,需要再次还原到相对左上角原点的旧坐标。

vector<vector<int>> Rotate(vector<vector<int>>& matrix, float angle)
{
  int len = (int)(sqrt(pow(matrix.size(), 2) + pow(matrix[0].size(), 2)) + 0.5);

  vector<vector<int> > retMat(len,vector<int>(len,0);
  float anglePI = angle * CV_PI / 180;
  int xSm, ySm;

  for(int i = 0; i < retMat.size(); i++)
    for(int j = 0; j < retMat.size(); j++)
    {
      xSm = (int)((i-retMat.size()/2)*cos(anglePI) - (j-retMat..size()/2)*sin(anglePI) + 0.5);
      ySm = (int)((i-retMat.size()/2)*sin(anglePI) + (j-retMat..size()/2)*cos(anglePI) + 0.5);
      xSm += matrix.size() / 2;
      ySm += matrix[0].size() / 2;

      if(xSm >= matrix.size() || ySm >= matrix[0].size() || xSm <= 0 || ySm <= 0){
        retMat[i, j] = Vec3b(0, 0);
      }
      else{
        retMat[i, j] = matrix[xSm, ySm];
      }
    }

  return retMat;
}

2 Anagrams

Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.

anagrams指的是字符相同只是顺序不同的字符串。我们先对每一个字符串排序,将排序后的字符串作为字典的key值,将排序前的字符串做为value值。然后遍历字典,如果value值大于1怎构成anagrams。

vector<string> anagrams(vector<string>& strs) {
        vector<string> ret;
        map<string,vector<string> >m;
        for(int i = 0 ; i < strs.size() ; i++)
        {
            string tmp=strs[i];
            sort(tmp.begin(),tmp.end());
            m[tmp].push_back(strs[i]);
        }
        for(map<string,vector<string> >::iterator it = m.begin(); it != m.end(); it++)
        {
            if ( (it->second).size() > 1 )
            {
                for(vector<string>::iterator it1 = (it->second).begin(); it1!= (it->second).end(); it1++)
                    ret.push_back(*it1);
            }
        }
        return ret;
    }
时间: 2024-08-01 16:44:13

LeetCode的medium题集合(C++实现)七的相关文章

LeetCode的medium题集合(C++实现)十一

1 Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

LeetCode的medium题集合(C++实现)五

1 Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. 可以采用递归的方法解决这个问题,当找到一组数之和

LeetCode的medium题集合(C++实现)六

1 Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative. 该题实际就是利用字符串来解决大数的乘法问题.为了计算方便先将两组数翻转,将字符串转化为整数利用两个循环逐位相乘,将结果保存.然后逐位解决进位问题. s

LeetCode的medium题集合(C++实现)四

1 Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending or

LeetCode的medium题集合(C++实现)十五

1 Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1],

LeetCode的medium题集合(C++实现)九

1 Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index. 利用两指针法

LeetCode的medium题集合(C++实现)八

1 Pow(x, n) 该题采用二分法进行递归 double myPow(double x, int n) { if(n==0) return 1; if(n<0) { n=(-n); x=1/x; } double res=myPow(x,n/2); if(n%2==0) { return res*res; } else { return res*res*x; } } 2 Maximum Subarray Find the contiguous subarray within an array

LeetCode的medium题集合(C++实现)十

1 Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations.Given n and k, return the kth permutation sequence. 使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制.下面采用数学法: 在n!个排列中,第一位元素相同的排列总是有(n?1)!个,如果p=k/(n?1)!,那么排列的第一位元素

LeetCode的medium题集合(C++实现)十七

1 Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example: Give