<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4942509877707550975</id><updated>2012-02-16T06:23:54.189-05:00</updated><category term='Data structure'/><category term='Algorithm'/><category term='Math'/><category term='Interview questions'/><category term='Linux'/><category term='Computer vision'/><category term='Computer graphics'/><title type='text'>Miracle space</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-5172634778290440682</id><published>2012-01-15T11:28:00.000-05:00</published><updated>2012-01-15T11:28:54.554-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>Implement atoi</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;br /&gt;Implement atoi to convert a string to an integer.&lt;br /&gt;&lt;br /&gt;Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.&lt;br /&gt;&lt;br /&gt;Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.&lt;br /&gt;&lt;br /&gt;Requirements for atoi:&lt;br /&gt;The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.&lt;br /&gt;&lt;br /&gt;The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.&lt;br /&gt;&lt;br /&gt;If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.&lt;br /&gt;&lt;br /&gt;If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Used Java because it`s cleaner&lt;br /&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;public int atoi(String str) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Start typing your Java solution below&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // DO NOT write main() function&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String expression = str.trim();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(expression==null||expression.equals("")){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int sign = 1;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int index = 0;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(expression.charAt(0) == '-'){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sign = -1;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(expression.charAt(0) == '-' || expression.charAt(0) == '+'){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; index++;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(expression.length()==1){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int ret = 0;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(index &amp;lt; expression.length()){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; char c = expression.charAt(index);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(c &amp;lt; '0' || c &amp;gt; '9') break;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int value = c - '0';&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(sign &amp;gt; 0){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(ret &amp;gt; Integer.MAX_VALUE / 10) return Integer.MAX_VALUE;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }else{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(ret &amp;lt; Integer.MIN_VALUE / 10) return Integer.MIN_VALUE;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ret = ret*10;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(sign &amp;gt; 0){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(ret &amp;gt; Integer.MAX_VALUE - value) &amp;nbsp;return Integer.MAX_VALUE;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }else{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(ret &amp;lt; Integer.MIN_VALUE + value ) return Integer.MIN_VALUE;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ret += value * sign; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; index++;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return ret;&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-5172634778290440682?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/5172634778290440682/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2012/01/implement-atoi.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5172634778290440682'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5172634778290440682'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2012/01/implement-atoi.html' title='Implement atoi'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-3329474575845959563</id><published>2012-01-14T17:39:00.000-05:00</published><updated>2012-01-14T17:40:00.079-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>Palindrome Number</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;Determine whether an integer is a palindrome. Do this without extra space.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;Some hints:&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;Could negative integers be palindromes? (ie, -1)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;If you are thinking of converting the integer to string, note the restriction of using extra space.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;There is a more generic way of solving this problem.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: inherit; font-size: x-small;"&gt;&lt;span style="font-family: inherit; font-size: x-small;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; public boolean isPalindrome(int x) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(x &amp;lt; 0) return false;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int numOfDigits = 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int base = 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(x / base &amp;gt; 9) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; base *= 10;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ++numOfDigits;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int head, tail;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(int i = 0; i &amp;lt; numOfDigits / 2; ++i) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; head = x / base;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tail = x % 10;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(head != tail) return false;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x -= head * base;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x /= 10;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; base /= 100;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return true;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit; font-size: x-small;"&gt;&lt;span style="font-family: inherit; font-size: x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-3329474575845959563?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/3329474575845959563/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2012/01/palindrome-number.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/3329474575845959563'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/3329474575845959563'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2012/01/palindrome-number.html' title='Palindrome Number'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-1513822499910466900</id><published>2012-01-12T23:32:00.000-05:00</published><updated>2012-01-12T23:40:58.480-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>In-place array rotation</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;br /&gt;&lt;div style="font: normal normal normal 16px/normal Arial, sans-serif;"&gt;&lt;span style="font-family: inherit; font-size: small;"&gt;The idea that is commonly seen in skill testing interview questions is using three reversals to perform the rotations: rev(1,k)&amp;nbsp;rev(k+1,n-k)&amp;nbsp;rev(1,n). This leads us to the following code:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: inherit; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="code-wrapper"&gt;int reverse(struct baseType * a, int n)&amp;nbsp;&lt;span style="background-color: transparent;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent;"&gt;&amp;nbsp; &amp;nbsp; int i, j;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; j = n / 2;&lt;br /&gt;&amp;nbsp; &amp;nbsp; n--;&lt;br /&gt;&amp;nbsp; &amp;nbsp; for (i=0; i &amp;lt; j; i++)&amp;nbsp;&lt;span style="background-color: transparent;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; struct baseType tmp = a[i];&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a[i] = a[n-i];&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a[n-i] = tmp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int rotate(struct baseType a[], int n, int k)&amp;nbsp;&lt;span style="background-color: transparent;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent;"&gt;&amp;nbsp; &amp;nbsp; if(a == NULL || n &amp;lt;= 0)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return -__LINE__;&lt;br /&gt;&amp;nbsp; &amp;nbsp; if(k &amp;lt; 0 || k &amp;gt;= n)&lt;span style="background-color: transparent;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;k %= n;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (k &amp;lt; 0)&amp;nbsp;&lt;span style="background-color: transparent;"&gt;k += n;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; if (k == 0) return 0;&lt;br /&gt;&amp;nbsp; &amp;nbsp; reverse (a, k);&lt;br /&gt;&amp;nbsp; &amp;nbsp; reverse (a + k, n - k);&lt;br /&gt;&amp;nbsp; &amp;nbsp; reverse (a, n);&lt;br /&gt;&amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;&amp;nbsp;}&lt;/div&gt;&lt;div style="font: normal normal normal 16px/normal Arial, sans-serif;"&gt;&lt;br /&gt;&lt;span style="font-family: inherit; font-size: small;"&gt;&lt;br /&gt;This solution reads and writes each element twice, however, some quick performance testing on random arrays shows that this code is faster for very small sized baseTypes. The reason being that the reverse() is very good for memory access locality. However, for any larger sized baseTypes, the first code sequence given will win by the simple virtue of performing fewer operations.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-1513822499910466900?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/1513822499910466900/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2012/01/in-place-array-rotation.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/1513822499910466900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/1513822499910466900'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2012/01/in-place-array-rotation.html' title='In-place array rotation'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-4396729319077824849</id><published>2012-01-12T21:08:00.001-05:00</published><updated>2012-01-12T21:09:46.007-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>Rotating a 2D array of integers (matrix) by a given angle (+90, -90, +180, -180)</title><content type='html'>&lt;div&gt;&lt;u&gt;&lt;span style="background-color: white; color: #999999; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 14px; line-height: 21px;"&gt;This is posted by&amp;nbsp;&lt;/span&gt;&lt;span class="fn" style="background-color: white; color: #999999; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 14px; line-height: 21px;"&gt;&lt;a href="http://www.blogger.com/profile/08197834521620905502" rel="author" style="color: #771000;" title="author profile"&gt;Rajendra Kumar Uppal&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="post-body entry-content" id="post-body-3916936333076938749" style="color: #333333; font-size: 15px; line-height: 1.4; position: relative;"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;So, I came across this problem on a forum and before looking up, I tried to solve it. After 15 minutes of trying, I figured out that the solution is pretty easy and elegant which was messy otherwise if you are going to do it in an insane way. So, here we go:&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;span style="color: #990000;"&gt;Problem definition:&lt;/span&gt;&lt;/b&gt;&amp;nbsp;You are given a 2D square matrix, or 2D array of integers of size n (n rows and n columns), your output should be n by n 2D matrix rotated by a given angle, which could be +90, -90, +180, -180.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;i&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Example:&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Input &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Output&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;1 2 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7 4 1&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;4 5 6 &amp;nbsp; &amp;nbsp; Rotate by +90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8 5 2&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;7 8 9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9 6 3&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;If you go by solving it something like manually, swapping elements, its going to be messy,&amp;nbsp;tedious, and error prone in that there will be much more edge cases, test cases to handle. So, after trying on paper, I figured out following elegant solution to solve this in an easy manner.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="color: #990000; font-family: Verdana, sans-serif;"&gt;Rotate by +90 (clockwise once):&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Input: n by n matrix M, where n &amp;gt;= 2&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Algorithm:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 1:&lt;/i&gt;&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Transpose" style="color: #771000; text-decoration: none;"&gt;Transpose&lt;/a&gt;&amp;nbsp;M&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 2:&lt;/i&gt;&amp;nbsp;Reverse each row&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Dry run:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 1:&lt;/i&gt;&amp;nbsp;Transpose&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;M &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M'&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;1 2 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 4 7&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;4 5 6 &amp;nbsp; &amp;nbsp; &amp;nbsp;-- &amp;nbsp; &amp;nbsp; 2 5 8&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;7 8 9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 6 9&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 2:&lt;/i&gt;&amp;nbsp;Reverse each row&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;M' &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M''&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;1 4 7 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7 4 1&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;2 5 8 &amp;nbsp; &amp;nbsp; &amp;nbsp;-- &amp;nbsp; &amp;nbsp; 8 5 2&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;3 6 9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9 6 3&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;M'' is rotated form of M by +90 degree.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Pseudocode:&amp;nbsp;&lt;/b&gt;is here which is self explanatory and easily convertible to source code in a language of your choice.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;i&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Transpose&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; for i in [0, n)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for j in [0, n)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if ( i &amp;lt; j )&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; swap( M[i][j], M[j][i] )&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;i&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Reverse a row (rowidx)&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; start = 0&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; end = cols - 1&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; while ( start &amp;lt; end ) {&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; swap( M[rowidx][start], M[rowidx][end] )&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ++start&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; --end&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;i&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Rotate&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; Transpose&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; for i in [0, rows)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Reverse( i )&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;That was fair enough until only asked to rotate by +90 degree. If problem is further extended to be solved for&amp;nbsp;&lt;b&gt;any given angle&lt;/b&gt;, of course the rotation should make sense, for example, 47 degree is not a choice ;-). So, here are some elegant techniques (I'll be brief now for other angles, because for +90 degree I have elaborated the solution):&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="color: #990000; font-family: Verdana, sans-serif;"&gt;Rotation by -90 degree (anticlockwise once):&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 1&lt;/i&gt;: Transpose&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;i&gt;Step 2&lt;/i&gt;: Reverse each column&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;span style="color: #990000;"&gt;Rotation by +180 degree (clockwise twice):&lt;/span&gt;&amp;nbsp;&lt;/b&gt;Two methods follows&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;First:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Rotate input matrix +90 degree twice, if routine for which is available to you&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Second:&lt;/b&gt;&amp;nbsp;(You'll be amazed!)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Step 1: Reverse each row&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Step 2: Reverse each column&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;span style="color: #990000;"&gt;Rotation by -180 degree (anticlockwise twice):&lt;/span&gt;&lt;/b&gt;&amp;nbsp;Three(!!!) methods follows&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;First:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Rotate input matrix -90 degree twice, if routine for which is available to you&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Second:&lt;/b&gt;&amp;nbsp;(You'll be amazed again!)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Step 1: Reverse each column&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Step 2: Reverse each row&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Third:&lt;/b&gt;&amp;nbsp;(Aha!)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Because rotating a matrix +180 degree or -180 should produce same result. So you can rotate it by +180 degree using one of above methods.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; text-align: justify;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;span style="color: #990000;"&gt;Concluding note:&lt;/span&gt;&lt;/b&gt;&amp;nbsp;Above techniques are elegant and very simple and straightforward to implement. Try them for some input of your choice and rotate it in all angles. These techniques are tested with a working C program.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-4396729319077824849?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/4396729319077824849/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2012/01/rotating-2d-array-of-integers-matrix-by.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4396729319077824849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4396729319077824849'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2012/01/rotating-2d-array-of-integers-matrix-by.html' title='Rotating a 2D array of integers (matrix) by a given angle (+90, -90, +180, -180)'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-5455074248841836405</id><published>2011-12-25T19:34:00.000-05:00</published><updated>2011-12-25T19:38:41.500-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Linux'/><title type='text'>Learn Vim Progressively — 1st Level : Survive</title><content type='html'>&lt;u&gt;&lt;span style="color: #a64d79;"&gt;I`m now learning vim, and I found a very interesting article which talks about how to learn vim progressively in four steps. I just paste the whole article here to share with others.&lt;/span&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="sc"&gt;&lt;abbr title="Too long; didn't read"&gt;&lt;/abbr&gt;&lt;/span&gt;Want to learn vim (the best text editor known to human kind) the fastest way possible. I suggest you a way. Start by learning the minimal to survive, then integrate slowly all tricks.&lt;br /&gt;&lt;div class="intro"&gt;&lt;/div&gt;&lt;a href="http://www.vim.org/"&gt;Vim&lt;/a&gt; the Six Billion Dollar editor&lt;br /&gt;&lt;blockquote&gt;Better, Stronger, Faster.&lt;/blockquote&gt;Learn &lt;a href="http://www.vim.org/"&gt;vim&lt;/a&gt; and it will be your last text editor.There isn’t any better text editor I know.Hard to learn, but incredible to use.&lt;br /&gt;I suggest you to learn it in 4 steps:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Survive&lt;/li&gt;&lt;li&gt;Feel comfortable&lt;/li&gt;&lt;li&gt;Feel Better, Stronger, Faster&lt;/li&gt;&lt;li&gt;Use vim superpowers&lt;/li&gt;&lt;/ol&gt;By the end of this journey, you’ll become a vim superstar. &lt;br /&gt;But before we start, just a warning.Learning vim will be painful at first.It will take time.It will be a lot like playing a music instrument.Don’t expect to be more efficient with vim than with another editor in less than 3 days.In fact it will certainly take 2 weeks instead of 3 days.&lt;br /&gt;&lt;h2 id="st-level----survive"&gt;1&lt;sup&gt;st&lt;/sup&gt; Level – Survive&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;Install &lt;a href="http://www.vim.org/"&gt;vim&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Launch vim&lt;/li&gt;&lt;li&gt;DO NOTHING! Read.&lt;/li&gt;&lt;/ol&gt;In a standard editor, typing on the keyboard is enough to write something and see it on the screen.Not this time.Vim is in &lt;i&gt;Normal&lt;/i&gt; mode.Let’s get in &lt;i&gt;Insert&lt;/i&gt; mode.Type on the letter &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;i&lt;/code&gt;.&lt;br /&gt;You should feel a bit better.You can type letters like in a standard notepad.To get back in &lt;i&gt;Normal&lt;/i&gt; mode just tap the &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;ESC&lt;/code&gt; key.&lt;br /&gt;You know how to switch between &lt;i&gt;Insert&lt;/i&gt; and &lt;i&gt;Normal&lt;/i&gt; mode.And now, the list of command you can use in &lt;i&gt;Normal&lt;/i&gt; mode to survive:&lt;br /&gt;&lt;blockquote&gt;&lt;ul&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;i&lt;/code&gt; → &lt;i&gt;Insert&lt;/i&gt; mode. Type &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;ESC&lt;/code&gt; to return to Normal mode.&lt;/li&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;x&lt;/code&gt; → Delete the char under the cursor&lt;/li&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:wq&lt;/code&gt; → Save and Quit (&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:w&lt;/code&gt; save, &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:q&lt;/code&gt; quit)&lt;/li&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;dd&lt;/code&gt; → Delete (and copy) current line&lt;/li&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;p&lt;/code&gt; → Paste&lt;/li&gt;&lt;/ul&gt;Recommended:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;hjkl&lt;/code&gt; (highly recommended but not mandatory) →  basic cursor move (←↓↑→). Hint: &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;j&lt;/code&gt; look like a down arrow.&lt;/li&gt;&lt;li&gt;&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:help &amp;lt;command&amp;gt;&lt;/code&gt; → Show help about &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;&amp;lt;command&amp;gt;&lt;/code&gt;, you can start using &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:help&lt;/code&gt; without anything else.&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;Only 5 commands. This is very few to start.Once these command start to become natural (may be after a complete day), you should go on level 2.&lt;br /&gt;But before, just a little remark on &lt;i&gt;Normal mode&lt;/i&gt;.In standard editors, to copy you have to use the &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;Ctrl&lt;/code&gt; key (&lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;Ctrl-c&lt;/code&gt; generally).In fact, when you press &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;Ctrl&lt;/code&gt;, it is a bit like if all your key change meaning.With vim in Normal mode, it is a bit like if your &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;Ctrl&lt;/code&gt; key is always pushed down.&lt;br /&gt;A last word about notations: &lt;br /&gt;&lt;ul&gt;&lt;li&gt;instead of writing &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;Ctrl-λ&lt;/code&gt;, I’ll write &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;&amp;lt;C-λ&amp;gt;&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;command staring by &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:&lt;/code&gt; will must end by &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;&amp;lt;enter&amp;gt;&lt;/code&gt;. For example, when I write &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:q&lt;/code&gt; it means &lt;code style="border: 1px solid rgb(204, 204, 204); padding: 3px;"&gt;:q&amp;lt;enter&amp;gt;&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-5455074248841836405?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/5455074248841836405/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/learn-vim-progressively-1st-level.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5455074248841836405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5455074248841836405'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/learn-vim-progressively-1st-level.html' title='Learn Vim Progressively — 1st Level : Survive'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-4546083236195559639</id><published>2011-12-25T19:31:00.000-05:00</published><updated>2011-12-25T21:19:03.385-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Linux'/><title type='text'>Some Linux commands useful for manipulating text files</title><content type='html'>&lt;div style="font-family: inherit;"&gt;&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} &lt;/style&gt;I have learned some Linux utilities for manipulating text files these days. They are extremely handy which is far beyond my expectation. I should have learned them before I used Java to write those unresuable, verbose and error-prone codes to process texts a week ago.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;cat&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;This is the simplest tool in this category. The cat command copies its input to output unchanged (identity filter). When supplied a list of file names, it concatenates them onto stdout.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;2. head&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;Display the first few lines of a specified file. Particularly useful when the file is big and you wants to see only the header. You don`t have to open the whole file (which makes the system slow) using this command.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;3. tail&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;Displays the last part of a file, similar to head.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;4. cut&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;The cut command prints selected parts of input lines. It can select columns (assumes tab-separated input); can select a range of character positions; can also specify delimiter characters.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;5. sort&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;Sort each line of the file using either lexicographic &lt;/span&gt;&lt;span style="font-size: small;"&gt;or arithmetic order. It can use a key for comparison to sort the lines in delimiter seperated files.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;6. uniq&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;Remove or report adjacent duplicate lines&lt;b&gt;.&lt;/b&gt; Useful for finding duplicate and non-duplicate lines.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;7. wc&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;The word count utility, wc, counts the number of lines, characters or words. The line counting feature is my best favorite because it does not have to open the file.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;8. tr&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;span style="font-size: small;"&gt;Copies standard input to standard output with substitution or deletion of selected characters. It can be used to filter a range of characters in order to make certain conversions.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;9. grep&amp;nbsp;&lt;/span&gt;&lt;/b&gt; &lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;Search substrings that match the given regular expression, and print the line they reside in. Often used to search for the results we are interested in. &lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;b&gt;10. sed&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;More powerful tool compared to the above. It Looks for patterns one line at a time, like grep , but changes lines of the file. It`s a non-interactive text editor. Editing commands come in as a script. There is an interactive editor ed which accepts the same commands. It`s a A Unix filter which is the superset of previously mentioned tools, and it`s syntax is also used in VIM.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;Above are the most common used Linux text manipulating utilities. Although they are already very convenient individually, combining them using pipes can make them even more powerful and flexible. &lt;b&gt;If you are a linux admin or academic researcher, these are the commands you must know.&lt;/b&gt;&lt;/div&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-4546083236195559639?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/4546083236195559639/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/some-linux-commands-useful-for.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4546083236195559639'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4546083236195559639'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/some-linux-commands-useful-for.html' title='Some Linux commands useful for manipulating text files'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-2199183549411462251</id><published>2011-12-12T17:18:00.001-05:00</published><updated>2011-12-12T20:12:03.970-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>More array problems</title><content type='html'>&lt;style type="text/css"&gt;c.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} &lt;/style&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Today I summarize some array problems that an be solved using hash tables.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Remove duplicates in an array.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Before starting with this question, we should ask the interviewer what`s the type of elements in the array. If the answer is character, well, the only thing we need is a boolean array of size 256 (assuming the charset is ASCII). If the answer is integer or other larger types, we should use a hash table.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; &lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;We solve this question using two indecies, reader and writer. We iterate each element using reader and find its existence in hash table. If it does not exist, we insert an entry in hash table, copy the value indexed by reader to that of writer, and increment both indecies. Otherwise, we increment only the reader index.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;This solution costs O(n) time and O(n) space. If extra space is not allowed, we can use any in-place sorting algorithm (e.g. quick sort) to sort the array and then use similar reader and writer technique to remove duplicates. This will cost O(nlogn) time and O(1) space.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Decide if two strings are anagrams.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;If number of each character in two strings are the same, then they are anagrams. We can scan the first string and increment the number of occurence of each character using a hash table. Then we scan the second string, decrementing the counter of each encountered character. In the end, if all the entries in the hash table are zero, then the two strings are anagrams.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Another extremely simple solution is to sort two strings and compare if there are equal.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Find the intersection of two arrays.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;We assume that there is no duplicate in each array. We scan the first array and mark the occurence of each element. Then we scan the second array and find if each element can be found in hash table. If so, we append it to the result.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Given a number N, find pairs of number a1, a2 in an array such that a1 + a2 = N.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;We will scan the array for two passes. In the first pass, we calculate N - a1, where a1 is each element. We insert it into hash table which key is &lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;N - a1&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; and value is a1`s index. Then in the second pass, we use N - a2, where a2 is each element, to find the target index using the hash table.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-2199183549411462251?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/2199183549411462251/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/more-array-problems.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/2199183549411462251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/2199183549411462251'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/more-array-problems.html' title='More array problems'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-5278913070553957652</id><published>2011-12-10T19:06:00.001-05:00</published><updated>2011-12-10T19:50:30.852-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><category scheme='http://www.blogger.com/atom/ns#' term='Data structure'/><title type='text'>More linked list problems</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} &lt;/style&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;There are a lot of commonly asked linked list questions. They can range from simple to much more challenging. The following are some questions that can all be solved using two pointers ptr1 and ptr2 with different moving speed. They are relatively simple so I won`t post code here, but just the method:&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Find the middle element of a linked list.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Let ptr1 moves 1 step and ptr2 moves 2 steps&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; in each loop&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;. When ptr2 reaches the end, ptr1 is pointing the middle element.&amp;nbsp;&lt;/span&gt; &lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Find the nth last element of a linked list.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Let ptr2 move n steps ahead first, and then let two pointers move together. When ptr2 reaches the end, ptr1 is pointing the nth last element.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Two linked lists of different length intersect at one node. Find this node.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;We should first find the lengths of two linked lists by iterating through the whole list. Suppose the length of two lists are len1 and len2, len1 &amp;lt; len2, the difference d = len2 - len1. Now reset two pointers and let the pointer of longer list move d steps first. Then move two pointers simultaneously and compare the data of them. When data is equal, we find the node.&lt;/span&gt;&lt;br /&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Given a circular linked list, find node at the beginning of the loop.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Let N be the node we want to find. I&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;n each loop,&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; ptr1 moves 1 step and ptr2 moves 2 steps&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;, until they meet at one node. The distance from this node to N is equal to the distance from head node to N. Now we reset ptr1 to head node and begin to move two pointers simultaneously. When they meet again, N is found there.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-5278913070553957652?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/5278913070553957652/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/more-linked-list-problems.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5278913070553957652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/5278913070553957652'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/more-linked-list-problems.html' title='More linked list problems'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-7842165243366717236</id><published>2011-12-10T11:37:00.001-05:00</published><updated>2011-12-10T19:03:27.212-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>Maximum subsequence sum problem</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} .string {color: #A31515;}&lt;/style&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;The problem we’d like to solve can be stated as:&lt;/div&gt;&lt;blockquote style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;b&gt;Given a sequence of numbers, find the maximum sum of a contiguous subsequence of those numbers.&lt;/b&gt;&lt;/blockquote&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;One obvious solution is that we can simply enumerate every possible subsequence and calculate their sum. This will take O(n^2) time.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;There is a linear solution called Kadane`s algorithm. Following is the code:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&lt;span class="keyword"&gt;int&lt;/span&gt; max_sub_sum(&lt;span class="keyword"&gt;int&lt;/span&gt; arr[], &lt;span class="keyword"&gt;int&lt;/span&gt; len) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;int&lt;/span&gt; max_local = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;int&lt;/span&gt; max_global = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; i = 0; i &amp;lt; len; i++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;max_local += arr[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;if&lt;/span&gt;(max_local &amp;lt; 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;max_local = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;if&lt;/span&gt;(max_local &amp;gt; max_global)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;max_global = max_local;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;return&lt;/span&gt; max_global;&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;Why does it work? If max_local becomes negative, it will not contribute to the next subsequence by adding the next element. Actually the next element itself would be larger than the sum of it and max_local, so we can simply restart summing from here.&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;Note that this algorithm only works when there is at least one positive integer in the array.&lt;br /&gt;&lt;br /&gt;Some interviewers may ask this question with slight modification that the input array is a circular array. In this case, we can concatenate the input array with itself, and apply Kadane`s algorithm.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-7842165243366717236?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/7842165243366717236/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/maximum-subsequence-sum-problem.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/7842165243366717236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/7842165243366717236'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/maximum-subsequence-sum-problem.html' title='Maximum subsequence sum problem'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-2374467590518809235</id><published>2011-12-09T16:20:00.001-05:00</published><updated>2011-12-10T12:22:06.226-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Computer vision'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><title type='text'>Ellipse fitting and parameter conversion</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} &lt;/style&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;The third assignment of Computer Vision course involves an interesting problem. Given a large number of points in Cartesian coordinate system, how to find an ellipse that best fit these points. In &lt;span id="goog_1695101271"&gt;&lt;/span&gt;&lt;a href="http://cococubed.asu.edu/papers/conics/fitzgibbon_1999.pdf" target="_blank"&gt;this&lt;span id="goog_1695101272"&gt;&lt;/span&gt;&lt;/a&gt; paper the illustration and solution of this problem is presented.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;The theory and proofs are not trivial and I can`t understand all of them. However, it`s implementation is very simple. Below is a six-line Matlab implementation of the algorithm.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&lt;span class="keyword"&gt;function&lt;/span&gt; a = fit_ellipse(x, y)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; D = [x.*x x.*y y.*y x y ones(size(x))];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; S = D' * D;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; C(6, 6) = 0; C(1, 3) = -2; C(2, 2) = 1; C(3, 1) = -2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [gevec, geval] = eig(S, C);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [NegR, NegC] = find(geval &amp;lt; 0 &amp;amp; ~isinf(geval));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a = gevec(:, NegC);&lt;br /&gt;&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;The input of this function is two vectors containing x and y coordinates of the points, respectively. The output is the 6 parameters of implicit equation of the ellipse.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-4BTxF7S6JM4/TuKAuJ79-yI/AAAAAAAAAAU/DMQSDvmKt2M/s1600/ca5d856e1fbe0fdf05d99def75d82005.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="17" src="http://1.bp.blogspot.com/-4BTxF7S6JM4/TuKAuJ79-yI/AAAAAAAAAAU/DMQSDvmKt2M/s320/ca5d856e1fbe0fdf05d99def75d82005.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Our TA is very smart and he used a trick to simplify the problem for us to implement. So we can avoid solving the general eigenvector problem, which is not provided by &lt;a href="http://opencv.willowgarage.com/wiki/" target="_blank"&gt;OpenCV&lt;/a&gt; library. However, the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;eigen()&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;function provided by OpenCV can only solve symmetric matrix, which is not our case. Fortunately, there is another linear algebra C++ library called &lt;a href="http://eigen.tuxfamily.org/index.php?title=Main_Page" target="_blank"&gt;Eigen&lt;/a&gt;, and OpenCV provided functions to convert data structures needed by both libraries back and forth. With Eigen, we can get things done, but it leaves me rather messy code. This is why I don`t post my C++ implementation here.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Having only the implicit equation is not sufficient, because we cannot get any useful information from it directly. To visualize the ellipse, we have to get semi-major axis, semi-minor axis, rotation and center from the 6 parameters. Below is the C++ code:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// Input parameters: a, b, c, d, e, f&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; phi = atan(b / (a - c)) * 0.5; &lt;span class="comment"&gt;// rotation&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; cos_phi = cos(phi);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; sin_phi = sin(phi);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; u = (2 * c * d - b * e) / (b * b - 4 * a * c);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; v = (2 * a * e - b * d) / (b * b - 4 * a * c);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; center = cv::Vec2d(u, v);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// center&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// eliminate rotation and recalculate 6 parameters&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; aa = a * cos_phi * cos_phi - b * cos_phi * sin_phi + c * sin_phi * sin_phi;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; bb = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; cc = a * sin_phi * sin_phi + b * cos_phi * sin_phi + c * cos_phi * cos_phi;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; dd = d * cos_phi - e * sin_phi;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; ee = d * sin_phi + e * cos_phi;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; ff = 1 + (d * d) / (4 * a) + (e * e) / (4 * c);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sx = sqrt(ff / aa);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// semi-major axis&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sy = sqrt(ff / cc);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// semi-minor axis&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;if&lt;/span&gt;(sx &amp;lt; sy) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; temp = sx;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sx = sy;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sy = temp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;Now we can use information to visualize the ellipse. Here is an example of my result:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-sqRRih-NLCI/TuKN-lGFaBI/AAAAAAAAAAc/UqY9CJcy1YA/s1600/result1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="291" src="http://3.bp.blogspot.com/-sqRRih-NLCI/TuKN-lGFaBI/AAAAAAAAAAc/UqY9CJcy1YA/s400/result1.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The red dots are cross section points of human body and we want to fit. The ellipse shown is the result. We can see that waist, left and right biceps are quite good, but chest, hips, left and right thigh are not satisfactory. One possible explaination is that there may be many noisy points outside this visual range that affect the result.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-2374467590518809235?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/2374467590518809235/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/ellipse-fitting-and-parameter.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/2374467590518809235'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/2374467590518809235'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/ellipse-fitting-and-parameter.html' title='Ellipse fitting and parameter conversion'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-4BTxF7S6JM4/TuKAuJ79-yI/AAAAAAAAAAU/DMQSDvmKt2M/s72-c/ca5d856e1fbe0fdf05d99def75d82005.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-8953094062175862476</id><published>2011-12-09T14:14:00.001-05:00</published><updated>2011-12-10T12:21:35.524-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><title type='text'>Find the sum of all prime numbers below N</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;} &lt;/style&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;This is another classic question. Everyone would immediately come up with an idea that we can iterate from 2 to N and check if it is a prime number. If so, we add it to the sum.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&lt;span class="keyword"&gt;bool&lt;/span&gt; isprime(&lt;span class="keyword"&gt;int&lt;/span&gt; num) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;int&lt;/span&gt; j = sqrt((&lt;span class="keyword"&gt;float&lt;/span&gt;)num);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; i = 2; i &amp;lt;= j; i++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;if&lt;/span&gt;(num % i == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;return false&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="keyword"&gt;int&lt;/span&gt; primesum(&lt;span class="keyword"&gt;int&lt;/span&gt; range) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;int&lt;/span&gt; sum = 2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; i = 3; i &amp;lt;= range; i += 2) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;if&lt;/span&gt;(isprime(i))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;sum += i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;return&lt;/span&gt; sum;&lt;br /&gt;} &lt;/div&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;This solution uses &lt;a href="http://en.wikipedia.org/wiki/Trial_division" target="_blank"&gt;Trail Division&lt;/a&gt; to test if a number is prime. &lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;The trial factors need go no further than sqrt(&lt;i&gt;n&lt;/i&gt;)&lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; because, if &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;n&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; is divisible by some number &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;p&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;, then &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;n = p * q&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; and if &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;q&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; were smaller than &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;p&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;, &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;n&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; would have earlier been detected as being divisible by &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;q&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; or a prime factor of &lt;/span&gt;&lt;i style="font-family: Arial,Helvetica,sans-serif;"&gt;q&lt;/i&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;When calculating the sum, we only need to check even numbers. This would save half of the time.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; The time complexity of this method is O(n * sqrt(n)), and it requires O(1) space. The algorithm can be further optimized if we have a smaller pre-generated prime number table and test only prime factors.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;If extra space is allowed, there is a better solution which is called &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" target="_blank"&gt;Seive of Eratosthenes&lt;/a&gt;. The basic idea is to directly generate composite numbers rather than testing for prime numbers. The &lt;/span&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Seive of Eratosthenes algorithm is described as follows:&lt;/span&gt;&lt;br /&gt;&lt;ol style="color: #38761d; font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;Create a list of consecutive integers from 2 to &lt;i&gt;n&lt;/i&gt;: (2, 3, 4, ..., &lt;i&gt;n&lt;/i&gt;).&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;Initially, let &lt;i&gt;p&lt;/i&gt; equal 2, the first prime number.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;Starting from &lt;i&gt;p&lt;/i&gt;, count up in increments of &lt;i&gt;p&lt;/i&gt; and mark each of these numbers greater than &lt;i&gt;p&lt;/i&gt; itself in the list. These numbers will be 2&lt;i&gt;p&lt;/i&gt;, 3&lt;i&gt;p&lt;/i&gt;, 4&lt;i&gt;p&lt;/i&gt;, etc.; note that some of them may have already been marked.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;Find the first number greater than &lt;i&gt;p&lt;/i&gt; in the list that is not marked; let &lt;i&gt;p&lt;/i&gt; now equal this number (which is the next prime).&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-size: small;"&gt;If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;When the algorithm terminates, all the numbers in the list that are not marked are prime.&lt;/div&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;&lt;span class="keyword"&gt;int&lt;/span&gt; primesum(&lt;span class="keyword"&gt;int&lt;/span&gt; range) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;int&lt;/span&gt; sum = 2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;bool&lt;/span&gt;* prime_table = &lt;span class="keyword"&gt;new bool&lt;/span&gt;[range];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; memset(prime_table, &lt;span class="keyword"&gt;true&lt;/span&gt;, &lt;span class="keyword"&gt;sizeof&lt;/span&gt;(bool));&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; i = 2; i &amp;lt;= range / 2; i++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;if&lt;/span&gt;(prime_table[i]) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; j = 2 * i; j &amp;lt;= range; j += i) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prime_table[j] = &lt;span class="keyword"&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;for&lt;/span&gt;(&lt;span class="keyword"&gt;int&lt;/span&gt; i = 3; i &amp;lt;= range; i += 2) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;if&lt;/span&gt;(prime_table[i])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sum += i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;return&lt;/span&gt; sum;&lt;br /&gt;} &lt;/div&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;The time complexity is O(nloglogn), and space complexity if O(n).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;Above is only the implementation of original version. There are some refinements of this algorithm. For further informaton, please go to this link &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. &lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-8953094062175862476?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/8953094062175862476/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/find-sum-of-all-prime-numbers-below-n.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/8953094062175862476'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/8953094062175862476'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/find-sum-of-all-prime-numbers-below-n.html' title='Find the sum of all prime numbers below N'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-4660731577214714669</id><published>2011-12-09T00:20:00.001-05:00</published><updated>2011-12-10T12:21:48.772-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Interview questions'/><category scheme='http://www.blogger.com/atom/ns#' term='Data structure'/><title type='text'>Reverse a singly linked list</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;This is a very classic interview question. It demonstrates the ability to                work with basic data structure and pointers. The seemingly easy question has some pitfalls. Nearly every candidate starts with two                temporary pointers and eventually finds out that you need                three. You need to point to the current node (the one you’re                handling), the previous node (so you can point back to it), and                the next node (so you can prevent lost of the next node). Also, you should not forget to make the first node point to NULL.&lt;br /&gt;&lt;br /&gt;There are two solutions to this question. One is iterative and the other is recursive.&amp;nbsp; &lt;/div&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;Node* reverse_iterative(Node* head) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node* prev = head;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node* curr = head-&amp;gt;next;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node* next = head-&amp;gt;next-&amp;gt;next;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prev-&amp;gt;next = NULL;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;while&lt;/span&gt;(curr-&amp;gt;next) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; curr-&amp;gt;next = prev;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prev = curr;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; curr = next;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; next = next-&amp;gt;next;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; curr-&amp;gt;next = prev;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;return&lt;/span&gt; curr;&lt;br /&gt;}&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;br /&gt;The recursive version is shown as follow:&lt;br /&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;Node* reverse(Node* prev, Node* curr) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="comment"&gt;// If reached the end of the list, return the head&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;if&lt;/span&gt;(curr-&amp;gt;next == NULL) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;curr-&amp;gt;next = prev;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;return&lt;/span&gt; curr;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="comment"&gt;// Remember the next node&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Node* next = curr-&amp;gt;next;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="comment"&gt;// Change current pointer to previous node&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;curr-&amp;gt;next = prev;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;return&lt;/span&gt; reverse(curr, next);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Node *reverse_recursive(Node* curr)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="keyword"&gt;return&lt;/span&gt; reverse(NULL, curr);&lt;br /&gt;} &lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;Although recursive version is more straightforward, in general we prefer the iterative version because if the list is very large, this version would fail because of stack overflow.&lt;br /&gt;&lt;br /&gt;Note that there are some variations of this question like "How to print a singly linked list in reverse order". In this case, if we are allowed to alter the data structure, we can reverse linked list first and print it from the beginning.&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-4660731577214714669?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/4660731577214714669/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/reverse-singly-linked-list.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4660731577214714669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/4660731577214714669'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/reverse-singly-linked-list.html' title='Reverse a singly linked list'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4942509877707550975.post-6167961807723626823</id><published>2011-12-08T17:16:00.001-05:00</published><updated>2011-12-09T16:24:49.625-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><title type='text'>Find projection of a vector on a plane</title><content type='html'>&lt;style type="text/css"&gt;.code-wrapper {background: #eeeeee;font-family: "Courier New";padding: 20px;border: 2px solid #000000}.keyword {color: blue;font-weight: bold;}.comment {color: #38761d;}&lt;/style&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;Given a vector V in 3D space and a normal vector N of a plane, how to calculate the projection of V on the plane?&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;The following is my C++ and OpenCV implementation of the algorithm.&lt;/div&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-family: inherit;"&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;&lt;div class="code-wrapper"&gt;Vec3d findProjection(Vec3d V, Vec3d N) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Vec3d v, n;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="comment"&gt;// We convert both vectors to unit vectors&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; normalize(V, v);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; normalize(N, n);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #38761d;"&gt;// Then calculate the cos angle between n and v &lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="keyword"&gt;double&lt;/span&gt; cosVN = v.dot(n);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #38761d;"&gt;// Make sure the angle is in range of -90 to 90 degrees&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/b&gt;(cosVN &amp;lt; 0) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cosVN = -cosVN;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n = -n;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;/b&gt; V_len = sqrt(V.dot(V));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b style="color: blue;"&gt;double&lt;/b&gt; _N_len = V_len * cosVN;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #38761d;"&gt;// _N is the projection of V on N&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Vec3d _N = _N_len * n;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;/b&gt; V - _N;&lt;br /&gt;}&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4942509877707550975-6167961807723626823?l=miracle21.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miracle21.blogspot.com/feeds/6167961807723626823/comments/default' title='帖子评论'/><link rel='replies' type='text/html' href='http://miracle21.blogspot.com/2011/12/find-projection-of-vector-on-given.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/6167961807723626823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4942509877707550975/posts/default/6167961807723626823'/><link rel='alternate' type='text/html' href='http://miracle21.blogspot.com/2011/12/find-projection-of-vector-on-given.html' title='Find projection of a vector on a plane'/><author><name>Ruobing Li</name><uri>http://www.blogger.com/profile/01553568179989160761</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
