2020/12/16 leetcode刷题

738.单调递增的数字

在这里插入图片描述
解题思路:

利用贪心算法:此题从后向前遍历;一旦出现strNum[i - 1] > strNum[i]的情况(非单调递增),利用flag标记该位置,strNum[i - 1]–,然后从flag到len ;strNum[i]赋值为9;注意设置flag参数标记第一个大于str[i]的位置,一定要从strnum.length()开始;

代码实现

class Solution {
public:
    int monotoneIncreasingDigits(int N) {
    //to_string 将数字转换为字符串
        string strNum=to_string(N);
        //记录第一个大于str[i] 的位置;flag
        int flag=strNum.size();
        for(int i=strNum.size()-1;i>=0;i--){
            if(strNum[i-1]>strNum[i]){
                flag=i;
                strNum[i-1]--;
            }
        }
        for(int i=flag;i<strNum.size();i++){
            strNum[i]='9';
        }
        //stoi将字符串转换为整型变量;
        return stoi(strNum);
    }
};

20 有效的括号

在这里插入图片描述
解题思路:

利用辅助栈,当碰到左括号时入栈,右括号时比较与栈顶元素,相匹配则出栈,不匹配return false;

class Solution {
public:
    bool isValid(string s) {
        if(s.size()==0) return true;
        stack<char>sta;
        for(int i=0 ; i<s.length() ; i++){
            if(s[i]=='('||s[i]=='{'||s[i]=='['){
                sta.push(s[i]);
            }else{
                if(s[i]==')'){
                    if(!sta.empty()&&sta.top()=='('){
                        sta.pop();
                    }else{
                        return false;
                    }
                  
                }else if(s[i]==']'){
                   if(!sta.empty()&&sta.top()=='['){
                        sta.pop();
                    }else{
                        return false;
                    }
                  
                }else if(s[i]=='}'){
                    if(!sta.empty()&&sta.top()=='{'){
                            sta.pop();
                    }else{
                        return false;
                    }
                }
            }
        }
        if(sta.empty()){
            return true;
        }
        return false;

    }
};

精简版本:

在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了!

在这里插入图片描述

class Solution {
public:
    bool isValid(string s) {
        stack<int> st;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(') st.push(')');
            else if (s[i] == '{') st.push('}');
            else if (s[i] == '[') st.push(']');
            // 第三种情况 是遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
            // 第二种情况 遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
            else if (st.empty() || st.top() != s[i]) return false;
            else st.pop(); // st.top() == s[i]
        }
        // 第一种情况 此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true
        return st.empty();
    }
};


19 删除链表的倒数第N个节点

leetcode19
在这里插入图片描述
解题思路

解法1:可以先遍历一遍求出链表的长度N,然后等价于求删除正数第N-n+1的节点,在来一遍遍历,删除节点即可;

解法2:设置双指针,快指针先走K步,然后和慢指针一起向后走,当快指针为空即走到链表末尾时,此时慢指针正好对应与待删除结点的位置上,而如果在链表的头部加上虚拟头节点,此时则对应待删除结点的前一个位置,将此节点指向next的next即可,即slow->next=slow->next->next;

代码实现


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
//解法1:
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int cnt=0;
        if(head==nullptr) return nullptr;
        ListNode *cur=head;
        ListNode *dummpy=new ListNode(0);
        dummpy->next=head;
        while(cur){
            cnt++;
            cur=cur->next;
        }
        cnt-=n;
        ListNode *tmp=dummpy;
        while(cnt--){
            tmp=tmp->next;
        }
        tmp->next=tmp->next->next;
        return dummpy->next;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
 //解法二
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==nullptr||head->next==nullptr) return nullptr;
        ListNode *fast=head;
        //添加亚节点在链表头节点之前,防止越界的同时找到待删除节点的前一个位置。
        ListNode *dummpy=new ListNode (0,head);
        ListNode *slow=dummpy;

        while(n--){
           fast=fast->next;
        }
        while(fast!=nullptr){
            slow=slow->next;
            fast=fast->next;
        }
		slow->next=slow->next->next;
		
        return dummpy->next;
        
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *slow=head,*fast=head,*pre=head;
        while(n--){
            fast=fast->next;
        }
        while(fast){
            pre=slow;
            slow=slow->next;
            fast=fast->next;
        }
        //重要,边界判断
        if(slow==head) return head->next;
        pre->next=slow->next;
        return head;

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值