【Rotate Image】cpp

题目

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

代码

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
            const unsigned int len = matrix.size();
            if ( len < 2 ) return;
            for ( int row = 0; row < len; ++row)
            {
                for (int col = 0; col < len-1-row; ++col)
                {
                    std::swap(matrix[row][col], matrix[len-1-col][len-1-row]);
                }
            }
            for ( int row = 0; row < len/2; ++row)
            {
                for ( int col = 0; col < len; ++col)
                {
                    std::swap(matrix[row][col], matrix[len-1-row][col]);
                }
            }
    }
};

Tips:

1. 算法:先沿着副对角线(右上到左下)swap元素;再沿着水平中轴线swap元素

2. 注意循环遍历时的结束条件,不要做无谓的遍历

时间: 2024-08-01 01:32:56

【Rotate Image】cpp的相关文章

【Rotate List】cpp

题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 代码: /** * Definition for singly-linked list. * struct ListNode {

leetcode 【 Rotate Image 】python 实现

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 代码:oj测试通过 Runtime: 53 ms 1 class Solution: 2 # @param matrix, a list of lists of integers 3 # @return a list

leetcode 【Rotate List 】python 实现

题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 代码:oj在线测试通过 Runtime: 200 ms 1 # Definition for singly-linked lis

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, 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,2], a sol

【LRU Cache】cpp

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【Text Justification】cpp

题目: Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pa

【Simplify Path】cpp

题目: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case whe