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.
rating 1k6 vozlit mà củng ko tha
hO88fV9.png
 
JS , pass test mà k qua do time out :too_sad: trình gà mấy thím chỉ giúp em
Mã:
function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
    let dummy = new ListNode(0);
    let current = dummy;
    // Duyệt qua từng node trong danh sách liên kết
    while (head !== null) {
        if (!nums.includes(head.val)) {
            current.next = head;
            current = current.next;
        }
        // Chuyển sang node tiếp theo
        head = head.next;
    }
    current.next = null;
    return dummy.next;
}
 
Sửa lần cuối:
JS , pass test mà k qua do time out :too_sad: trình gà mấy thím chỉ giúp em
function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
let dummy = new ListNode(0);
let current = dummy;
// Duyệt qua từng node trong danh sách liên kết
while (head !== null) {
if (!nums.includes(head.val)) {
current.next = head;
current = current.next;
}
// Chuyển sang node tiếp theo
head = head.next;
}
current.next = null;
return dummy.next;
}
sau khi tối ưu lại tí

Mã:
function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
    // Chuyển nums thành Set để có độ phức tạp tìm kiếm O(1)
    const numSet = new Set(nums);
   
    let dummy = new ListNode(0);  // Node giả để dễ thao tác
    let current = dummy;
    while (head !== null) {
        // Nếu giá trị của node không có trong numSet, ta thêm node vào danh sách mới
        if (!numSet.has(head.val)) {
            current.next = head;
            current = current.next;
        }
        // Chuyển sang node tiếp theo
        head = head.next;
    }
    current.next = null;  // Đảm bảo danh sách mới không còn liên kết tới danh sách cũ
    return dummy.next;  // Trả về danh sách mới
}
 
sau khi tối ưu lại tí

Mã:
function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
    // Chuyển nums thành Set để có độ phức tạp tìm kiếm O(1)
    const numSet = new Set(nums);
   
    let dummy = new ListNode(0);  // Node giả để dễ thao tác
    let current = dummy;
    while (head !== null) {
        // Nếu giá trị của node không có trong numSet, ta thêm node vào danh sách mới
        if (!numSet.has(head.val)) {
            current.next = head;
            current = current.next;
        }
        // Chuyển sang node tiếp theo
        head = head.next;
    }
    current.next = null;  // Đảm bảo danh sách mới không còn liên kết tới danh sách cũ
    return dummy.next;  // Trả về danh sách mới
}
Chuyển qua set là ăn thôi, về học lại time complexity chứ ko các rồng trong này nó nói đểu đá khoáy đó
4gmOAMB.gif


via theNEXTvoz for iPhone
 
Python:
class Solution:
    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
        numset = set(nums)
        temp = ListNode(0,head)
        p = temp
        while(temp.next):
            if(temp.next.val in numset):
                temp.next = temp.next.next
            else:
                temp = temp.next
        return p.next
 
sau khi tối ưu lại tí

Mã:
function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
    // Chuyển nums thành Set để có độ phức tạp tìm kiếm O(1)
    const numSet = new Set(nums);
  
    let dummy = new ListNode(0);  // Node giả để dễ thao tác
    let current = dummy;
    while (head !== null) {
        // Nếu giá trị của node không có trong numSet, ta thêm node vào danh sách mới
        if (!numSet.has(head.val)) {
            current.next = head;
            current = current.next;
        }
        // Chuyển sang node tiếp theo
        head = head.next;
    }
    current.next = null;  // Đảm bảo danh sách mới không còn liên kết tới danh sách cũ
    return dummy.next;  // Trả về danh sách mới
}
phá list cũ thế này tí nữa lại ăn gạch của @Cố Trường Ca cho xem
3FUKQb8.png
 
em code bẩn :cry::beat_brick:
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 ListNode modifiedList(int[] nums, ListNode root) {
        Map<Integer, Integer> map = new HashMap<>();
        int n = nums.length;
        for (int i : nums)
            map.put(i, 1);

        ListNode node = new ListNode();
        ListNode dum = node;
        while (root != null) {
            if (map.get(root.val) == null) {
                if (dum == null) {
                    dum = new ListNode(root.val);
                } else {
                    ListNode curr = new ListNode(root.val);
                    dum.next = curr;
                    dum = dum.next;
                }
            }
            root = root.next;
        }
        dum.next = null;
        return node.next;
    }
}
Java:
class Solution {
    public int[] missingRolls(int[] a, int mean, int n) {
        int[] res = new int[n];

        int numberOfRoll = a.length;

        int originSum = 0;
        for (int i : a) originSum += i;

        int rem = (numberOfRoll + n) * mean - originSum;
       
        if (rem > 6 * n || rem < n)
            return new int[]{};

        int down = n;
        int setSum = 0;
        for (int i = 0; i < n; i++) {
            res[i] = rem / down--;
            rem -= res[i];
        }
        return res;
    }
}
 
C#:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode ModifiedList(int[] nums, ListNode head) {
        HashSet<int> set = new(nums);
        List<ListNode> helper = new List<ListNode>();

        while(head != null){
            if(!set.Contains(head.val))
                helper.Add(head);
            head = head.next;
        }

        helper.Reverse();
        ListNode prev = null;
        
        for(int i = 0; i < helper.Count(); ++i){
            head = new ListNode(helper[i].val, prev);
            prev = head;
        }
        return head;
    }
}

Giờ mới hiểu cảm giác của thor khi không có búa trong tay
MjfezZB.png
ai lấy mất búa của anh à?
 
trình em cùi quá nên đang học lại DS từ đầu ròi mới dám qua làm leetcode, lúc trước cứ cắm đầu làm mà chẳng đọng lại bao nhiu :pudency: , em cập nhật ở đây với các thím cho có động lực

06.09.2024: Array - Dynamic array, Left Rotation
 
trình em cùi quá nên đang học lại DS từ đầu ròi mới dám qua làm leetcode, lúc trước cứ cắm đầu làm mà chẳng đọng lại bao nhiu :pudency: , em cập nhật ở đây với các thím cho có động lực

06.09.2024: Array - Dynamic array, Left Rotation
sáng làm daily rồi paste vào trong đây để ae chửi cho sáng mắt là trình độ gia tăng nhé fency. :canny:

via theNEXTvoz for iPhone
 
Xử lý LinkedList của Rust phức tạp quá nên đọc lời giải.:rolleyes:
C-like:
impl Solution {
    pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
        let nums: &mut std::collections::HashSet<_> = &mut nums.into_iter().collect();
        let mut head = Box::new(ListNode { val: 0, next: head });

        let mut current = &mut head;
        while let Some(next) = current.next.take() {
            if nums.contains(&next.val) {
                current.next = next.next;
            } else {
                current.next = Some(next);
                current = current.next.as_mut().unwrap();
            }
        }

        head. Next
    }
}
 
trình em cùi quá nên đang học lại DS từ đầu ròi mới dám qua làm leetcode, lúc trước cứ cắm đầu làm mà chẳng đọng lại bao nhiu :pudency: , em cập nhật ở đây với các thím cho có động lực

06.09.2024: Array - Dynamic array, Left Rotation
1 ngày học có 1 bài thế lày thì bao h mới xong DSA
g3wDD5m.png
học nguyên 1 chương đi
LAqd64z.png
 
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.155
Quay lại
Lên đầu trang