Skip to content

Commit

Permalink
Programmers brute force, leetcode string,trim,arr
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrevile committed Feb 3, 2022
1 parent 02f6531 commit 08dfc2f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Programmers/빛의 경로 사이클.py
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
14 changes: 14 additions & 0 deletions leetcode JS/No.58. Length of Last Word.js
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;
};
39 changes: 39 additions & 0 deletions leetcode JS/No.6. Zigzag Conversion.js
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;
}
};

0 comments on commit 08dfc2f

Please sign in to comment.