This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #https://leetcode.com/problems/island-perimeter/description/ | |
| class Solution(object): | |
| def islandPerimeter(self, grid): | |
| """ | |
| :type grid: List[List[int]] | |
| :rtype: int | |
| """ | |
| return sum(sum(map(operator.ne, [0] + row, row + [0])) | |
| for row in grid + map(list, zip(*grid))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://leetcode.com/problems/max-area-of-island/description/ | |
| # complex number make things simplers | |
| class Solution(object): | |
| def maxAreaOfIsland(self, grid): | |
| """ | |
| :type grid: List[List[int]] | |
| :rtype: int | |
| """ | |
| grid = {i + j*1j: val for i, row in enumerate(grid) for j, val in enumerate(row)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. | |
| #For example: | |
| #Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. | |
| #Follow up: | |
| #Could you do it without any loop/recursion in O(1) runtime? | |
| class Solution(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. | |
| #Find all the elements of [1, n] inclusive that do not appear in this array. | |
| #Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. | |
| #Input: | |
| #[4,3,2,7,8,2,3,1] | |
| #Output: | |
| #[5,6] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. | |
| int getSum(int a, int b) { | |
| if (b) | |
| return getSum(a ^ b, (a & b) << 1); | |
| return a; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://leetcode.com/problems/find-the-difference/description/ | |
| # aka: remove all duplicated elements | |
| # idea: a XOR a = 0 | |
| class Solution(object): | |
| def findTheDifference(self, s, t): | |
| """ | |
| :type s: str | |
| :type t: str | |
| :rtype: str | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #https://leetcode.com/problems/move-zeroes/description/ | |
| class Solution(object): | |
| def moveZeroes(self, nums): | |
| """ | |
| :type nums: List[int] | |
| :rtype: void Do not return anything, modify nums in-place instead. | |
| """ | |
| zero = 0 | |
| for i in xrange(0, len(nums)): | |
| if nums[i] != 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # link: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ | |
| # Definition for a binary tree node. | |
| # class TreeNode(object): | |
| # def __init__(self, x): | |
| # self.val = x | |
| # self.left = None | |
| # self.right = None | |
| class Solution(object): | |
| def findTarget(self, root, k): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = 1000001 | |
| b = 2 | |
| def mdivide(a, b): | |
| i = 1 | |
| t1 = b | |
| while t1 < a: | |
| if (t1<<1) < a: | |
| t1<<=1 | |
| i<<=1 |
We can make this file beautiful and searchable if this error is corrected: It looks like row 6 should actually have 19 columns, instead of 10 in line 5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ,2011,2011,2011,2012,2012,2012,2013,2013,2013,2014,2014,2014,2015,2015,2015,2016 (*),2016 (*),2016 (*) | |
| ,Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2),Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2),Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2),Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2),Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2),Diện tích(Km2),Dân số trung bình (Nghìn người),Mật độ dân số (Người/km2) | |
| CẢ NƯỚC,330957.6,87860.4,265.5,330951.1,88809.3,268.3,330972.4,89759.5,271.2,330966.9,90728.9,274,330966.9,91713.3,277,331230.8,92695.1,280 | |
| Đồng bằng sông Hồng,21068.1,20066.1,952.4,21050.9,20274.9,963.1,21059.3,20481.9,972.6,21060.0,20705.2,983,21060.0,20925.5,994,21260.3,21133.8,994 | |
| Hà Nội,3328.9,6761.3,2031.1,3323.6,6865.2,2065.6,3324.3,6977.0,2098.8,3324.5,7095.9,2134,3324.5,7216.0,2171,3358.9,7328.4,2182 | |
| Vĩnh Phúc,1236.5,1011.4,818.0,1236.5,1022.4,826.8,1238.6,1029.4,831 |