您的位置:首页 > 编程语言 > C语言/C++

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

2015-05-21 11:23 357 查看
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.

[code]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.当有首端指针大于尾端指针时结束。

[code]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给对应的矩阵位置赋值

[code]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;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: