Leetcode刷题 2020.7.15

2023-12-16 07:08
文章标签 leetcode 15 刷题 2020.7

本文主要是介绍Leetcode刷题 2020.7.15,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述两数之和

这道题中的next指针的安排是重点。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *h = l1, *p = l1;while(l1 || l2){if(!l1)p -> next = new ListNode(0), l1 = p -> next;//此行处理为重点!!!if(!l2)l2 = new ListNode(0);l1 -> val += l2 -> val;if(l1 -> val > 9){if(l1 -> next)l1 -> next -> val ++;else l1 -> next = new ListNode(1);l1 -> val %= 10;} p = l1;     //注意保留之前的结点,防止之后的结点为空l1 = l1 -> next, l2 = l2 -> next;}return h;}
};

在这里插入图片描述无重复的最长子串
运行错误的代码!!!

class Solution {
public:int lengthOfLongestSubstring(string s) {vector<int> ve(27, -1);int len = s.size(), t, Max = 0, k = 0;for (int i = 0; i < len; i++){t = s[i] - 'a';if(ve[t] == -1) ve[t] = i;else {Max = max(Max, i - k);for (int j = k; j < ve[t]; j++) ve[s[j] - 'a'] = -1;k = ve[t] + 1;ve[t] = i;}}if(!k) Max = len;return Max;}};

正解,滑动窗口!!!!!!!

class Solution {
public:int lengthOfLongestSubstring(string s) {unordered_map<char, int> tab;int len = s.size(), Max = 0, j = 0;for (int i = 0; i < len; i++){while(!tab.count(s[j]) && j < len) tab[s[j]] = 1, j++;if(j < len) tab.erase(s[i]);Max = max(Max, j - i);}return Max;}};

代码简化!!!!

class Solution {
public:int lengthOfLongestSubstring(string s) {unordered_map<char, int> tab; int len = s.size(), Max = 0;for (int i = 0, j = 0; j < len; j++){tab[s[j]]++;while(tab[s[j]] > 1) tab[s[i++]]--;Max = max(Max, j - i + 1);}return Max;}};

这篇关于Leetcode刷题 2020.7.15的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/499579

相关文章

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param