Skip to content

Commit 48d69a8

Browse files
committed
12.7
1 parent e5b6ef2 commit 48d69a8

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

123.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Solution:
2+
"""
3+
@param board: A list of lists of character
4+
@param word: A string
5+
@return: A boolean
6+
"""
7+
def exist(self, board, word):
8+
# write your code here
9+
if len(board) == 0 or len(board[0]) == 0:
10+
return False
11+
if len(word) == 0:
12+
return True
13+
14+
cols, rows = len(board), len(board[0])
15+
used = [[0]*rows for i in range(cols)]
16+
# print(used)
17+
for i in range(cols):
18+
for j in range(rows):
19+
if Solution.findWord(1, board, word, cols, rows, i, j, used):
20+
return True
21+
22+
return False
23+
24+
def findWord(self, board, word, cols, rows, indexi, indexj, used):
25+
if len(word) == 0:
26+
return True
27+
if indexi < 0 or indexi >= cols:
28+
return False
29+
if indexj < 0 or indexj >= rows:
30+
return False
31+
# print(board[indexi][indexj])
32+
# print("word:", word, word[1:])
33+
# print(used[indexi][indexj] == 0, indexi, indexj)
34+
# print(board[indexi][indexj] == word[0])
35+
# print(used)
36+
if used[indexi][indexj] == 1 or board[indexi][indexj] != word[0]:
37+
return False
38+
39+
used[indexi][indexj] = 1
40+
41+
res = Solution.findWord(1, board, word[1:], cols, rows, indexi-1, indexj, used) or Solution.findWord(1, board, word[1:], cols, rows, indexi+1, indexj, used) or Solution.findWord(1, board, word[1:], cols, rows, indexi, indexj+1, used) or Solution.findWord(1, board, word[1:], cols, rows, indexi, indexj-1, used)
42+
43+
if res == False:
44+
used[indexi][indexj] = 0
45+
return res
46+
47+
arr = [
48+
49+
"ABCE",
50+
51+
"SFCS",
52+
53+
"ADEE"
54+
55+
]
56+
word = "ABCCE"
57+
print(Solution.exist(1, arr, word))

165.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
Definition of singly-linked-list:
3+
class ListNode {
4+
public:
5+
int val;
6+
ListNode *next;
7+
ListNode(int val) {
8+
this->val = val;
9+
this->next = NULL;
10+
}
11+
}
12+
13+
14+
class Solution {
15+
public:
16+
/**
17+
* @param l1: ListNode l1 is the head of the linked list
18+
* @param l2: ListNode l2 is the head of the linked list
19+
* @return: ListNode head of linked list
20+
*/
21+
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
22+
// write your code here
23+
if(l1 == NULL){
24+
return l2;
25+
}
26+
if(l2 == NULL){
27+
return l1;
28+
}
29+
30+
ListNode *tmp = NULL;
31+
ListNode *head = NULL;
32+
if(l1->val < l2->val){
33+
head = new ListNode(l1->val);
34+
l1 = l1->next;
35+
}
36+
else{
37+
head = new ListNode(l2->val);
38+
l2 = l2->next;
39+
}
40+
ListNode *front = head;
41+
while(l1 != NULL && l2 != NULL){
42+
if(l1->val < l2->val){
43+
tmp = new ListNode(l1->val);
44+
l1 = l1->next;
45+
}
46+
else{
47+
tmp = new ListNode(l2->val);
48+
l2 = l2->next;
49+
}
50+
front->next = tmp;
51+
front = tmp;
52+
}
53+
while(l1 != NULL){
54+
tmp = new ListNode(l1->val);
55+
front->next = tmp;
56+
front = tmp;
57+
l1 = l1->next;
58+
}
59+
while(l2 != NULL){
60+
tmp = new ListNode(l2->val);
61+
front->next = tmp;
62+
front = tmp;
63+
l2 = l2->next;
64+
}
65+
66+
return head;
67+
}
68+
};

0 commit comments

Comments
 (0)