Day 5: Lintcode【数组】【简单】【恢复旋转数组】【最大子数组】【两个数字和】

2019.3.12做题

1. 恢复旋转数组(39)

题目:Recover Rotated Sorted Array

Given a rotated sorted array, recover it to sorted array in-place.

Example
Example1:

[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]

Example2:

[6,8,9,1,2] -> [1,2,6,8,9]

Challenge

In-place, O(1) extra space and O(n) time.

Clarification

What is rotated array?

For example, the orginal array is [1,2,3,4], The rotated array of it can be [1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]

思路:

其实就是寻找单调性发生变化的那个点,只要找到那个点怎么都好办。

  • 可以按照一些博客说的那样,将断点前半部分翻转一下,再整体翻转一下得到恢复的数组。
  • 也可以像我这样写的一样,找到断点之后记录一下断点位置,继续走下去将后半部分存储到一个新容器中,最后回过头来将断点之前的元素储存下来。这样的复杂度也是n平方。

代码:

class Solution {
public:
    /**
     * @param nums: An integer array
     * @return: nothing
     */
    void recoverRotatedSortedArray(vector<int> &nums) {
        // write your code here
        int l = nums.size();
        int flag = 0;
        vector<int>res;
        for(int i=1; i<l; ++i){
            if(nums[i]<nums[i-1]){
                flag = i; 
            }
            if(flag != 0){
                res.push_back(nums[i]);
            }
        }
        for(int i=0; i<flag; ++i){
            res.push_back(nums[i]);
        }
        if(flag !=0)
            nums = res;
    }
};

2.最大子数组 (41)

题目:Maximum Subarray

Given an array of integers, find a contiguous subarray which has the largest sum.

Example
Example1:

Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Example2:

Given the array [1,2,3,4], the contiguous subarray [1,2,3,4] has the largest sum = 10.

Challenge

Can you do it in time complexity O(n)?

Notice

The subarray should contain at least one number.

思路:

这题可以和day4 中的最小子数组做为对比练习。

  • 都设置一个sum,一个res;
    • 求最小子数组的时候,将sum与res对比,比res还小就更新res;sum>res就舍掉,更新sum值为0;
    • 求最大子数组的时候,将sum与res对比,比res还大就更新res;sum<0就舍掉,更新sum值为0;
  • 返回res

代码:

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @return: A integer indicate the sum of max subarray
     */
    int maxSubArray(vector<int> &nums) {
        // write your code here
        int l = nums.size();
        int sum=0, maxsum=nums[0];
        for(int i=0; i<l; ++i){
            sum+=nums[i];
            if(sum>maxsum) maxsum = sum;
            else if(sum<0){
                sum = 0;
            }
        }
        return maxsum;
    }
};

3.两个数字和(56)

题目:Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are zero-based.

Example
Example1:

numbers=[2, 7, 11, 15], target=9
return [0, 1]

Example2:

numbers=[15, 2, 7, 11], target=9
return [1, 2]

Challenge

Either of the following solutions are acceptable:

O(n) Space, O(nlogn) Time
O(n) Space, O(n) Time

Notice

You may assume that each input would have exactly one solution

思路:

只在内层循环做了些手脚。。。

代码:

class Solution {
public:
    /**
     * @param numbers: An array of Integer
     * @param target: target = numbers[index1] + numbers[index2]
     * @return: [index1, index2] (index1 < index2)
     */
    vector<int> twoSum(vector<int> &numbers, int target) {
        // write your code here
        int l = numbers.size();
        int index1, index2;
        int another;
        int flag = 0;
        for(int i=0; i<l; ++i){
            another = target-numbers[i];
            index1 = i;
            
            for(int j=i+1; j<l; ++j){
                if(numbers[j]==another){
                    index2 = j;
                    flag = 1;
                    break;
                }
            }
            if(flag==1) break;            
        }
        vector<int >res;
        res.push_back(index1);
        res.push_back(index2);
        return res; 
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值