摘要:
A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
摘要:
You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:这道题关键在于数组的操作问题上,如何保存中间变量,还有图片的旋转方法。首先将数组分为(行数/2)层,然后一层一层的进行旋转。class Solution {public: void rotate(vector > &matrix) { if(matrix.size()==0||matrix[0].size()=... 阅读全文
摘要:
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?思路:这道题有点难度,需要用到数学推理。如下图,需要设置慢指针slow,一次走一步和快指针fast,一次走两步,我们设环开始位置距离头结点的距离为K,当快指针追到慢指针时候的位置设为慢指针走的距离为x,快指针走的距离为y。链表的总长度为L。那么我们可以有如下推算过程:y=2x;y=L+x-k;这样可以退出 阅读全文