Leetcode中一维数组与应用II

一、最大子序列和

题目一:Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the contiguous subarray [4, -1, 2, 1] has the largest sum=6.

思路:这是一道很常见的题,求最大子序列的和。

class Solution {
public:
    int maxSubArray(int A[], int n) {
        if(n==0) return 0;
        int res=A[0];
        int sum=0;
        for(int i=0; i<n; i++){
            if(sum<0) sum=0;
            sum+=A[i];
            if(sum>res) res=sum; //更新最大值
        }
        return res;
    }
};

题目二:Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].  You have a car with unlimited gas tank and it cost cost[i] of gas to travel form station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

思路:这道题本质是类似于求子序列和(不需要求最大子序列和,在这里没有意义)。要求每一个位置的累计和必须大于0!

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        if(gas.size()==0) return 0;
        int sum=0;
        int start=0;
        for(int i=0;i<gas.size();i++){
            int tmp=gas[i]-cost[i];
            if(sum<0){
                sum=0;
                start=i;
            }
            sum+=tmp;
        }
        if(sum<0) return -1;//无法跑完后半程,出局
        for(int i=0;i<start;i++)
            sum=sum+gas[i]-cost[i];
        if(sum>=0) return start;
        return -1; //无法跑完全程,出局
    }
};

二、找出只出现一次的数

题目三:Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Linear runtime complexity is required. 

思路:这道题我想歪了。。。下面是一种O(nlogn)的解法。

class Solution {
public:
    int singleNumber(int A[], int n) {
        if(n==0 || (n-1)%3) return -1;
        sort(A,A+n);
        int i;
        for(i=0;i<n-2;i++){
            if(A[i]==A[i+2])
                i=i+2;
            else break;
        }
        if(i<n-2) return A[i];
        else return A[n-1];
    }
};
看了长颈鹿先生的super解法,重新给出O(32n)算法。思路是统计每一个位上1的个数,然后%3。如果一个数值出现了一次,求模之后肯定是1.

class Solution {  
public:  
    int singleNumber(int A[], int n) {  
        int res=0;  
        for(int i=0;i<32;i++){  
            int count = 0;  
            for(int j=0;j<n;j++){  
                count += (A[j]>>i)&1;  
            }  
            res |= ((count%3)<<i);  
        }  
        return res;  
    }  
};  

三、Jump Game

题目四: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 it you are able to reach the last index.

思路:尽力跳。维护一个当前跳到最远的量。

class Solution {
public:
    bool canJump(int A[], int n) {
        if(n==0 || n==1) return true;
        int largest=A[0];
        for(int j=0; j<=largest; j++){   //largest acts as a milestone
            if(A[j]+j>=n-1) return true;
            if(A[j]+j>largest)
                largest=A[j]+j;
        }
        return false;
    }
};


题目五: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. Your goal is to reach the last index in the minimum number of jumps. 

思路:凌乱了。。。这是什么算法,算是贪心吗?

//将区间以i为中心分成两个部分,前一部分cur表示i之前能达到的最佳位置,
//后一部分fast表示上一次调到的位置到i之间的所有位置所能跳到的最远的距离

class Solution {
public:
    int jump(int A[], int n) {
        int steps = 0;
        int fast = 0;
        int curr = 0;
        for (int i = 0; i < n; ++i) {
            if (i > curr) {
                curr = fast;  //i之前某个位置能跳到的最远的位置
                ++steps;
            }
            fast = max(fast, i+A[i]); //当前位置能达到的最远的位置
        }

        return steps;
    }
};








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值