TommyCpp / AhaMoment

Note, Practice project, leetcode and other stuff.
1 stars 3 forks source link

Daily check in #3

Open TommyCpp opened 6 years ago

TommyCpp commented 6 years ago

496. Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/description/ 维护一个Map用于存储每个字母对应的结果

TommyCpp commented 6 years ago

500. Keyboard Row

https://leetcode.com/problems/keyboard-row/description/

TommyCpp commented 6 years ago

没有什么难度,但是有一种解决方案使用Stream+正则表达式可以大大减少代码量

TommyCpp commented 6 years ago

412. Fizz Buzz

https://leetcode.com/problems/fizz-buzz/description/

TommyCpp commented 6 years ago

669. Trim a Binary Search Tree

https://leetcode.com/problems/trim-a-binary-search-tree/description/ 经典算法题了

TommyCpp commented 6 years ago

给定一棵二叉搜索树和一个上下界[L,R],要求筛选出在[L,R]之间的数,分情况讨论递归处理一下就可以得出结论

TommyCpp commented 6 years ago

575. Distribute Candies

https://leetcode.com/problems/distribute-candies/description/

TommyCpp commented 6 years ago

记录下有多少种独特的candy,得出种类数N,令糖果数M,取min(N, M/2) 可以使用Set来实现 当种类数N超过糖果数M的一半的时候,就可以直接返回结果了

TommyCpp commented 6 years ago

463. Island Perimeter

https://leetcode.com/problems/island-perimeter/description/

TommyCpp commented 6 years ago

为什么暴力算法可以超过80%的Java提交:confused: 再提交一次竟然超过91%的Java提交:scream:???

TommyCpp commented 6 years ago

566. Reshape the Matrix

https://leetcode.com/problems/reshape-the-matrix/description/

注意代码不涉及原始矩阵的行数

TommyCpp commented 6 years ago

766. Toeplitz Matrix

https://leetcode.com/problems/toeplitz-matrix/description/

研究出迭代顺序即可,Repo中代码先按照增加行index,再处理列index

具体来说,

TommyCpp commented 6 years ago

442. Find All Duplicates in an Array

https://leetcode.com/problems/find-all-duplicates-in-an-array/discuss/92390/Python-O(n)-time-O(1)-space

题目要求一个O(1)空间复杂度,O(n)时间复杂度的算法

用数组下标做Hash的想法 9150e4e5ly1fg5ka1who6j20h80fun5r

TommyCpp commented 6 years ago

682. Baseball Game

https://leetcode.com/problems/baseball-game/description/

用栈处理,可以考虑在一轮迭代中同时完成结果累加和压栈步骤

TommyCpp commented 6 years ago

637. Average of Levels in Binary Tree

https://leetcode.com/problems/average-of-levels-in-binary-tree/description/

先构建每一层的数组再求平均值

TommyCpp commented 6 years ago

513. Find Bottom Left Tree Value

https://leetcode.com/problems/find-bottom-left-tree-value/description/ 注意题目条件中要求最后一行最左侧的元素,应当从最后一行着手

TommyCpp commented 6 years ago

515. Find Largest Value in Each Tree Row

https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/

注意Python3中如何获得int的最小值使用sys模块

TommyCpp commented 6 years ago

496. Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/description/

TommyCpp commented 6 years ago

104. Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ 经典算法题了

TommyCpp commented 6 years ago

540. Single Element in a Sorted Array

https://leetcode.com/problems/single-element-in-a-sorted-array/description/

几种思路:

TommyCpp commented 6 years ago

136. Single Number

https://leetcode.com/problems/single-number/description/

TommyCpp commented 6 years ago

647. Palindromic Substrings

https://leetcode.com/problems/palindromic-substrings/description/ 动态规划问题,核心是认识到构成回文的充要条件是除去其首尾位置的字符之外的字符串是回文

TommyCpp commented 6 years ago

413. Arithmetic Slices

https://leetcode.com/problems/arithmetic-slices/description/

  1. 动态规划问题
    • 子问题:A[i]是否是Arithmetic?
    • 状态转移方程:dp[i+1] = 1 + dp[i] if A[i] - A[i-1] == A[i-1] - A[i-2]
  2. 观察,如果Slice长度为3时候能找到多少个,当长度为4的时候能找到多少个?
    • 可以得出规律,针对长度为n的Arithmetic Slices,其数量是num(n-1)*num(n-2) / 2
TommyCpp commented 6 years ago

406. Queue Reconstruction by Height

https://leetcode.com/problems/queue-reconstruction-by-height/description/

TommyCpp commented 6 years ago

485. Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones/description/ 暴力算法即可

TommyCpp commented 6 years ago

226. Invert Binary Tree

https://leetcode.com/problems/invert-binary-tree/description/ 官方吐槽,最为致命

TommyCpp commented 6 years ago

508. Most Frequent Subtree Sum

https://leetcode.com/problems/most-frequent-subtree-sum/description/ 使用Python Counter API处理

TommyCpp commented 6 years ago

260. Single Number III

https://leetcode.com/problems/single-number-iii/description/ 同样适用Counter API处理,但是应当有一个使用bit操作处理的方法

TommyCpp commented 6 years ago

526. Beautiful Arrangement

https://leetcode.com/problems/beautiful-arrangement/description/ 回溯法解决 具体来说

TommyCpp commented 6 years ago

784. Letter Case Permutation

https://leetcode.com/problems/letter-case-permutation/description/ 回溯法解决

TommyCpp commented 6 years ago

739. Daily Temperatures

https://leetcode.com/problems/daily-temperatures/description/ 用Stack处理

TommyCpp commented 6 years ago

553. Optimal Division

https://leetcode.com/problems/optimal-division/description/ 推导出固定式,即永远用第一项除以其他所有项的余数...

TommyCpp commented 6 years ago

748. Shortest Completing Word

https://leetcode.com/problems/shortest-completing-word/description/ 为了提高效率,当word长度大于等于目前最短的符合条件的word的时候直接不判断

TommyCpp commented 6 years ago

451. Sort Characters By Frequency

https://leetcode.com/problems/sort-characters-by-frequency/description/ Counter API是真的好用

TommyCpp commented 6 years ago

693. Binary Number with Alternating Bits

https://leetcode.com/problems/binary-number-with-alternating-bits/description/ 注意右移的语法,本题利用右移和1不断取出最右侧bit

A >>=1 //右移一位
TommyCpp commented 6 years ago

762. Prime Number of Set Bits in Binary Representation

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/description/

Integer.bitCount(Integer i); //统计bit数量
TommyCpp commented 6 years ago

609. Find Duplicate File in System

https://leetcode.com/problems/find-duplicate-file-in-system/description/ 分解String后用HashTable处理

关于几个Follow Up Question 相关讨论 https://leetcode.com/problems/find-duplicate-file-in-system/discuss/104120/Follow-up-questions-discussion

  1. Imagine you are given a real file system, how will you search files? DFS or BFS?

    DFS

  2. If the file content is very large (GB level), how will you modify your solution?

    用 Hash 函数来处理内容

  3. If you can only read the file by 1kb each time, how will you modify your solution?
  4. What is the time complexity of your modified solution?
  5. What is the most time-consuming part and memory consuming part of it? How to optimize?
  6. How to make sure the duplicated files you find are not false positive?
TommyCpp commented 6 years ago

520. Detect Capital

https://leetcode.com/problems/detect-capital/description/

TommyCpp commented 6 years ago

796. Rotate String

https://leetcode.com/problems/rotate-string/description/

TommyCpp commented 6 years ago

606. Construct String from Binary Tree

https://leetcode.com/problems/construct-string-from-binary-tree/description/ 分情况讨论,

TommyCpp commented 6 years ago

198. House Robber

https://leetcode.com/problems/house-robber/description/ 动态规划问题 子问题:dp[i]表示nums[:i]的最大偷窃值 状态转移方程:dp[i+1] = max(dp[i-1]+nums[i+1], dp[i])

为什么在这里子问题中不需要区分偷窃的房子中包不包括最后一个?

  1. 如果没有偷窃最后一个房子,容易证明dp[i] = dp[i-1],这个时候按照转移方程我们可以通过dp[i-1]+nums[i+1]这条路径获得正确的值
  2. 如果有偷窃最后一个房子,那么则不能偷窃i+1个房子
  3. 所以在任何情况下,我们都不需要判断dp[i]+nums[i+1]
TommyCpp commented 6 years ago

389. Find the Difference

https://leetcode.com/problems/find-the-difference/description/ Counter API

  1. 注意Counterdict的子类

  2. Counter常见操作

    sum(c.values())                 # total of all counts
    c.clear()                       # reset all counts
    list(c)                         # list unique elements
    set(c)                          # convert to a set
    dict(c)                         # convert to a regular dictionary
    c.items()                       # convert to a list of (elem, cnt) pairs
    Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs
    c.most_common()[:-n-1:-1]       # n least common elements
    +c                              # remove zero and negative counts
  3. Counter的加减操作

    c = Counter(a=3, b=1)
    d = Counter(a=1, b=2)
    c - d # 逐个相减,保留正数
    c + d #逐个相加
    c & d #min(c[x], d[x]) 
    c | d  # max(c[x],d[x])
TommyCpp commented 6 years ago

462. Minimum Moves to Equal Array Elements II

https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/

注意不需要判断数组长度是否是奇数,因为即便是偶数,用len(nums)//2和用len(nums)//2-1位置的元素作为中位数不存在区别

TommyCpp commented 6 years ago

448. Find All Numbers Disappeared in an Array

https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 与#442相似,用数组下标做Hash的想法,妙啊!

TommyCpp commented 6 years ago

819. Most Common Word

https://leetcode.com/problems/most-common-word/description/ 注意首先要对paragraph进行一些处理(全部变成最小化,去除标点符号)

TommyCpp commented 6 years ago

717. 1-bit and 2-bit Characters

https://leetcode.com/problems/1-bit-and-2-bit-characters/description/ 感觉类似哈夫曼编码的思想 如果出现1,则后一位不管是0或1都无所谓,所以跳过

TommyCpp commented 6 years ago

347. Top K Frequent Elements

https://leetcode.com/problems/top-k-frequent-elements/description/ 不说了,Counter API

TommyCpp commented 6 years ago

781. Rabbits in Forest

https://leetcode.com/problems/rabbits-in-forest/description/ 从森林里所有的兔子中抽样一部分(也有可能是全部),询问有多少兔子和他们一个颜色,得出answers 因此,answers中每个元素加一就可以形成一个集合。 例如[0,0,1,2,2]对应集合[1,1,2,3,3] 因为要求最小值,所以直观上来说我们将相同数量的集合**尽可能***认为是同一集合,上例中包含三种不同大小的集合. 但是又观察到[1,1]不可能是一个集合,因为每个兔子只可能有一种颜色 因此, 对于n个大小为1的集合,至少对应n个兔子 又因为, [3,3]肯定凑不成一个大小为3的集合, 因此肯定有一个隐藏的兔子,[3,3]对应3个兔子 [2]肯定凑不成一个大小为2的集合, 因此肯定还有一个隐藏的兔子,[2]对应2个兔子

又考虑这样一种情况[0,0,1,1,1] 后半部分,对应[2,2,2]集合,这个集合可以凑成一个包含两个元素的集合,但是还多一个元素。因此必须凑成两个集合,最终需要4个元素。

TommyCpp commented 6 years ago

817. Linked List Components

https://leetcode.com/problems/linked-list-components/description/

注意题意是让我们从G中找出list中的联通部分 首先考虑极端情况,如果G中每个元素之间都不联通,则返回G的长度

考虑从表头开始遍历,每遍历到一个节点就去G中找,找到了则继续,如果没有找到,说明联通部分断开了,结果加一,继续寻找(注意此时不需要再检测当前节点是否在G内部,因为题目中给定了G中元素均取自list)。

TommyCpp commented 6 years ago

171. Excel Sheet Column Number

https://leetcode.com/problems/excel-sheet-column-number/description/

没啥好说的Easy题

TommyCpp commented 6 years ago

238. Product of Array Except Self

https://leetcode.com/submissions/detail/157501055/ 创建两个数组before和after,分别记录nums[i]前的所有数之积和nums[i]后所有数之积,然后把每个i对应的before[i]after[i]相乘就可以得到结果