leetcode算法刷题记录--7

两数之和(leetcode1)

C++

#include <vector>
#include <unordered_map>
using namespace std;

// 两数之和
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        // 定义unordered_map存储值和索引
        unordered_map<int,int> tmp;
        for(int i=0;i<nums.size();i++){
            auto it = tmp.find(target - nums[i]);
            if(it != tmp.end()){
                ans.push_back(it->second);
                ans.push_back(i);
                return ans;
            }
            tmp[nums[i]]=i;
        }
        return ans;
    }
};

Python

from typing import List
# 两数之和
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # 创建一个字典存储数值和对应的下标
        dic = {}
        for i,num in enumerate(nums):
            if (target-num) in dic:
                return [dic[target-num],i]
            dic[num] = i
        return []

买卖股票的最佳时机(leetcode121)

C++

#include <vector>
using namespace std;
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int profit = 0;
        int min_prices = prices[0];
        for(int i=1;i<prices.size();i++){
            profit = max(profit,prices[i]-min_prices);
            min_prices = min(min_prices,prices[i]);
        }
        return profit;
    }
};

Python

from typing import List
# 买卖股票的最佳时机
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit = 0
        min_price = prices[0]
        for i in prices[1:]:
            profit = max(profit,i-min_price)
            min_price = min(min_price, i)
        return profit

有效的括号(leetcode20) 

C++

#include<string>
#include <vector>
using namespace std;
// 有效的括号
class Solution {
public:
    bool isValid(string s) {
        vector<char> stack;
        for (int i=0;i<s.length();i++) {
            if(s[i]==']'){
                if(!stack.empty() && stack.back()=='['){
                    stack.pop_back();
                }else{
                    return false;
                }
            }
            else if(s[i]=='}'){
                if(!stack.empty() && stack.back()=='{'){
                    stack.pop_back();
                }else{
                    return false;
                }
            }
            else if(s[i]==')'){
                if(!stack.empty() && stack.back()=='('){
                    stack.pop_back();
                }else{
                    return false;
                }
            }
            else{
                stack.push_back(s[i]);
            }
        }
        if(stack.empty()) return true;
        return false;
    }
};

Python

# 有效的括号
class Solution:
    def isValid(self, s: str) -> bool:
        # 使用一个列表模拟栈操作
        stack = []
        for i in s:
            # 右括号则匹配
            if i == ")":
                if stack and stack[-1]=="(":
                    del stack[-1]
                else:
                    return False
            elif i == "}":
                if stack and stack[-1]=="{":
                    del stack[-1]
                else:
                    return False
            elif i == "]":
                if stack and stack[-1]=="[":
                    del stack[-1]
                else:
                    return False
            # 左括号则进入栈中
            else:
                stack.append(i)

        if len(stack)==0:
            return True
        return False

合并两个有序数组(leetcode88)

C++

#include <vector>
using namespace std;
// 合并两个有序数组
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        // muns1无值则将nums2逐个添加到nums1中
        if(m==0){
            for(int i=0;i<n;i++){
                nums1[i] = nums2[i];
            }
        }
        // nums2无值则不做处理
        if(n==0){
            return;
        }
        // 定义i指向nums1当前处理位置,
        int i = m-1;
        // 定义j指向nums2当前处理位置,
        int j = n-1;
        for(int index=0;index<m+n;index++){
            if(i<0 || j<0) break;
            if(nums1[i]>nums2[j]){
                nums1[m+n-1-index] = nums1[i];
                i--;
            }else{
                nums1[m+n-1-index] = nums2[j];
                j--;
            }
        }
        // 如果nums2未处理完,则将nums2中未处理的值填入到nums1中
        while(j>=0){
            nums1[j] = nums2[j];
            j--;
        }
    }
};

Python

from typing import List
# 合并两个有序数组
class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        if m==0:
            for i in range(n):
                nums1[i] = nums2[i]
        if n==0:
            return
        i = m-1
        j = n-1
        for index in range(n+m):
            if i<0 or j<0:
                break
            # 判断将最大值向后填充
            if nums1[i]>nums2[j]:
                nums1[n+m-1-index] = nums1[i]
                i-=1
            else:
                nums1[n + m - 1 - index] = nums2[j]
                j -= 1
        # 如果nums2指针没走完,则将num2余下元素放置到nums1中
        if j>=0:
            for k in range(j+1):
                nums1[k] = nums2[k]
if __name__ == '__main__':
    # nums1 = [1, 2, 3, 0, 0, 0]
    # m = 3
    # nums2 = [2, 5, 6]
    # n = 3
    nums1 = [2,0]
    m = 1
    nums2 =[1]
    n=1
    s = Solution()
    s.merge(nums1,m,nums2,n)
    print(nums1)

LRU缓存(leetcode146)

C++

#include <unordered_map>
using namespace std;
// LRU缓存
struct DiListNode {
    int key;
    int val;
    DiListNode *pre;
    DiListNode *next;

    DiListNode() : key(0), val(0), pre(nullptr), next(nullptr) {}

    DiListNode(int key, int val) : key(key), val(val), pre(nullptr), next(nullptr) {}

    DiListNode(int key, int val, DiListNode *pre, DiListNode *next) : key(key), val(val), pre(pre), next(next) {}
};

class LRUCache {
public:
    // 总容量
    int capacities;
    // 已使用容量
    int used;
    // 虚拟头节点
    DiListNode* dummy;
    // 尾节点
    DiListNode* end;
    // 存储key与节点对应关系的harsh表
    unordered_map<int, DiListNode*> cache;

    LRUCache(int capacity) {
        capacities = capacity;
        used = 0;
        dummy= new DiListNode(-1,0);
        end = nullptr;
    }

    int get(int key) {
        // 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1。
        if(cache.find(key)!=cache.end()){   //存在
            DiListNode *cur = cache.find(key)->second;
            // 将cur移动到链表头后,返回val
            if(cur->pre==dummy) return cur->val;    // 原本在表头则无需移动
            movetohead(cur);
            return cur->val;
        }
        return -1;
    }

    void put(int key, int value) {
        // 如果关键字 key 已经存在,则变更其数据值 value ;
        if(cache.find(key)!=cache.end()){
            // 变更其值
            DiListNode*cur = cache.find(key)->second;
            cur->val = value;
            // 移动到链表头部
            if(cur->pre==dummy) return;
            movetohead(cur);
        } else{     // 如果不存在,则向缓存中插入该组 key-value 。
            if(used==capacities){       // 如果插入操作导致关键字数量超过 capacity ,则应该逐出最久未使用的关键字。
                cache.erase(end->key);
                end->pre->next = nullptr;
                end = end->pre;
                used--;
            }
            DiListNode *cur = new DiListNode(key, value);
            cache[key] = cur;
            cur->next = dummy->next;
            if(used!=0){
                dummy->next->pre = cur;
            }else{
                end = cur;
            }
            cur->pre = dummy;
            dummy->next = cur;
            used++;
        }
    }
    void movetohead(DiListNode* L){
        if(L==end){  // L是尾节点
            end->pre->next = nullptr;
            end = end->pre;
        }else{   //L是中间节点
            L->pre->next = L->next;
            L->next->pre = L->pre;
        }
        L->next = dummy->next;
        dummy->next->pre = L;
        L->pre = dummy;
        dummy->next = L;
    }
};

Python

# LRU缓存
# 定义双向链表用于
class DiLinkList:
    def __init__(self, key=0, val=0, pre=None, next=None):
        self.key = key
        self.val = val
        self.pre = pre
        self.next = next

class LRUCache:

    def __init__(self, capacity: int):
        # 缓存容量
        self.capacity = capacity
        # 已使用容量
        self.used = 0
        # 定义一个字典存储键值对
        self.harsh = {}
        # 定义虚拟头节点
        self.dummy = DiLinkList(-1)     # 取-1作为key
        # 定义链表尾节点
        self.end = None

    def get(self, key: int) -> int:
        if key in self.harsh:
            # 将对应节点移动到表头
            head = self.harsh[key]
            if head.pre==self.dummy:    # 是头节点则无需移动
                return self.harsh.get(key).val
            self.movetohead(head)
            return self.harsh.get(key).val
        return -1

    def put(self, key: int, value: int) -> None:
        # 如果关键词存在则变更其值,如果关键词不存在则插入,如果关键词超量则删除尾节点后在头节点插入
        # 存在
        if key in self.harsh:
            head = self.harsh[key]
            head.val = value
            # 移动到链表头
            if head.pre == self.dummy:  # 是头节点则无需移动
                return
            self.movetohead(head)
        else:
            # 超量先删除尾节点,
            if self.used==self.capacity:
                # 更新链表,删除
                self.harsh.pop(self.end.key)
                self.end.pre.next = None
                self.end = self.end.pre
                self.used -= 1

            # 插入,更新链表
            node = DiLinkList(key,value,self.dummy,self.dummy.next)
            if self.used !=0:   # 不是第一个节点
                self.dummy.next.pre = node
                self.dummy.next = node
            else:
                self.dummy.next = node
                self.end = node    # 是第一个节点

            # 更新harsh表
            self.harsh[key] = node
            # 更新已使用空间
            self.used+=1
    def movetohead(self,head):  # 将某个节点移动到链表头部

        if head.next == None:  # 是尾节点
            self.end = head.pre
            head.pre.next = None
        else:  # 不是尾节点
            head.pre.next = head.next
            head.next.pre = head.pre
        head.next = self.dummy.next
        head.pre = self.dummy
        self.dummy.next.pre = head
        self.dummy.next = head

if __name__ == '__main__':
    lRUCache = LRUCache(2)
    lRUCache.put(2, 1)
    lRUCache.put(1, 1)
    lRUCache.put(2, 3)
    lRUCache.put(4, 1)
    print(lRUCache.get(1))
    print(lRUCache.get(2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值