Do it


  • Home

  • Tags

  • Archives

Leetcode 667. Beautiful Arrangement II

Posted on 2019-04-09 |
Words count in article: 356 | Reading time ≈ 1
Quesiton Solution1对于一个序列[1,2,3,4]: 从index 1开始逆转序列 [1,4,3,2],k = 2 从index 2开始逆转序列 [1,4,2,3],k = 3 … 但是这样速度很慢。 123456class Solution: def constructArr ...
Read more »

Leetcode 495. Teemo Attacking

Posted on 2019-04-08 |
Words count in article: 185 | Reading time ≈ 1
Question Solution1考虑前后时间点之差和持续时间的大小。 1234567891011class Solution: def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: ...
Read more »

leetcode 565. Array Nesting

Posted on 2019-04-08 |
Words count in article: 169 | Reading time ≈ 1
Question Solution分析,我们可以将nums中的数分成几组。如果某组的第一个数出现两次,则在第二次出现的时候就需要退出循环,并且已经达到最大的step。将第一个循环的数从nums剔除或者不考虑,继续对下一个循环进行计算step。但是剔除会很消耗时间,因为需要复制和遍历来找到需要剔除的数 ...
Read more »

Leetcode 238. Product of Array Except Self

Posted on 2019-04-07 |
Words count in article: 147 | Reading time ≈ 1
Question Solution因为不能用除法,所以只能考虑乘法。考虑两次乘法,第一次从左乘到右,第二次从右乘到左。 123456789101112131415class Solution: def productExceptSelf(self, nums: List[int]) -> ...
Read more »

Leetcode 695/200. Max Area of Island

Posted on 2019-04-07 |
Words count in article: 424 | Reading time ≈ 2
Question1 Solution首先考虑怎么计算面积。考虑用递归。如果当前元素是1,那么我们就要考虑其上下左右四个元素是否是1,以及更外面的元素是否是1.同时在计算的时候,要将当前元素赋值为0,防止重复计算。 12345678910111213141516171819202122class So ...
Read more »

Leetcode 442. Find All Duplicates in an Array

Posted on 2019-04-07 |
Words count in article: 223 | Reading time ≈ 1
Question Solution1考虑使用字典,如果当前元素已经在字典中,添加到output中。 12345678910class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: dicc ...
Read more »

Leetcode 969. Pancake Sorting

Posted on 2019-04-07 |
Words count in article: 185 | Reading time ≈ 1
Question Solution注意到,A是1-n的排列。考虑A中最大元素是不是在其应该所在的位置。如果在则选择第2大的元素。如果不在就翻转。 12345678910111213class Solution: def pancakeSort(self, A: List[int]) -> ...
Read more »

Leetcode 950. Reveal Cards In Increasing Order

Posted on 2019-04-06 |
Words count in article: 320 | Reading time ≈ 1
Question Solution1考虑先将原序列逆序排列。例子:[19, 17, 13, 11, 10, 8, 7, 5, 3, 2]。新序列为new,依次将第一个元素添加到其中。 1: [19] 2: [17, 19] 3: [13, 17, 19] 但是这时候发现不对了,因为将13移除后,序 ...
Read more »

Leetcode 136. Single Number

Posted on 2019-04-06 |
Words count in article: 69 | Reading time ≈ 1
Question Solution1使用字典 1234567891011class Solution: def singleNumber(self, nums: List[int]) -> int: dictt = {} for num i ...
Read more »

Leetcode 283. Move Zeroes

Posted on 2019-04-05 |
Words count in article: 163 | Reading time ≈ 1
Question Solution考虑到可能多个0排列在一起,所以不能前后调换来做。使用两个指针,第一个指向0,第二个指向0之后的第一个非0元素, 然后替换。 123456789101112131415161718class Solution: def moveZeroes(self, num ...
Read more »
1…4567
JW

JW

64 posts
14 tags
GitHub E-Mail
0%
© 2019 JW
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4
访客数 人 总访问量 次