码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode的medium题集合(C++实现)九

时间:2015-05-21 12:48:33      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   两指针法   

1 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.
利用两指针法:第一个指针扫描当前值,第二个指针指向当前能跳到的最远距离。扫描的同时实时更新最远距离。如果最远距离能到达数组尾端,返回true,如果不能到达数组尾端而且当前指针不能向前移动时则返回false.

bool canJump(vector<int>& nums) {
        int n=nums.size(),sum=0;
        if(n<=1) return true;
        for(int i=0;i<n-1;i++)
        {
            if(nums[i]==0&&sum<i+1) return false;
            if(nums[i]+i>sum&&nums[i]>0)
            {
                sum=nums[i]+i;
                if(sum>=n-1) return true;
            }
        }
        return false;
    }

2 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example, Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
利用四个指针分别指向行和列的首端和尾端,每旋转一圈输出时将相应的指针加1或减1.当有首端指针大于尾端指针时结束。

vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> res;
        if(matrix.empty()) return res;
        int m=matrix.size();
        int n=matrix[0].size();

        int rbegin=0,cbegin=0,rend=m-1,cend=n-1;
        while(1)
        {
            if(cbegin>cend) return res;
            for(int i=0;(i+cbegin)<=cend;i++)
            {
                res.push_back(matrix[rbegin][i+cbegin]);
            }
            rbegin++;
            if(rbegin>rend) return res;
            for(int j=0;(j+rbegin)<=rend;j++)
            {
                res.push_back(matrix[j+rbegin][cend]);
            }
            cend--;
            if(cend<cbegin) return res;
            for(int k=0;(cend-k)>=cbegin;k++)
            {
                res.push_back(matrix[rend][cend-k]);
            }
            rend--;
            if(rend<rbegin) return res;
            for(int l=0;(rend-l)>=rbegin;l++)
            {
                res.push_back(matrix[rend-l][cbegin]);
            }
            cbegin++;
        }
        return res;
    }

3 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
该题与上一题解法一样,利用变量sum,初始化为0,然后每步自加1给对应的矩阵位置赋值

vector<vector<int>> generateMatrix(int n) {
        vector<vector<int> > matrix(n,vector<int>(n,0));
        if(n==0) return matrix;
        int rbegin=0,cbegin=0,rend=n-1,cend=n-1,sum=0;
        while(1)
        {
            if(cbegin>cend) break;
            for(int i=0;(i+cbegin)<=cend;i++)
            {
                matrix[rbegin][i+cbegin]=(++sum);
            }
            rbegin++;
            if(rbegin>rend) break;
            for(int j=0;(j+rbegin)<=rend;j++)
            {
                matrix[j+rbegin][cend]=(++sum);
            }
            cend--;
            if(cend<cbegin) break;
            for(int k=0;(cend-k)>=cbegin;k++)
            {
                matrix[rend][cend-k]=(++sum);
            }
            rend--;
            if(rend<rbegin) break;
            for(int l=0;(rend-l)>=rbegin;l++)
            {
                matrix[rend-l][cbegin]=(++sum);
            }
            cbegin++;
        }
        return matrix;
    }

LeetCode的medium题集合(C++实现)九

标签:c++   leetcode   两指针法   

原文地址:http://blog.csdn.net/zhulong890816/article/details/45889395

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!