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

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;
    }
时间: 2024-07-31 01:48:38

LeetCode的medium题集合(C++实现)九的相关文章

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

1 Unique Paths A robot is located at the top-left corner of a m x n grid (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

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

1 Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. 可以采用递归的方法解决这个问题,当找到一组数之和

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

1 Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative. 该题实际就是利用字符串来解决大数的乘法问题.为了计算方便先将两组数翻转,将字符串转化为整数利用两个循环逐位相乘,将结果保存.然后逐位解决进位问题. s

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

1 Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending or

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

1 Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1],

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

1 Pow(x, n) 该题采用二分法进行递归 double myPow(double x, int n) { if(n==0) return 1; if(n<0) { n=(-n); x=1/x; } double res=myPow(x,n/2); if(n%2==0) { return res*res; } else { return res*res*x; } } 2 Maximum Subarray Find the contiguous subarray within an array

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

1 Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations.Given n and k, return the kth permutation sequence. 使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制.下面采用数学法: 在n!个排列中,第一位元素相同的排列总是有(n?1)!个,如果p=k/(n?1)!,那么排列的第一位元素

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

1 Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example: Give

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

1 Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red,