-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution(object): | ||
def breakPalindrome(self, palindrome): | ||
""" | ||
:type palindrome: str | ||
:rtype: str | ||
""" | ||
if len(palindrome) == 1: | ||
return "" | ||
if len(set(palindrome)) == 1: | ||
if palindrome[0] == "a": | ||
return palindrome[:-1] + "b" | ||
for ch in palindrome: | ||
if ord(ch) > ord("a"): | ||
res = palindrome.replace(ch, "a", 1) | ||
return res if res != res[::-1] else palindrome[:-1] + "b" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution(object): | ||
def diagonalSort(self, mat): | ||
""" | ||
:type mat: List[List[int]] | ||
:rtype: List[List[int]] | ||
""" | ||
if not mat or not mat[0]: | ||
return None | ||
m, n = len(mat), len(mat[0]) | ||
for j in range(n): | ||
i, l, pos = 0, [], [] | ||
while i < m and j < n: | ||
l.append(mat[i][j]) | ||
pos.append([i, j]) | ||
i += 1 | ||
j += 1 | ||
l.sort() | ||
for i, p in enumerate(pos): | ||
x, y = p | ||
mat[x][y] = l[i] | ||
|
||
for i in range(1, m): | ||
j, l, pos = 0, [], [] | ||
while i < m and j < n: | ||
l.append(mat[i][j]) | ||
pos.append([i, j]) | ||
i += 1 | ||
j += 1 | ||
l.sort() | ||
for i, p in enumerate(pos): | ||
x, y = p | ||
mat[x][y] = l[i] | ||
return mat |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution(object): | ||
def arrayRankTransform(self, arr): | ||
""" | ||
:type arr: List[int] | ||
:rtype: List[int] | ||
""" | ||
l = sorted(arr) | ||
dic, rank = {}, 0 | ||
pre_num = None | ||
for i, num in enumerate(l): | ||
if pre_num != num: | ||
rank += 1 | ||
dic[num] = rank | ||
pre_num = num | ||
|
||
res = [] | ||
for num in arr: | ||
res.append(dic[num]) | ||
return res |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution(object): | ||
def removePalindromeSub(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
if not s: | ||
return 0 | ||
return 1 if s == s[::-1] else 2 |