算法刷题计划(七)(LeetCode)删除链表的倒数第 N 个结点、有效的括号、合并两个有序链表、整数转换英文表示

22 篇文章 0 订阅
20 篇文章 0 订阅


一、删除链表的倒数第N个结点

  • 题目:

在这里插入图片描述


  • 解题方法:
    1、计算链表长度
/**
 * 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*dummy=new ListNode(0,head);
        int length=ListNodeLength(head);
        ListNode*current=dummy;
        for(int i=0;i<length-n;i++){
            current=current->next;
        }
        current->next=current->next->next;
        ListNode*ans=dummy->next;
        delete dummy;
        return ans;
    }

private:
    int ListNodeLength(ListNode*head){
        int length=0;
        while(head!=nullptr){
            head=head->next;
            length++;
        }
        return length;
    }
};

2、利用栈

{
        //栈
        ListNode*dummy=new ListNode(0,head);
        stack<ListNode*>sta;
        ListNode*current=dummy;
        while(current!=nullptr){
            sta.push(current);
            current=current->next;
        }
        for(int i=0;i<n;i++){
            sta.pop();
        }
        ListNode*preList=sta.top();
        preList->next=preList->next->next;
        ListNode*ans=dummy->next;
        delete dummy;
        return ans;
    }

3、双指针

{
        //双指针
        ListNode*dummy=new ListNode(0,head);
        ListNode*fast=head;
        ListNode*slow=dummy;
        for(int i=0;i<n;i++){
            fast=fast->next;
        }
        while(fast!=nullptr){
            fast=fast->next;
            slow=slow->next;
        }
        slow->next=slow->next->next;
        ListNode*ans=dummy->next;
        delete dummy;
        return ans;
    }

4、递归

    {
        //递归
        if(head==nullptr)   return nullptr;
        head->next=removeNthFromEnd(head->next,n);
        index++;
        if(index==n)    return head->next;
        return head;
    }
    int index=0;

二、有效的括号

  • 题目:
    在这里插入图片描述

  • 解题方法:
    1、哈希表
class Solution {
public:
    bool isValid(string s) {
        int n = s.size();
        if (n % 2 == 1) {
            return false;
        }

        unordered_map<char, char> pairs = {
            {')', '('},
            {']', '['},
            {'}', '{'}
        };
        stack<char> stk;
        for (char ch: s) {
            if (pairs.count(ch)) {
                if (stk.empty() || stk.top() != pairs[ch]) {
                    return false;
                }
                stk.pop();
            }
            else {
                stk.push(ch);
            }
        }
        return stk.empty();
    }
};

2、遍历

bool isValid(string s) {
        stack<char> stack;
        for(int i=0; i<s.length(); ++i){
            char c = s[i];
            if(!stack.empty()){
                char t = stack.top();
                if(t=='(' && c==')' 
                    || t=='[' && c==']'
                    || t=='{' && c=='}'){
                    stack.pop();
                    continue;
                }
            }
            stack.push(c);
        }
        return stack.empty();
    }

三、合并两个有序链表

  • 题目:
    在这里插入图片描述

  • 解题方法:
    1、递归
/**
 * 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1==nullptr&&l2==nullptr)    return nullptr;
        if(l1==nullptr&&l2!=nullptr)    return l2;
        if(l1!=nullptr&&l2==nullptr)    return l1;
        if(l1->val > l2->val)  {
            l2->next=mergeTwoLists(l1,l2->next);
            return l2;
        }
        else   {
            l1->next=mergeTwoLists(l1->next,l2);
            return l1;
        }
    }
};

2、迭代

 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1==nullptr&&l2==nullptr)    return nullptr;
        if(l1==nullptr&&l2!=nullptr)    return l2;
        if(l1!=nullptr&&l2==nullptr)    return l1;
        ListNode*newListNode=new ListNode(-1);
        ListNode*prev=newListNode;
        while(l1!=nullptr&&l2!=nullptr){
            if(l1->val>l2->val){
                prev->next=l2;
                l2=l2->next;
            }else{
                prev->next=l1;
                l1=l1->next;
            }
            prev=prev->next;
        }
        prev->next=l1!=nullptr?l1:l2;
        return newListNode->next;
    }

四、整数转换英文表示

  • 题目:
    在这里插入图片描述

  • 解题方法:
class Solution {
public:
    string numberToWords(int num) {
        if(num==0)  return "Zero";
        string str;
        for(int i =3,unit=1000000000;i>=0;i--,unit/=1000){
            int currentNum=num/unit;
            if(currentNum){
                num-=currentNum*unit;
                string strNum;
                recusion(strNum,currentNum);
                str+=strNum+thousands[i];
            }
        }
        while(str.back()==' ')  str.pop_back();
        return str;
    }
    void recusion(string&str,int num){
        if(num==0)  return ;
        if(num<10)  str+=singles[num%10];
        else if(num<20) str+=teens[num-10];
        else if(num<100)    str+=tens[num/10],recusion(str,num%10);
        else{
            str+=singles[num/100]+"Hundred ";
            recusion(str,num%100);
        }
    }
private:
    vector<string> singles = {"","One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
    vector<string> teens = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ","Seventeen ", "Eighteen ", "Nineteen "};
    vector<string> tens = {"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
    vector<string> thousands = {"", "Thousand ", "Million ", "Billion "};
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值