thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
Có cái pattern cho BS ấy, lội ngược lại mà kiếm. Chuyên trị các loại BS. T thì k dùng cái đó đó trước khi đọc đc cái đó thì t đã tự build đc pattern cho riêng mình rồi.
BS mà làm từ đầu k follow theo pattern nào là hay sai linh tinh, ăn nhiều bọ lắm
xài pattern r đó bác. mà nghĩ cái hàm condition hơi lâu thôi. nháp mãi mới thấy
iIbBe8q.png
 
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        trav = head
        num_node = 0

        while trav != None:
            num_node += 1
            trav = trav.next

        num_part = num_node // k
        mod = num_node % k

        arr = []

        for i in range(k):
            part_size =  num_part + 1 if mod > 0 else num_part

            if part_size == 0:
                arr.append(None)
                continue

            arr.append(head)
            part_size -= 1

            while part_size > 0:
                head = head.next
                part_size -= 1
           
            if head == None:
                arr.append(None)
            else:  
                temp = head.next
                head.next = None
                head = temp

            mod -= 1

        return arr

tham gia ạ, mới chôm được cái divmod từ bác @freedom.9 , cho em hỏi xíu đoạn cuối sao bác không cần check None này nhỉ

Python:
if head == None:
     arr.append(None)
 
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        trav = head
        num_node = 0

        while trav != None:
            num_node += 1
            trav = trav.next

        num_part = num_node // k
        mod = num_node % k

        arr = []

        for i in range(k):
            part_size =  num_part + 1 if mod > 0 else num_part

            if part_size == 0:
                arr.append(None)
                continue

            arr.append(head)
            part_size -= 1

            while part_size > 0:
                head = head.next
                part_size -= 1
           
            if head == None:
                arr.append(None)
            else:  
                temp = head.next
                head.next = None
                head = temp

            mod -= 1

        return arr

tham gia ạ, mới chôm được cái divmod từ bác @freedom.9 , cho em hỏi xíu đoạn cuối sao bác không cần check None này nhỉ

Python:
if head == None:
     arr.append(None)
À mình precalculate sizes rồi nên lúc partsize bằng 0 thì mình biết nó là None nên add None luôn, do lúc đó head None rồi đó bác.

via theNEXTvoz for iPhone
 
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
        dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        indexD = 0
        i, j = 0, 0
        result = [[-1 for _ in range(n)] for _ in range(m)]

        while head:
            result[i][j] = head.val
            nextI, nextJ = i + dirs[indexD][0], j + dirs[indexD][1]

            if nextI < 0 or nextI >= m or nextJ < 0 or nextJ >= n or result[nextI][nextJ] != -1:
                indexD = (indexD + 1) % 4

            i += dirs[indexD][0]
            j += dirs[indexD][1]

            head = head.next
        return result
 
JavaScript:
var spiralMatrix = function(m, n, head) {
    const matrix = Array.from({ length: m }, () => Array(n).fill(-1));
    let [left, right, top, bottom] = [0, n - 1, 0, m - 1];
    let cur = head;
    while (cur) {
        // Go right
        for (let c = left; c <= right; c++) {
            matrix[top][c] = cur.val;
            if (!cur.next) return matrix;
            cur = cur.next;
        }
        top++;

        // Go Down
        for (let r = top; r <= bottom; r++) {
            matrix[r][right] = cur.val;
            if (!cur.next) return matrix;
            cur = cur.next;
        }
        right--;

        // Go Left
        for (let c = right; c >= left; c--) {
            matrix[bottom][c] = cur.val;
            if (!cur.next) return matrix;
            cur = cur.next;
        }
        bottom--;

        // Go Up
        for (let r = bottom; r >= top; r--) {
            matrix[r][left] = cur.val;
            if (!cur.next) return matrix;
            cur = cur.next;
        }
        left++;
    }
    return matrix;
};
 
C++:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        vector<vector<int>> res(m, vector<int>(n, -1));
        int count = 0, left = 0, right = n - 1, top = 0, down = m - 1;
        ListNode* current = head;
        while (count < m*n){
            for (int i = left; i <= right; i++)
            {
                res[top][i] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            for (int i = top + 1; i <= down; i ++){
                res[i][right] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            if(top != down)
            {
                for (int i = right - 1; i >= left; i--)
                {
                    res[down][i] = current->val;
                    current = current->next;
                    count += 1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            if (left != right)
            {
                for (int i = down - 1; i > top; i--){
                    res[i][left] = current -> val;
                    current = current->next;
                    count +=1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            left +=1;
            right -=1;
            top += 1;
            down -=1;
        }

        return res;
    }
};
 
C++:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        vector<vector<int>> res(m, vector<int>(n, -1));
        int count = 0, left = 0, right = n - 1, top = 0, down = m - 1;
        ListNode* current = head;
        while (count < m*n){
            for (int i = left; i <= right; i++)
            {
                res[top][i] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            for (int i = top + 1; i <= down; i ++){
                res[i][right] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            if(top != down)
            {
                for (int i = right - 1; i >= left; i--)
                {
                    res[down][i] = current->val;
                    current = current->next;
                    count += 1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            if (left != right)
            {
                for (int i = down - 1; i > top; i--){
                    res[i][left] = current -> val;
                    current = current->next;
                    count +=1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            left +=1;
            right -=1;
            top += 1;
            down -=1;
        }

        return res;
    }
};
Học thêm tà môn gì nữa đây
KE5ti7l.png
 
Java:
class Solution {
    ListNode head;
    int[][] map;

    int count;
    int i = 0;
    int j = 0;
    int level = 0;

    public int[][] spiralMatrix(int m, int n, ListNode head) {
        this.head = head;
        this.map = new int[m][n];
        this.count = m * n;

        while(count > 0) {
            while(j < n - level) {
                add(i, j++);
            }

            if (count <= 0) return map;

            j--;
            i++;

            while(i < m - level) {
                add(i++, j);
            }
            if (count <= 0)
                return map;

            i--;
            j--;

            while(j >= 0 + level) {
                add(i, j--);
            }

            if (count <= 0) return map;

            i--;
            j++;
           
            while(i > 0 + level) {
                add(i--, j);
            }

            if (count <= 0) return map;

            i++;
            j++;
           
            //printMap(map);
            level++;
        }

        return map;
    }

    void add(int i, int j) {
        if (head == null) {
            map[i][j] = -1;
        }
        else {
            map[i][j] = head.val;
            head = head.next;
        }
        count--;
    }

    public static void printMap(int[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
Java:
class Solution {
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int i =0 ; int j =-1 ;
        int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};
        int cur=0;
        int[][] matrix = new int[m][n];
        for (int[] row: matrix){
            Arrays.fill(row, -1);
        }
        ListNode curNode = head;
        for(int k=0;k<m*n;k++){
            if(curNode==null){
                break;
            }
            while(i+directions[cur][0]>=m
            || j + directions[cur][1]>=n
            || i+ directions[cur][0]<0
            || j + directions[cur][1]<0
            || matrix[i+ directions[cur][0]][j + directions[cur][1]]!=-1){
                cur=(cur+1)%4;
            }
            i+= directions[cur][0];
            j+= directions[cur][1];

            matrix[i][j] = curNode.val;
            curNode = curNode.next;
           
        }
        return matrix;
    }
}
 
Java:
class Solution {
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int i =0 ; int j =-1 ;
        int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};
        int cur=0;
        int[][] matrix = new int[m][n];
        for (int[] row: matrix){
            Arrays.fill(row, -1);
        }
        ListNode curNode = head;
        for(int k=0;k<m*n;k++){
            if(curNode==null){
                break;
            }
            while(i+directions[cur][0]>=m
            || j + directions[cur][1]>=n
            || i+ directions[cur][0]<0
            || j + directions[cur][1]<0
            || matrix[i+ directions[cur][0]][j + directions[cur][1]]!=-1){
                cur=(cur+1)%4;
            }
            i+= directions[cur][0];
            j+= directions[cur][1];

            matrix[i][j] = curNode.val;
            curNode = curNode.next;
          
        }
        return matrix;
    }
}
1BW9Wj4.png
Sao không đặt biến cho nó gọn mà cộng directions tràn lan đại hải vậy
 
C++:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        vector<vector<int>> res(m, vector<int>(n, -1));
        int count = 0, left = 0, right = n - 1, top = 0, down = m - 1;
        ListNode* current = head;
        while (count < m*n){
            for (int i = left; i <= right; i++)
            {
                res[top][i] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            for (int i = top + 1; i <= down; i ++){
                res[i][right] = current->val;
                current = current->next;
                count +=1;
                if (current == nullptr){
                    return res;
                }
            }

            if(top != down)
            {
                for (int i = right - 1; i >= left; i--)
                {
                    res[down][i] = current->val;
                    current = current->next;
                    count += 1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            if (left != right)
            {
                for (int i = down - 1; i > top; i--){
                    res[i][left] = current -> val;
                    current = current->next;
                    count +=1;
                    if (current == nullptr){
                        return res;
                    }
                }
            }

            left +=1;
            right -=1;
            top += 1;
            down -=1;
        }

        return res;
    }
};
Cơ bắp quá
u40wsAh.png

Java:
class Solution {
    int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int[][] ans = new int[m][n];
        int d = 0, row = 0, col = 0;
        ListNode pointer = head;

        for (int[] r: ans) {
            Arrays.fill(r, -1);
        }

        while (pointer != null) {
            ans[row][col] = pointer.val;
            int nextRow = row + dirs[d][0], nextCol = col + dirs[d][1];

            if (nextRow < 0 || nextRow >= m || nextCol < 0 || nextCol >= n || ans[nextRow][nextCol] != -1) {
                d = (d + 1) % 4;
                nextRow = row + dirs[d][0];
                nextCol = col + dirs[d][1];
            }

            pointer = pointer.next;
            row = nextRow;
            col = nextCol;
        }

        return ans;
    }
}
 
Cơ bắp quá
u40wsAh.png

Java:
class Solution {
    int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int[][] ans = new int[m][n];
        int d = 0, row = 0, col = 0;
        ListNode pointer = head;

        for (int[] r: ans) {
            Arrays.fill(r, -1);
        }

        while (pointer != null) {
            ans[row][col] = pointer.val;
            int nextRow = row + dirs[d][0], nextCol = col + dirs[d][1];

            if (nextRow < 0 || nextRow >= m || nextCol < 0 || nextCol >= n || ans[nextRow][nextCol] != -1) {
                d = (d + 1) % 4;
                nextRow = row + dirs[d][0];
                nextCol = col + dirs[d][1];
            }

            pointer = pointer.next;
            row = nextRow;
            col = nextCol;
        }

        return ans;
    }
}
Ờ nhỉ thế ko nghĩ ra, đm đúng là chơi với 1k3 hoài hư não quá rồi =((
 
Java:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int[][] matrix = new int[m][n];
        for (int i = 0; i < m; i++) {
            Arrays.fill(matrix[i], -1);
        }
        int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int i = 0, j = 0, k = 0;
        while (head != null) {
            matrix[i][j] = head.val;
            int x = i + move[k][0], y = j + move[k][1];
            if (x >= m || y >= n || x < 0 || y < 0 || matrix[x][y] != -1) k = (k + 1) % 4;
            i += move[k][0];
            j += move[k][1];
            head = head.next;
        }
        return matrix;
    }
}
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.213.732
Quay lại
Lên đầu trang