-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Programmers brute force, leetcode string,trim,arr
- Loading branch information
Showing
3 changed files
with
90 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,37 @@ | ||
def solution(grid): | ||
global visited, direction | ||
answer = [] | ||
direction = [[1, 0], [0, 1], [-1, 0], [0, -1]] | ||
visited = [[[False] * 4 for i in range(len(grid[0]))] for i in range(len(grid))] | ||
|
||
for i in range(len(grid)): | ||
for k in range(len(grid[0])): | ||
for direct in range(4): | ||
if visited[i][k][direct] == False: | ||
count = simulation(i, k, direct, grid) | ||
|
||
if count != 0: | ||
answer.append(count) | ||
answer.sort() | ||
return answer | ||
|
||
|
||
def simulation(i, k, direct, grid): | ||
global visited, direction | ||
visited[i][k][direct] = True | ||
row, col, next_direct = i, k, direct | ||
count = 0 | ||
while True: | ||
row = (len(grid) + (row + direction[next_direct][0])) % len(grid) | ||
col = (len(grid[0]) + (col + direction[next_direct][1])) % len(grid[0]) | ||
count += 1 | ||
if grid[row][col] == "L": | ||
next_direct = (4 + (next_direct + 1)) % 4 | ||
elif grid[row][col] == "R": | ||
next_direct = (4 + (next_direct - 1)) % 4 | ||
if visited[row][col][next_direct] == True: | ||
if (row, col, next_direct) == (i, k, direct): | ||
return count | ||
else: | ||
return 0 | ||
visited[row][col][next_direct] = True |
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,14 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLastWord = function (s) { | ||
// let string_arr = s.split(" "); | ||
// for(let i=string_arr.length-1; i >= 0; i--){ | ||
// if(string_arr[i].length > 0){ | ||
// return string_arr[i].length | ||
// } | ||
// } | ||
let string_arr = s.trim().split(" "); | ||
return string_arr[string_arr.length - 1].length; | ||
}; |
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,39 @@ | ||
/** | ||
* @param {string} s | ||
* @param {number} numRows | ||
* @return {string} | ||
*/ | ||
var convert = function (s, numRows) { | ||
if (numRows === 1) { | ||
return s; | ||
} else { | ||
let arr = new Array(numRows); | ||
for (let i = 0; i < numRows; i++) { | ||
arr[i] = new Array(); | ||
} | ||
let start = 0; | ||
let current_row = 0; | ||
let current_col = 0; | ||
let direction = 0; | ||
while (start < s.length) { | ||
if (current_row === numRows - 1) { | ||
direction = 1; | ||
} else if (current_row === 0) { | ||
direction = 0; | ||
} | ||
arr[current_row][current_col] = s[start]; | ||
if (direction === 0) { | ||
current_row++; | ||
} else { | ||
current_row--; | ||
current_col++; | ||
} | ||
start++; | ||
} | ||
let answer = ""; | ||
for (let i = 0; i < arr.length; i++) { | ||
answer += arr[i].join(""); | ||
} | ||
return answer; | ||
} | ||
}; |