• 算法-贪心


    1.摆动序列

    如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。

    例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。

    给定一个整数序列,返回作为摆动序列的最长子序列的长度。 通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。

    示例 1:

    输入: [1,7,4,9,2,5]
    输出: 6 
    解释: 整个序列均为摆动序列。
    

    示例 2:

    输入: [1,17,5,10,13,15,10,5,16,8]
    输出: 7
    解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。

    示例 3:

    输入: [1,2,3,4,5,6,7,8,9]
    输出: 2

    进阶:
    你能否用 O(n) 时间复杂度完成此题?

    class Solution {
    public:
        int wiggleMaxLength(vector<int>& nums) {
            int len=nums.size();
            if(len<2) return len;
            const int BEGIN=0;
            const int UP=1;
            const int DOWN=2;
            int STATE=BEGIN;
            int length=1;
            for(int i=1;i<len;i++){
                switch(STATE){
                    case BEGIN:
                        if(nums[i-1]<nums[i]){
                            STATE=UP;
                            length++;
                        }else if(nums[i-1]>nums[i]){
                            STATE=DOWN;
                            length++;
                        }
                        break;
                    case UP:
                        if(nums[i-1]>nums[i]){
                            STATE=DOWN;
                            length++;
                        }
                        break;
                    case DOWN:
                        if(nums[i-1]<nums[i]){
                            STATE=UP;
                            length++;
                        }     
                }
            }
            return length;
            
        }
         
    };

    利用有限状态自动机,思路清晰,代码简洁(前后相等直接不处理)


    2.移除k位数字

    给定一个以字符串表示的非负整数 num,移除这个数中的 位数字,使得剩下的数字最小。

    注意:

    • num 的长度小于 10002 且 ≥ k。
    • num 不会包含任何前导零

    示例 1 :

    输入: num = "1432219", k = 3
    输出: "1219"
    解释: 移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219。
    

    示例 2 :

    输入: num = "10200", k = 1
    输出: "200"
    解释: 移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。
    

    示例 3 :

    输入: num = "10", k = 2
    输出: "0"
    解释: 从原数字移除所有的数字,剩余为空就是0。
    class Solution {
    public:
        string removeKdigits(string num, int k) {
            int len=num.size();
            if(len==0) return 0;
            stack<char> temp;
            temp.push(num[0]);
            for(int i=1;i<len;i++){
                if(k==0){
                    temp.push(num[i]);
                    continue;
                }//k等于0则直接入栈
                while(!temp.empty() && num[i]<temp.top() &&  k){ //栈不空条件要在前
                         temp.pop();
                         k--;  
                }   
                if(num[i]!='0'|| !temp.empty())  //若为0且为前导则步入栈,反之入栈
                temp.push(num[i]);
            }
            while(k!=0 && !temp.empty()){     //遍历结束后若k还不为0且栈不空,去除栈尾
                temp.pop();
                k--;
            }
            vector<char> res;   //临时结果数组
            while(!temp.empty()){
                res.push_back(temp.top());
                temp.pop();
            }
            if(res.empty()) return "0";//为空则返回0
            string result(res.rbegin(),res.rend());//反向迭代器逆序初始化result
            
            return result;
        }
    };

    3.

  • 相关阅读:
    mac c++编译出现segmentation fault :11错误
    ssh 连接缓慢解决方法
    237. Delete Node in a Linked List
    203. Remove Linked List Elements
    Inversion of Control Containers and the Dependency Injection pattern
    82. Remove Duplicates from Sorted List II
    83. Remove Duplicates from Sorted List
    SxsTrace
    使用CCleaner卸载chrome
    decimal and double ToString problem
  • 原文地址:https://www.cnblogs.com/chendaniu/p/10285556.html
Copyright © 2020-2023  润新知