
LeeCode&其它例题
无
仗剑天涯CC
博客都是学习过程中总结的笔记,供大家参考,有错误可以指出来
展开
-
LeeCode74 搜索二维矩阵
先看下题目看到这个题我第一时间想的是二分查找,只不过跟官方不同的是,我的是外层遍历,找到可能存在target的行,然后内层二分查找,判断具体有没有target这个数。我觉得我的想法也可以实现,只不过题目要求用高效的方法求解,遍历明显不太高效。看看官方的程序class Solution {public: bool searchMatrix(vector<vector<int>> matrix, int target) { auto.原创 2021-03-31 14:49:05 · 212 阅读 · 0 评论 -
LeeCode61 旋转链表
给一个链表的头结点,旋转链表,是每一个结点右移k个位置输入:head = [1,2,3,4,5], k = 2输出:[4,5,1,2,3]struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next)原创 2021-03-27 10:04:42 · 125 阅读 · 0 评论 -
判断单链表中是否存在环
1、使用快慢指针慢指针每次移动一个结点,快指针每次移动两个结点,快指针移动的快,必将先进入环,待慢指针移动进环内,就有点像追及问题了,快指针移动的快,当p1 == p2 时,就说明快慢指针相遇了,即链表有环。懒得画图,就描述一下算了。。。bool hasCycle(ListNode *head) { if(head == null) return false; ListNode *p1 = head,*p2=head; wh.原创 2021-03-23 17:40:51 · 453 阅读 · 0 评论 -
LRU缓存算法
LRU 缓存机制可以通过哈希表辅以双向链表实现,我们用一个哈希表和一个双向链表维护所有在缓存中的键值对。双向链表按照被使用的顺序存储了这些键值对,靠近头部的键值对是最近使用的,而靠近尾部的键值对是最久未使用的。哈希表即为普通的哈希映射(HashMap),通过缓存数据的键映射到其在双向链表中的位置。#include <iostream>#include <unordered_map>using namespace std;struct ListedNode{原创 2021-03-23 16:20:53 · 140 阅读 · 0 评论 -
链表翻转
class Solution {public: ListNode* reverseList(ListNode* head) { // 迭代 ListNode* pre = nullptr, * cur = head, * next; while (cur) { next = cur->next; cur->next = pre; pre = cur; .原创 2021-03-23 11:39:19 · 216 阅读 · 0 评论 -
翻转字符串
#include <iostream>using namespace std;char* reverse(char* str){ if (str == NULL || str == "\0") return str; char* start=str, *end=str; while (*end != '\0') { end++; } --end; char temp; while (start < end) { temp = *start; *.原创 2021-03-09 16:28:32 · 96 阅读 · 0 评论 -
LeeCode 21&25 合并两个(K个)有序链表
关于合并两个有序链表,LeeCode官方提供了两种解决办法;一、迭代listnode* mergetwolist(listnode* l1, listnode* l2) // 迭代{ listnode* prehead = new listnode(-1); //创建哨兵结点 listnode* pre = prehead; //创建pre指针, while (l1 != nullptr && l2 != nullptr) {原创 2021-03-04 18:30:05 · 205 阅读 · 1 评论 -
判断哪个函数调用会有问题?
#include <iostream>using namespace std;class A{public: void FunctionA() { cout << "FunctionA" << endl;// cout << x; } virtual void FunctionB() { cout << "FunctionB" << endl; } static void FunctionC() { cout.原创 2021-03-03 17:45:46 · 246 阅读 · 1 评论 -
LeeCode 27 移除元素,返回数组新长度
一、利标准库提供的移除算法remove()int removeElement(vector<int>& nums, int val) { remove(nums.begin(), nums.end(), val); return nums.size();}二、利用vector容器自带的删除函数erase()int removeElement(vector<int>& nums, int val) { auto it = nums.begin()原创 2021-03-03 16:37:19 · 266 阅读 · 1 评论 -
LeeCode 26 删除排序数组中的重复项,返回数组新长度
一、利用算法中的unique函数实现去重的目的,unique函数是C++中的标准库函数,包含于<algorithm>头文件中。功能是将数组中相邻的重复元素去除。但是本质上是将重复的元素移动到数组的末尾,最后再将迭代器末尾指向不重复序列的下一个元素。unique函数使用前一般还要用到sort函数排序、erase函数,以达到彻底清除重复元素的目的。int remove(vector<int>& nums){ sort(nums.begin(),nums.end());.原创 2021-03-03 15:56:26 · 276 阅读 · 1 评论 -
LeeCode 20.有效的括号
问题:源程序加上了自己的注释bool isValid(string s) // Leecode 20 { if (s.size() % 2 == 1) return false; unordered_map<char, char> pairs = { //哈希表的键为右括号,值为相同类型的左括号 {')','('}, {']','['}, {'}','{'} }; stack<char> sta; for (char c : s..原创 2021-03-02 18:47:02 · 311 阅读 · 1 评论 -
编写一个函数,实现将char类型的字符串,循环右移n个位置
例如 abcdefg 右移2个位置变成 fgabcdevoid LoopMove(char* pStr, int n){ int str_n = strlen(pStr); if (str_n == 0 || n < 0) return; int st = n % str_n; char temp[128]; memcpy(temp,pStr+str_n-st,st); memcpy(temp+st,pStr,str_n-st); memcpy(pStr, temp, s.原创 2021-03-01 13:12:26 · 1712 阅读 · 0 评论