【Leetcode】Rotate Image

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) {
        int n = matrix.size();

        for(int i = 0; i < n / 2; i++)
            for(int j = i; j < n - i - 1;j++)
            {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = temp;
            }
    }
};

代码二:

// LeetCode, Rotate Image
// 思路1,时间复杂度O(n^2),空间复杂度O(1)
class Solution {
public:
	void rotate(vector<vector<int>>& matrix) {

		const int n = matrix.size();

		for (int i = 0; i < n; ++i) // 沿着副对角线反转
			for (int j = 0; j < n - i; ++j)
				swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);

		for (int i = 0; i < n / 2; ++i) // 沿着水平中线反转
			for (int j = 0; j < n; ++j)
				swap(matrix[i][j], matrix[n - 1 - i][j]);
	}
};

【Leetcode】Rotate Image,布布扣,bubuko.com

时间: 2024-08-26 04:20:54

【Leetcode】Rotate Image的相关文章

【LeetCode】Rotate List

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. start with an example. Given [0,1,2], rotate 1 steps to the right -&

【LeetCode】 Rotate List 循环链表

题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a list, rotate the list to the right by k places, where k is non-negative. * 题目:循环移动链表,等价于将链表从右边数的k个节点移动到表的前方 * 思路:移动倒是简单,重点是要找到链表倒数的k个数,就等价于找到倒数第K+1个数,设置两个指针,

【leetcode】Rotate Image(middle)

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? 思路:我的思路,先沿对角线对称,再左右对称. void rotate(int **matrix, int n) { //先沿对角线对称 for(int i = 0; i < n; i++) { for(int j = 0;

【leetcode】Rotate List(middle)

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. 题目关键点:k可能比链表长度还大, 需要获取链表长度len, 用 k % len 获取具体需要移动的数字. class Solution

【LeetCode】Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->