LeetCode开心刷题二十天——32. Longest Valid Parentheses(Compare consider with 31)33. Search in Rotated Sorted...

32. Longest Valid Parentheses
Hard
199995FavoriteShare

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

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
Accepted
200,042
Submissions
774,584
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<string>
#include<memory>
#include<memory.h>
#include<hash_map>
#include<map>
#include<set>
#include<unordered_map>
using namespace std;
class Solution {
public:
    int longestValidParentheses(string s) {
        stack<int> q;
        int start=0;
        int ans=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='(')
                q.push(i);
            else
            {
                if(q.empty())
                    start=i+1;
                else
                {
                    int temp=q.top();q.pop();
                    //+1 is to add the first(
                    ans=max(ans,q.empty()?i-start+1:i-q.top());
                }

            }
        }
        return ans;

    }
};
int main()
{
    string res={"(())"};
    Solution s;
    int n;
    n=s.longestValidParentheses(res);
    cout<<n<<endl;
    return 0;
}

 

33. Search in Rotated Sorted Array
Medium
2624330FavoriteShare

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.

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

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

ATTENTION POINT:
please clarify > >= whether add = can cause largely difference in many situations
in this code if you not write l<=r ,not =,will cause a lot of problem
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<string>
#include<memory>
#include<memory.h>
#include<hash_map>
#include<map>
#include<set>
#include<unordered_map>
using namespace std;
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int l=0,r=nums.size()-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(nums[mid]==target) return mid;
            if(nums[mid]<nums[r])
            {
                if(target>nums[mid]&&target<=nums[r])
                    l=mid+1;
                else
                    r=mid-1;

            }
            else
            {
                if(target>=nums[l]&&target<nums[mid])
                {
                    r=mid-1;
                }
                else
                {
                    l=mid+1;
                }
            }

        }
        return -1;
    }
};
int main()
{
    vector<int> nums={4,5,6,7,0,1,2};
    int target=0,res=0;
    Solution s;
    res=s.search(nums,target);
    cout<<res<<endl;
    return 0;
}

 

38. Count and Say
Easy
8296371FavoriteShare

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

 

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"
 ATTENTION:
1.for loop attention the begin and end;for(i=0/1;i</<=n;i++)
2.string has special action to solve,such as string+=a,then string add a behind it.
3.string number '3' and real number 3 the distance is '0',so it need to minus '0' before
it to add into calculation.
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<string>
#include<memory>
#include<memory.h>
#include<hash_map>
#include<map>
#include<set>
#include<unordered_map>
using namespace std;
class Solution {
public:
    string countAndSay(int n) {
        string ans="1";
        for(int i=1;i<n;i++)
        {
            ans=say(ans);
        }
        return ans;
    }
private:
    string say(const string& n)
    {
        string ans;
        int s=0,l=n.length();
        for(int e=1;e<=l;e++)
        {
            if(e==l||n[s]!=n[e])
            {
                int count=e-s;
                ans+='0'+count;
                ans+=n[s];
                s=e;
            }
        }
        return ans;
    }
};
int main()
{
    string res;
    Solution s;
    int n=4;
    res=s.countAndSay(n);
    cout<<res<<endl;
    return 0;
}
 
   

 

 

转载于:https://www.cnblogs.com/Marigolci/p/11204703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值