<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rotate Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rotate/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rotate/</link>
	<description></description>
	<lastBuildDate>Fri, 30 Oct 2020 03:35:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>rotate Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rotate/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 61. Rotate List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 29 Oct 2020 05:59:44 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[rotate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7573</guid>

					<description><![CDATA[<p>Given a linked&#160;list, rotate the list to the right by&#160;k&#160;places, where&#160;k&#160;is non-negative. Example 1: Input: 1-&#62;2-&#62;3-&#62;4-&#62;5-&#62;NULL, k = 2 Output: 4-&#62;5-&#62;1-&#62;2-&#62;3-&#62;NULL Explanation: rotate 1 steps&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/">花花酱 LeetCode 61. Rotate List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 61. Rotate List - 刷题找工作 EP365" width="500" height="281" src="https://www.youtube.com/embed/a4XZu2VVE9Q?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a linked&nbsp;list, rotate the list to the right by&nbsp;<em>k</em>&nbsp;places, where&nbsp;<em>k</em>&nbsp;is non-negative.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, k = 2
<strong>Output:</strong> 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL
<strong>Explanation:</strong>
rotate 1 steps to the right: 5-&gt;1-&gt;2-&gt;3-&gt;4-&gt;NULL
rotate 2 steps to the right: 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 0-&gt;1-&gt;2-&gt;NULL, k = 4
<strong>Output:</strong> <code>2-&gt;0-&gt;1-&gt;NULL</code>
<strong>Explanation:</strong>
rotate 1 steps to the right: 2-&gt;0-&gt;1-&gt;NULL
rotate 2 steps to the right: 1-&gt;2-&gt;0-&gt;NULL
rotate 3 steps to the right:&nbsp;<code>0-&gt;1-&gt;2-&gt;NULL</code>
rotate 4 steps to the right:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL</code></pre>



<h2><strong>Solution: Find the prev of the new head</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365.png" alt="" class="wp-image-7576" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Step 1: Get the tail node T while counting the length of the list.<br>Step 2: k %= l, k can be greater than l, rotate k % l times has the same effect.<br>Step 3: Find the previous node P of the new head N by moving (l &#8211; k &#8211; 1) steps from head<br>Step 4: set P.next to null, T.next to head and return N</p>



<p>Time complexity: O(n) n is the length of the list<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode* rotateRight(ListNode* head, int k) {
    if (!head) return head;    
    int l = 1;
    ListNode* tail = head;
    while (tail-&gt;next) { tail = tail-&gt;next; ++l; }
    k %= l;
    if (k == 0) return head;
    
    ListNode* prev = head;
    while (--l &gt; k) prev = prev-&gt;next;
    ListNode* new_head = prev-&gt;next;
    tail-&gt;next = head;
    prev-&gt;next = nullptr;
    return new_head;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
  public ListNode rotateRight(ListNode head, int k) {
    if (head == null) return null;
    int l = 1;
    ListNode tail = head;
    while (tail.next != null) {
      tail = tail.next;
      ++l;
    }
    k %= l;
    if (k == 0) return head;
    
    ListNode prev = head;
    for (int i = 0; i &lt; l - k - 1; ++i) {
      prev = prev.next;
    }
    
    ListNode new_head = prev.next;
    prev.next = null;
    tail.next = head;
    return new_head;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def rotateRight(self, head: ListNode, k: int) -&gt; ListNode:
    if not head: return head
    tail = head
    l = 1
    while tail.next: 
      tail = tail.next
      l += 1
    k = k % l
    if k == 0: return head
    
    prev = head
    for _ in range(l - k - 1): prev = prev.next
    
    new_head = prev.next
    tail.next = head
    prev.next = None
    return new_head</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/">花花酱 LeetCode 61. Rotate List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 48. Rotate Image</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 02 Oct 2019 16:20:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[in place]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mirror]]></category>
		<category><![CDATA[O(n^2)]]></category>
		<category><![CDATA[rotate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5698</guid>

					<description><![CDATA[<p>You are given an&#160;n&#160;x&#160;n&#160;2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image&#160;in-place, which means you&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/">花花酱 LeetCode 48. Rotate Image</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an&nbsp;<em>n</em>&nbsp;x&nbsp;<em>n</em>&nbsp;2D matrix representing an image.</p>



<p>Rotate the image by 90 degrees (clockwise).</p>



<p><strong>Note:</strong></p>



<p>You have to rotate the image&nbsp;<a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noreferrer noopener"><strong>in-place</strong></a>, which means you have to modify the input 2D matrix directly.&nbsp;<strong>DO NOT</strong>&nbsp;allocate another 2D matrix and do the rotation.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <strong>input matrix</strong> = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix <strong>in-place</strong> such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <strong>input matrix</strong> =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix <strong>in-place</strong> such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]</pre>



<h2><strong>Solution: 2 Passes</strong></h2>



<p>First pass: mirror around diagonal <br>Second pass: mirror around y axis</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  void rotate(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int n = matrix.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        swap(matrix[i][j], matrix[j][i]);
    for (int i = 0; i &lt; n; ++i)
      reverse(begin(matrix[i]), end(matrix[i]));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/">花花酱 LeetCode 48. Rotate Image</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 396. Rotate Function</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-396-rotate-function/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-396-rotate-function/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 16 Mar 2018 16:48:53 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rotate]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2142</guid>

					<description><![CDATA[<p>Problem: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a &#8220;rotation function&#8221; F on A as&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-396-rotate-function/">花花酱 LeetCode 396. Rotate Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem:</h1>
<div class="question-description">
<div>
<p>Given an array of integers <code>A</code> and let <i>n</i> to be its length.</p>
<p>Assume <code>B<sub>k</sub></code> to be an array obtained by rotating the array <code>A</code> <i>k</i> positions clock-wise, we define a &#8220;rotation function&#8221; <code>F</code> on <code>A</code> as follow:</p>
<p><code>F(k) = 0 * B<sub>k</sub>[0] + 1 * B<sub>k</sub>[1] + ... + (n-1) * B<sub>k</sub>[n-1]</code>.</p>
<p>Calculate the maximum value of <code>F(0), F(1), ..., F(n-1)</code>.</p>
<p><b>Note:</b><br />
<i>n</i> is guaranteed to be less than 10<sup>5</sup>.</p>
<p><b>Example:</b></p>
<pre class="crayon:false ">A = [4, 3, 2, 6]

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26

So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
</pre>
</div>
</div>
<h1>Solution 1: Brute Force</h1>
<p>Time complexity: O(n^2) TLE</p>
<p>Space complexity: O(1)</p>
<h1>Solution 2: Math</h1>
<pre class="crayon:false  " style="font-size: 10px;">F(K)          =               0A[K] + 1A[K+1] + 2A[K+2] + ... + (n-2)A[K-2] + (n-1)A[K-1]
F(K-1)        =     0A[K-1] + 1A[K] + 2A[K+1] + 3A[K+2] + ... + (n-1)A[K-2]
F(K) - F(K-1) = (n-1)A[K-1] - 1A[K] - 1A[K+1] - 1A[K+2] - ... -     1A[K-2]
              = (n-1)A[K-1] - (1A[K] + 1A[K+1] + ... + 1A[K-2] + 1A[K-1] - 1A[K-1])
              = nA[K-1] - sum(A)
compute F(0)
F(i)          = F(i-1) + nA[i-1] - sum(A)</pre>
<p>&nbsp;</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 11 ms
class Solution {
public:
  int maxRotateFunction(vector&lt;int&gt;&amp; A) {
    const int n = A.size();
    int sum = 0;
    int f = 0;
    for (int i = 0; i &lt; n; ++i) {
      sum += A[i];
      f += i * A[i];
    }
    int ans = f;
    for (int i = 0; i &lt; n - 1; ++i) {
      f = f + sum - n * A[n - i - 1];
      ans = max(ans, f);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-396-rotate-function/">花花酱 LeetCode 396. Rotate Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-396-rotate-function/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 796. Rotate String</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-796-rotate-string/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-796-rotate-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 11 Mar 2018 06:45:04 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[rotate]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2053</guid>

					<description><![CDATA[<p>题目大意：给你两个字符串A, B, 问能否通过旋转A得到B。 Problem: https://leetcode.com/problems/rotate-string/description/ We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-796-rotate-string/">花花酱 LeetCode 796. Rotate String</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>题目大意：给你两个字符串A, B, 问能否通过旋转A得到B。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/rotate-string/description/">https://leetcode.com/problems/rotate-string/description/</a></p>
<p>We are given two strings, <code>A</code> and <code>B</code>.</p>
<p>A <em>shift on <code>A</code></em> consists of taking string <code>A</code> and moving the leftmost character to the rightmost position. For example, if <code>A = 'abcde'</code>, then it will be <code>'bcdea'</code> after one shift on <code>A</code>. Return <code>True</code> if and only if <code>A</code> can become <code>B</code> after some number of shifts on <code>A</code>.</p><pre class="crayon-plain-tag">Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true

Example 2:
Input: A = 'abcde', B = 'abced'
Output: false</pre><p><strong>Note:</strong></p>
<ul>
<li><code>A</code> and <code>B</code> will have length at most <code>100</code>.</li>
</ul>
<p><strong>Solution 1: Brute Force</strong></p>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  bool rotateString(string A, string B) {
    if (A.length() != B.length()) return false;
    for (int i = 1; i &lt; A.length(); ++i)
      if (check(A, B, i)) return true;
    return false;
  }
private:
  bool check(const string&amp; A, const string&amp; B, int offset) {
    for (int i = 0; i &lt; A.length(); ++i)
      if (A[(i + offset) % A.length()] != B[i])
        return false;
    return true;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-796-rotate-string/">花花酱 LeetCode 796. Rotate String</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-796-rotate-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
