Zhuanli&Blog


  • 首页

  • 标签

  • 分类

  • 归档

数据库的导入导出工具

发表于 2018-04-19 | 分类于 mysql

导出工具ncexpdbstr

用法:

1
./ncexpdbstr -u ncm   -p ncm -d ncm -o ncmsql.str

导入工具ncupdatedbstr

用法:

1
./ncupdatedbstr  -u ncm -p ncm -d ncm -f ncmsql.str

leetcode 120. Triangle

发表于 2018-04-13 | 分类于 leetcode

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
#bottom-up
#dp[i] = min(dp[i],dp[i+1]) + triangle[layer][i]
dp = triangle[-1]
for layer in xrange(len(triangle)-2,-1,-1):
for i in xrange(len(triangle[layer])):
dp[i] = min(dp[i],dp[i+1]) + triangle[layer][i]
return dp[0]

leetcode 542. 01 Matrix

发表于 2018-04-13 | 分类于 leetcode

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.
Example 1:

Input:

1
2
3
0 0 0
0 1 0
0 0 0

Output:

1
2
3
0 0 0
0 1 0
0 0 0

Example 2:

Input:

1
2
3
0 0 0
0 1 0
1 1 1

Output:

1
2
3
0 0 0
0 1 0
1 2 1

Note:

The number of elements of the given matrix will not exceed 10,000.
There are at least one 0 in the given matrix.
The cells are adjacent in only four directions: up, down, left and right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from collections import deque
def main(matrix):
m,n = len(matrix),len(matrix[0])
q = deque()
for i in range(m):
for j in range(n):
if matrix[i][j] != 0:
matrix[i][j] = 20000
else:
q.append((i,j))

while q:
x,y = q.popleft()
for r,c in ((x-1,y),(x,y-1),(x,y+1),(x+1,y)):
z = matrix[x][y] + 1
if 0<=r<m and 0<=c<n and matrix[r][c] > z:
matrix[r][c] = z
q.append((r,c))
return matrix

leetcode 200. Number of Islands(bfs)

发表于 2018-04-12 | 分类于 leetcode

Given a 2d grid map of '1's (land) and '0's(water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

1
2
3
4
11110
11010
11000
00000

Answer: 1

Example 2:

1
2
3
4
11000
11000
00100
00011

Answer: 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from collections import deque

def bfs(grid,x,y):
q = deque()
q.append((x,y))
grid[x][y] = '0'
while q:
x,y = q.popleft()
if x > 0 and grid[x-1][y] == '1':
q.append((x-1,y))
grid[x-1][y] = '0'
if x < len(grid) - 1 and grid[x+1][y] == '1':
q.append((x+1,y))
grid[x+1][y] = '0'
if y > 0 and grid[x][y-1] == '1':
q.append((x,y-1))
grid[x][y-1] = '0'
if y < len(grid[0]) - 1 and grid[x][y+1] == '1':
q.append((x,y+1))
grid[x][y+1] = '0'

def main(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
bfs(grid,i,j)
count += 1
return count

leetcode 279. Perfect Squares

发表于 2018-04-12 | 分类于 leetcode

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
if n < 2:return n
lst = []
for i in range(1,n):
if i*i <= n:
lst.append(i*i)
else:break
cnt = 0
nums = {n}
while nums:
temp =set()
cnt += 1
for x in nums:
for y in lst:
if x == y :
return cnt
elif x < y:break
temp.add(x-y)
nums = temp
return cnt

leetcode 473. Matchsticks to Square

发表于 2018-04-10 | 分类于 leetcode

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.

Example 1:


Input: [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.

Example 2:


Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.

Note:

  1. The length sum of the given matchsticks is in the range of 0 to 10^9.
  2. The length of the given matchstick array will not exceed 15.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class Solution(object):
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def dfs(nums,pos,target):
if pos == len(nums):
return True
for i in range(4):
if target[i] >= nums[pos]:
target[i] -= nums[pos]
if dfs(nums,pos+1,target):
return True
target[i] += nums[pos]
return False
if len(nums) < 4:
return False
numsum = sum(nums)
if numsum%4 != 0:return False
nums.sort(reverse=True)
target = [numsum/4]*4
return dfs(nums,0,target)

leetcode 472. Concatenated Words

发表于 2018-04-08 | 分类于 leetcode

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

Example:

1
2
3
4
5
Input:["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".

Note:

The number of elements of the given array will not exceed 10,000

The length sum of elements in the given array will not exceed 600,000.

All the input string will only include lower case letters.
The returned elements order does not matter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution(object):
def findAllConcatenatedWordsInADict(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
word_set = set(words)
result = []
def dfs(word,cur,cnt):
if cur == len(word):
if cnt > 1:
return True
else:
return False

for i in xrange(cur+1,len(word)+1):
if word[cur:i] in word_set and dfs(word,i,cnt+1):
return True
return False
for word in words:
if dfs(word,0,0):
result.append(word)
return result

leetcode 200. Number of Islands

发表于 2018-04-05 | 分类于 leetcode

Given a 2d grid map of '1's (land) and '0's(water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

1
2
3
4
11110
11010
11000
00000

Answer: 1

Example 2:

1
2
3
4
11000
11000
00100
00011

Answer: 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
self.dfs(grid,i,j)
count += 1
return count
def dfs(self,grid,i,j):
if i < 0 or j < 0 or i >=len(grid) or j >= len(grid[0]) or grid[i][j] != '1':
return
grid[i][j] = '0'
self.dfs(grid,i-1,j)
self.dfs(grid,i,j-1)
self.dfs(grid,i+1,j)
self.dfs(grid,i,j+1)

leetcode 394. Decode String

发表于 2018-04-04 | 分类于 leetcode

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

Examples:

1
2
3
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
stack = []
stack.append(["",1])
num = ''
for ch in s:
if ch.isdigit():
num += ch
elif ch == '[':
stack.append(["",int(num)])
num = ''
elif ch == ']':
s1,k1 = stack.pop()
stack[-1][0] += s1*k1
else:
stack[-1][0] += ch
return stack[0][0]

leetcode 116. Populating Next Right Pointers in Each Node

发表于 2018-04-03 | 分类于 leetcode

Given a binary tree

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,

Given the following perfect binary tree,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

After calling your function, the tree should look like:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \  / \
4->5->6->7 -> NULL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None

class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
if root is None:
return None
pre = root
while pre.left:
cur = pre
while cur:
cur.left.next = cur.right
if cur.next:
cur.right.next = cur.next.left
cur = cur.next
pre = pre.left
1…161718…20

zhuanli

194 日志
9 分类
41 标签
GitHub
© 2018 — 2019 zhuanli
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4