博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Learning Notes --- HashTable
阅读量:4101 次
发布时间:2019-05-25

本文共 2810 字,大约阅读时间需要 9 分钟。

LeetCode Learning Notes

463 Island Perimeter

method#1: Math

Since the adjacent edge cannot be counted into perimeter, and each island cell has four edges, what we need to do is counting how many island cells(ones) and pairs of neighbor(two adjacent island called one pair of neighbor), and the answer is 4 * island_num - 2 * neighbor_num(subtracting 2 * neighbor_num because 4 * cell_num counts adjacent edges twice)

method#2: Iteration

The most intuitive way. Just iterate each cell, if it is an island, then check its surroundings(up, down, left, right), and there are two cases:

1 when the cell sits in boundary, its boundary edge should counted into perimeter
2 when the cell does not sit in boundary, only when its neighbor is water then that edge can be counted into perimeter

method#3: Another Iteration way

This method is kind of wired but excellent. The thought is similar to method#2, but more concise and elegant. Since each pair of neighbor cells (the edge between them) with different values is part of the perimeter, just count the differing pairs horizontally and vertically. When it comes to vertical iteration, one simple way is to transpose the matrix. In python, map(list,zip(*matrix)) can easily and effectively transpose a matrix.

Author:

Code

import operatorclass Solution:    def islandPerimeter(self, grid: 'List[List[int]]') -> 'int':        # math        land, neighbor = 0, 0         for i in range(len(grid)):            for j in range(len(grid[i])):                if grid[i][j]==1:                    land+=1                    if i+1

136 Single Number

This question is pretty simple if no restrictions. But when it comes to requirement of linear running time complexity and no use of extra memory, it becomes kind of tricky. One approach meets the requirement is using XOR operation. Since:

x ^ 0 == xx ^ x == 0x ^ y ^ x == y

The code comes easily and clearly:

code

class Solution:    def singleNumber(self, nums):        x = 0        for num in nums:            x ^= num        return x

953 Verifying Alien Dictionary

Approach

Actually, as long as the order is given, whether it is earth or alien does not matter. Then compare every two pair of words, from head to tail. (It works since if a<b, b<c, then a<c)

class Solution:    def isAlienSorted(self, words, order):        m = {
c: i for i, c in enumerate(order)} for i in range(len(words)-1): if not self.compare(words[i], words[i+1], m): return False return True def compare(self, s1, s2, m): len1, len2 = len(s1), len(s2) for i in range(min(len1,len2)): if m[s1[i]] > m[s2[i]]: return False elif m[s1[i]] == m[s2[i]]: if len1>len2: return False else: return True

转载地址:http://ywksi.baihongyu.com/

你可能感兴趣的文章
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>
面试想拿 10K,HR 说我只配7k?
查看>>
副业过万的程序员都知道的网站有哪些
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
乔布斯18岁求职信拍卖价22.24万美元,值吗?
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
假如计算机是中国人发明的,那代码应该这么写
查看>>
触目惊心:比特币到底消耗了多少能源?
查看>>
面试官:简历上敢写技术精通?那我就不客气了!
查看>>
如何判断一家互联网公司要倒闭了?
查看>>
想快速上手机器学习?来看下这个 GitHub 项目!
查看>>
GitHub 标星 3.6k,一本开源的深度学习中文教程!
查看>>
9 款你不能错过的 JSON 工具
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
200页!分享珍藏很久的Python学习知识手册(附链接)
查看>>
程序员之神
查看>>
4 岁小女孩给 Linux 内核贡献提交
查看>>