导出工具ncexpdbstr
用法:1
./ncexpdbstr -u ncm -p ncm -d ncm -o ncmsql.str
导入工具ncupdatedbstr
用法:1
./ncupdatedbstr -u ncm -p ncm -d ncm -f ncmsql.str
用法:1
./ncexpdbstr -u ncm -p ncm -d ncm -o ncmsql.str
用法:1
./ncupdatedbstr -u ncm -p ncm -d ncm -f ncmsql.str
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
13class 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]
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
30 0 0
0 1 0
0 0 0
Output:1
2
30 0 0
0 1 0
0 0 0
Example 2:
Input:1
2
30 0 0
0 1 0
1 1 1
Output:1
2
30 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
19from 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
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 | 11110 |
Answer: 1
Example 2:
1 | 11000 |
Answer: 31
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
29from 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
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
25class 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
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.
Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
0
to 10^9.
15.
1 |
|
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
5Input:["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
23class 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
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 | 11110 |
Answer: 1
Example 2:
1 | 11000 |
Answer: 31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class 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)
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
3s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
1 | class Solution(object): |
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:
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 | # Definition for binary tree with next pointer. |