31-35题 题解

31Next 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 order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

//STL
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        next_permutation(nums.begin(), nums.end());
    }
};
32 Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

//申请一个数组来存当前位置,如果当前位置是左括号,前一个位置是右括号,那么当前位置的最佳值就是前一个位置,否则为0;
//如果是右括号,看栈中是否为空,为空栈,当前位置为0,否则需要算到当前位置的最佳值 = 左括号前的最佳值 + 2 + 右括号前的最佳值。
//这样所有情况都给处理了。
class Solution {
public:

    int longestValidParentheses(string s) {
        int res = 0, ans = 0, len = s.length();
        int *f = (int*)malloc((len+1)*sizeof(int));
        f[0] = 0;
        stack<int> st;
        for(int i = 0; i < len; i++)
        {
            if(s[i] == '(')
            {
                if(i == 0 || s[i-1] == '(') f[i] = 0;
                else f[i] = f[i-1];
                st.push(f[i]);
            }
            else
            {
                if(!st.empty())
                {
                    int v = st.top(); st.pop();
                    f[i] = v + 2;
                    if(s[i-1] == ')') f[i] += f[i-1];
                }
                else f[i] = 0;
            }
            ans = max(ans, f[i]);
        }
        return ans;
    }
};


33 Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

//分类二分 6ms
class Solution {
public:
    int search(vector<int>& nums, int target) {
        if(nums.size() == 0) return -1;
        int k = -1;
        for(int i = 1; i < nums.size(); i++)
        {
            if(nums[i] > nums[i-1]) continue;
            k = i;
            break;
        }
        if(k == -1)
        {
            int s = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
            if(nums[s] == target) return s;
            return -1;
        }

        if(target < nums[0])
        {
            int s = lower_bound(nums.begin()+k, nums.end(), target) - nums.begin();
            if(nums[s] == target) return s;
            return -1;
        }
        int s = lower_bound(nums.begin(), nums.begin()+k, target)-nums.begin();
        if(nums[s] == target) return s;
        return -1;
    }
};

//暴力 9ms
class Solution {
public:
    int search(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] == target) return i;
        }
        return -1;            
    }
};
34 Search for a Range

Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

//STL lower_bound 和 upper_bound的运用哈~,找不到返回的是end()位置
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ans;
        ans.push_back(-1);
        ans.push_back(-1);
        int s = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
        int e = upper_bound(nums.begin(), nums.end(), target) - nums.begin();
        if(s >= nums.size()) return ans;
        if(nums[s] == target)
        {
            ans.clear();
            ans.push_back(s);
            ans.push_back(e-1);
        }
        return ans;
    }
};
35 Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 1:

Input: [1,3,5,6], 0
Output: 0
//STL
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        return lower_bound(nums.begin(), nums.end(), target)-nums.begin();        
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值