leetcode

LeetCode--139--单词拆分(python)

你。 提交于 2020-01-06 09:07:33
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。 说明: 拆分时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。 示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。 示例 2: 输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。 注意你可以重复使用字典中的单词。 示例 3: 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false 1 class Solution: 2 def wordBreak(self, s: str, wordDict: List[str]) -> bool: 3 dp = [False for _ in range(len(s)+1)] 4 dp[0]=True 5 for i in range(1,len(s)+1

LeetCode All in One 题目讲解汇总(持续更新中...)

假装没事ソ 提交于 2020-01-06 03:10:12
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example: Input: [1,2,3,4,5] 1 / \ 2 3 / \ 4 5 Output: [[4,5,3],[2],[1]] Explanation: 1. Removing the leaves [4,5,3] would result in this tree: 1 / 2 2. Now removing the leaf [2] would result in this tree: 1 3. Now removing the leaf [1] would result in the empty tree: [] Credits: Special thanks to @elmirap for adding this problem and creating all test cases. 这道题给了我们一个二叉树,让我们返回其每层的叶节点,就像剥洋葱一样,将这个二叉树一层一层剥掉,最后一个剥掉根节点。那么题目中提示说要用DFS来做,思路是这样的,每一个节点从左子节点和右子节点分开走可以得到两个深度

亚麻:Longest Palindrome

假如想象 提交于 2020-01-04 14:57:26
亚麻 的OA题 代码在leetcode 测试通过; leetcode 链接 https://leetcode.com/problems/longest-palindrome/description/    class Solution { public: int longestPalindrome(string s) { if( s.empty () ) return 0; unordered_map< char, int > res; int max =0; bool oflag = false; for (auto& e: s) res[e]+=1; for (auto& e: res){ if (e.second%2 == 0) max += e.second; else { oflag = true ; max += e.second -1; } } if(oflag) return max +1; else return max; } }; 来源: https://www.cnblogs.com/HisonSanDiego/p/8296768.html

Leetcode 4. Median of Two Sorted Arrays(二分)

纵饮孤独 提交于 2020-01-04 01:18:58
4. Median of Two Sorted Arrays 题目链接: https://leetcode.com/problems/median-of-two-sorted-arrays/ Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 题意: 给出两个数组nums1,nums2,数组里面的数都是单增的,要求在log(n+m)的时间内,找出两个数组合并后的中位数。 题解: 最近看了看leetcode的题,感觉还是挺有意思的。 这个题就要充分利用中位数的性质来解,中位数,我们首先最容易想到的就是中间位置的数。

NEW!【51CTO学院周年庆直播】6.13开始18场免费直播等你来!

爱⌒轻易说出口 提交于 2020-01-04 01:02:35
51CTO学院六周年大咖直播开始啦,谈前景,说趋势,聊转型,定规划,给方法,解难题,助通关,拿高薪,6月13日晚8点开始,18场系列直播,师傅零距离带徒弟,学习更享豪礼哟! 6月13日-6月27日公开课总览: 6月13日 周四 晚8点开始,QQ群内直播。群号:630179339 授课老师:新任帮主 授课主题:从入门到入魔高级网工之路 直播内容: 1.什么网络?为什么要学习网络? 2.学习网络能当饭吃吗? 3.没有基础能够跟快更好的入门呢? 4.网络行业都有什么证书及需不需要考证? 5.年薪60w与年薪16w能力区别 6.年薪连续翻三倍的好办法 7.互联网裁员,网工害怕吗 8.单凭证书就能混好吗? 9.网工与白菜的差距 直接与老师交流-点我入群>> 新任帮主相关课程: https://edu.51cto.com/lecturer/2815930.html 6月14日 周五 晚8点开始,QQ群内直播。群号:439813197 授课老师:徐培成 授课主题:45分钟演绎快速接入微信支付, 从零开始+企业账号操刀,助力网 站扫描付款! 直播内容: 1.移动支付的多种场景介绍 2.支付原理分析与架构说明 3.秘钥、证书、商户、服务号等专业术语讲解与逻辑梳理 4.结合真实企业账号(非沙箱),完成接入开发,实现支付转账与账户管理 直接与老师交流-点我入群>> 徐老师相关课程: https:/

LeetCode All in One 题目讲解汇总(持续更新中...)

会有一股神秘感。 提交于 2020-01-02 01:23:10
Reverse a linked list from position m to n . Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 很奇怪为何没有倒置链表之一,就来了这个倒置链表之二,不过猜也能猜得到之一就是单纯的倒置整个链表,而这道作为延伸的地方就是倒置其中的某一小段。对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点。这道题的要求是只通过一次遍历完成,就拿题目中的例子来说,变换的是2,3,4这三个点,我们需要找到第一个开始变换结点的前一个结点,只要让pre向后走m-1步即可,为啥要减1呢,因为题目中是从1开始计数的,这里只走了1步,就是结点1,用pre指向它。万一是结点1开始变换的怎么办,这就是我们为啥要用dummy结点了,pre也可以指向dummy结点。然后就要开始交换了,由于一次只能交换两个结点,所以我们按如下的交换顺序: 1 -> 2 -> 3 -> 4 -> 5 -> NULL 1 -> 3 -> 2 -> 4 -> 5 -> NULL

LeetCode All in One 题目讲解汇总(持续更新中...)

孤街醉人 提交于 2020-01-02 01:18:27
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list Algorithm of Insertion Sort: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements

LeetCode All in One 题目讲解汇总(持续更新中...)

拟墨画扇 提交于 2020-01-02 01:12:07
Given a linked list, return the node where the cycle begins. If there is no cycle, return null . To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1 , then there is no cycle in the linked list. Note: Do not modify the linked list. Example 1: Input: head = [3,2,0,-4], pos = 1 Output: tail connects to node index 1 Explanation: There is a cycle in the linked list, where tail connects to the second node. Example 2: Input: head = [1,2], pos = 0 Output: tail connects to node index 0

【优质blog、网址】置顶

徘徊边缘 提交于 2020-01-02 01:11:13
一、大公司等技术blog: blog1: http://blog.csdn.net/mfcing/article/details/51577173 blog2: http://blog.csdn.net/tengdazhang770960436/article/details/49963983 美团点评技术博客: https://tech.meituan.com/ 龙果学院博客 http://www.roncoo.com/article/index 董的博客--yarn 简书--源码解析spark,streaming Spark on Yarn遇到的几个问题 http://www.cnblogs.com/Scott007/p/3889959.html spark性能调优03-shuffle调优 http://www.cnblogs.com/lifeone/p/6434840.html Spark源码系列(二)RDD详解 http://www.cnblogs.com/cenyuhai/p/3779125.html Spark性能优化指南——基础篇 https://tech.meituan.com/spark-tuning-basic.html 使用 Spark MLlib 做 K-means 聚类分析 http://blog.csdn.net/paicmis/article

LeetCode 31. Next Permutation

大憨熊 提交于 2020-01-01 13:08:41
题目描述(中等难度) 这道题的的难度我觉得理解题意就占了一半。题目的意思是给定一个数,然后将这些数字的位置重新排列,得到一个刚好比原数字大的一种排列。如果没有比原数字大的,就升序输出。 关键就是刚好是什么意思?比如说原数字是 A,然后将原数字的每位重新排列产生了 B C D E,然后把这 5 个数字从小到大排列,比如是 D A B E C ,那么,我们要找的就是 B,就是那个刚好比 A 大的数字。 再比如 123,其他排列有 132,213,231,312,321,从小到大排列就是 123 132 213 231 312 321,那么我们要找的就是 132。 题目还要求空间复杂度必须是 O(1)。 #解法一 我们想几个问题。 要想使得数字变大,只要任意一位变大就可以。 要想得到刚好大于原来的数字,要变个位。 这里变大数字,只能利用交换。 如果从个位开始,从右往左进行,找一个比个位大的,交换过来,个位的数字交换到了更高位,由于个位的数字较小,所以交换过去虽然个位变大了,但数字整体变小了。例如 1 3 2,把 2 和 3 交换,变成 1 2 3,个位变大了,但整体数字变小了。 个位不行,我们再看十位,如果从十位左边找一个更大的数字交换过来,和个位的情况是一样的,数字会变小。例如 4 1 2 3,把 2 和 4 交换,2 1 4 3,数字会变小。如果从右边找一个更大的数字交换过来