【算法题】牛客研发最爱考[11 - 20]

刷题链接

两数之和

哈希

#include <unordered_map>

class Solution {
public:
    /**
     * 
     * @param numbers int整型vector 
     * @param target int整型 
     * @return int整型vector
     */
    vector<int> twoSum(vector<int>& numbers, int target) {
        // write code here
        unordered_map<int,int> hash; // <值,下标>
        for(int i = 0;i < numbers.size(); i++)
        {
            if(hash.count(target - numbers[i])){
                return {hash[target - numbers[i]] + 1, i + 1};
            }
            hash[numbers[i]] = i;
        }
        return {};
    }
};

子数组的最大累加和

动态规划,空间复杂度 O ( 1 ) O(1) O(1)

class Solution {
public:
    /**
     * max sum of the subarray
     * @param arr int整型vector the array
     * @return int整型
     */
    int maxsumofSubarray(vector<int>& arr) {
        // write code here
        int res = 0, cur = 0;
        for(auto c : arr)
        {
            cur += c;
            if(cur < 0) cur = 0;
            res = max(res,cur);
        }
        return res;
    }
};

合并有序链表

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param l1 ListNode类 
     * @param l2 ListNode类 
     * @return ListNode类
     */
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        // write code here
        ListNode* head = new ListNode(-1); // 新建头结点
        ListNode* cur = head;
        while(l1 && l2)
        {
            if(l1->val <= l2->val) {
                cur->next = l1;
                l1 = l1->next;
            }else{
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        cur->next = l1 == NULL? l2 : l1;
        return head->next;
    }
};

用两个栈实现队列

模拟

class Solution
{
public:
    
    void copy(stack<int> &source,stack<int> &copyto)
    {
        while(source.size()){
            copyto.push(source.top());
            source.pop();
        }
    }
    
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        int res = 0;
        copy(stack1,stack2);
        if(!stack2.empty()) {
            res = stack2.top();
            stack2.pop();
        }
        copy(stack2,stack1);
        return res;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

找到字符串最长无重复子串

哈希,双指针,滑动窗口

class Solution {
public:
    /**
     * 
     * @param arr int整型vector the array
     * @return int整型
     */
    int maxLength(vector<int>& arr) {
        // write code here
        int n = arr.size();
        int res = 0;
        map<int,int> hash;
        for(int i = 0, j = 0;i < n;i ++ )
        {
            hash[arr[i]] ++;
            while(hash[arr[i]] > 1){
                hash[arr[j]] -- ;
                j ++ ;
            }
            res = max(res,i - j + 1);
        }
        return res;
    }
};

合并两个有序数组

双指针算法,从后往前遍历,就不用担心覆盖了,归并排序的思想

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int k = m + n - 1;
        int i = m - 1,j = n - 1;
        while(i >= 0 && j >= 0)
        {
            if(A[i] >= B[j]) A[k -- ] = A[i -- ];
            else A[k -- ] = B[j -- ];
        }
        while(i >= 0) A[k -- ] = A[i -- ];
        while(j >= 0) A[k -- ] = B[j -- ];
    }
};

链表中的节点k个一组翻转

前置知识:翻转链表。可以画图理解

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        
        ListNode *cur = dummy;
        while(cur)
        {
            // 1.确定是否有k个数
            ListNode *first = cur->next;
            ListNode *end = cur;
            for(int i =0;i < k && end != NULL;i ++ )
                end = end->next;
            if(end == NULL) break;
            // 2.k个反转
            ListNode *p1 = cur->next, *p2 = p1->next;
            while(p1 != end)
            {
                auto next = p2->next;
                p2->next = p1;
                p1 = p2,p2 = next;
            }
            // 3.重新连接,画图理解
            first->next = p2;
            cur->next = end;
            cur = first;
        }
        
        return dummy->next;
    }
};

链表中环的入口节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        /*
            思路:快慢指针
                fast走两步,slow走一步,相遇的时候slow返回起点,然后各自走一步,再相遇就是环的入口
            证明:
                slow * 2 =fast -> (x + y) * 2 = x + y + n(y + z);
                化简得:x = (n - 1) * (y + z) + z, n为任意
        */
        auto fast = head,slow = head;
        while(fast)
        {
            fast = fast->next;
            if(fast) fast=fast->next;
            else break;
            slow = slow->next;
            
            if(fast == slow)
            {
                slow = head;
                while(fast != slow)
                {
                    fast =fast->next;
                    slow = slow->next;
                }
                return slow;
            }
        }
        return NULL;
    }
};

括号序列

#include <unordered_map>

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    bool isValid(string s) {
        // write code here
        unordered_map<char,char> hash = {
            {'(',')'},
            {'[',']'},
            {'{','}'}
        };
        
        stack<char> stk;
        for(auto c : s)
        {
            if(hash.count(c)) stk.push(c);
            else
            {
                if(stk.empty() || hash[stk.top()] != c) return false; // 这里是栈顶元素与c比较
                stk.pop();
            }
        }
        if(stk.size()) return false;
        return true;
    }
};

删除链表倒数第n个节点

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // write code here
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        
        auto fast = dummy, slow = dummy;
        
        while(n -- ) fast =fast->next;
        while(fast->next)
        {
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next; // 这里出错
        
        return dummy->next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值