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.
@LmaoSuVuong hnay khinh thớt thiếu nhi không thèm lên chơi, loại này 1 gạch là không đủ thím nhỉ @MasonMaoSuVuong
osCpCsi.png
 
Trúng đợt đề nhân đạo, có thể điểm danh trước khi ngủ :cautious:
C#:
public class Solution
{
    public int[] MissingRolls(int[] rolls, int mean, int n)
    {
        int m = rolls.Length;
        int total = (m + n) * mean;
        int remain = total;
        for (int i = 0; i < rolls.Length; i++)
        {
            remain -= rolls[i];
        }

        float average = (float)remain / n;
        if (average < 1f || 6f < average)
        {
            return [];
        }

        int[] result = new int[n];
        int baseValue = remain / n;   
        int extra = remain % n;       

        for (int i = 0; i < n; i++)
        {
            result[i] = baseValue + (i < extra ? 1 : 0);
        }

        return result;
    }
}
 
** má quên nhìn constaint đâm đầu vô DFS rồi BFS TLE MLE hết, xong quay sang math thì 1 for, chán đời T_T
 
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(-1, head)
        nums = set(nums)
        prev = dummy
        current = dummy.next
        while current != None:
            if current.val in nums:
                prev.next = current.next
                current = current.next
            else:
                current = current.next
                prev = prev.next

        return dummy.next
 
Bài nào cần nhân ma trận thì trư mới dùng Python @anoldvozer1710.v2
zFNuZTA.png

Java:
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet());
        ListNode dumpHead = new ListNode(0, head);
        ListNode pointer = head;
        ListNode prev = dumpHead;
        while (pointer != null) {
            if (set.contains(pointer.val)) {prev.next = pointer.next;} else prev = pointer;
            pointer = pointer.next;
        }
        return dumpHead.next;
    }
}
 
Sửa lần cuối:
Clist 1174, LC 1341, medium fake rồi :doubt:
JavaScript:
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
    const set = new Set(nums);
    while(head && set.has(head.val)) head = head.next;
    if (!head) return null;
    let cur = head;
    while (cur && cur.next) {
        if (set.has(cur.next.val)) cur.next = cur.next.next;
        else cur = cur.next;
    }
    return head;
};
 
Bài nào cần nhân ma trận thì trư mới dùng Python @anoldvozer1710.v2
zFNuZTA.png

Java:
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = new HashSet<>();

        for (int num: nums) {
            set.add(num);
        }

        ListNode dumpHead = new ListNode(0, head);
        ListNode pointer = head;
        ListNode prev = dumpHead;

        while (pointer != null) {
            if (set.contains(pointer.val)) {
                prev.next = pointer.next;
            } else {
                prev = pointer;
            }
            pointer = pointer.next;
        }

        return dumpHead.next;
    }
}
mấy bài mà có edge case int long gì đó thì chuyển sang mấy thằng python, js là xong, khỏi cần khẩm dô :doubt:
 
Java:
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = new HashSet();
        for(int num:nums){
            set.add(num);
        }
        ListNode dummyHead = new ListNode(0,head);
        ListNode preNode = dummyHead;
        ListNode curNode = head;
       
        preNode.next = curNode;
        while(curNode != null){
            if(set.contains(curNode.val)){
                ListNode delNode = curNode;
                curNode = curNode.next;
                delNode =null;
                preNode.next = curNode;
            }
            else{
                preNode = preNode.next;
                curNode =curNode.next;
            }
           
        }
        return dummyHead.next;
    }
}
cả tuần nay nháp chưa dc 1/3 mặt giấy nữa. leetcode mai cho 1 câu siu kay đỏ lè giùm
g9qSbBf.png
 
Sửa lần cuối:
Java:
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = new HashSet<>();
        for (int i : nums) {
            set.add(i);
        }

        while(head != null && set.contains(head.val)) {
            head = head.next;
        }

        ListNode last = head;
        ListNode cur = head.next;

        while(cur != null) {
            if (set.contains(cur.val)) {
                cur = cur.next;
            }
            else {
                last.next = cur;
                last = cur;
                cur = cur.next;
            }
        }

        if (last.next != null && set.contains(last.next.val)) {
            last.next = null;
        }

        return head;
    }
}
 
Swift:
class Solution {
    func modifiedList(_ nums: [Int], _ head: ListNode?) -> ListNode? {
        let nums = Set(nums)
        var result = ListNode(0, head)
        var root = result
        while let next = root.next {
            if nums.contains(next.val) {
                root.next = next.next
            } else {
                root = next
            }
        }
        return result.next
    }
}
 
C++:
class Solution {
public:
    ListNode* modifiedList(vector<int>& nums, ListNode* head) {
        int exist[100001] = {0};
        for(int i = 0;i<  nums.size();i++) {
            exist[nums[i]-1] = 1;
        }
        ListNode* prev = nullptr;
        ListNode* newHead = nullptr;
        while(head != nullptr){
            if(exist[head->val-1] != 1){
                if(prev != nullptr){
                    prev->next = head;
                }else{
                    newHead = head;
                }
                prev = head;
            }else{
                ListNode* tmp = head;
                head = head->next;
                delete tmp;
                if(prev != nullptr) prev->next = head;
                continue;
            }         
            head = head->next;
        }
        return newHead;
    }
};
 
Java:
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = new HashSet();
        for(int num:nums){
            set.add(num);
        }
        ListNode dummyHead = new ListNode(0,head);
        ListNode preNode = dummyHead;
        ListNode curNode = head;
      
        preNode.next = curNode;
        while(curNode != null){
            if(set.contains(curNode.val)){
                ListNode delNode = curNode;
                curNode = curNode.next;
                delNode =null;
                preNode.next = curNode;
            }
            else{
                preNode = preNode.next;
                curNode =curNode.next;
            }
          
        }
        return dummyHead.next;
    }
}
cả tuần nay nháp chưa dc 1/3 mặt giấy nữa. leetcode mai cho 1 câu siu kay đỏ lè giùm
g9qSbBf.png
Siu kay đỏ lè thì bác có nháp nữa đâu :angry:
 
Này mà medium là khinh thường thím @LmaoSuVuong
JavaScript:
var modifiedList = function(nums, head) {
    const set = new Set(nums);
    let dummy = new ListNode(0, head);
    let cur = dummy;
    while (cur.next) {
        if (set.has(cur.next.val)){
            cur.next = cur.next.next;
        } else {
            cur = cur.next;
        }
    }

    return dummy.next;
};
 
Python:
class Solution:
    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
        nums = set(nums)
        while head and head.val in nums:
            head = head.next
        if not head:
            return None
        temp = head
        while temp.next:
            if temp.next.val in nums:
                temp.next = temp.next.next
            else:
                temp = temp.next
        return head
 
PHP:
class Solution {

    /**
     * @param Integer[] $nums
     * @param ListNode $head
     * @return ListNode
     */
    function modifiedList($nums, $head) {
        $dict = [];
        foreach ($nums as $n) {
            $dict[$n] = 1;
        }

        // find real head
        while (isset($dict[$head->val])) {
            $head = $head->next;
        }

        // remove nodes
        $cur = $head;
        while ($cur->next) {
            $next = $cur->next;
            if (!isset($dict[$next->val])) {
                $cur = $next;
                continue;
            }
           
            $cur->next = $next->next;
        }

        return $head;
    }
}
 
Sửa lần cuố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 ListNode modifiedList(int[] nums, ListNode head) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        Queue<ListNode> queue = new ArrayDeque<>();
        queue.offer(head);
        ListNode root = new ListNode();
        ListNode temp = root;
        while (!queue.isEmpty()) {
            ListNode current = queue.poll();
            if (current.next != null) {
                queue.offer(current.next);
            }
            if (!set.contains(current.val)) {
                temp.next = new ListNode(current.val);
                temp = temp.next;
            }
        }
        return root.next;
    }
}
 
Mã:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
        arr = []
        n = set(nums)
        while head:
            if head.val not in n: arr.append(head.val)
            head = head.next

        dummy = ListNode()
        res = dummy
        for i in arr:
            node = ListNode(val=i)
            dummy.next = node
            dummy = dummy.next

        return res.next
1xEuo02.gif
hqua nghe vẻ rồng trong ng ngài @LmaoSuVuong dc tung hô quá , mong dc 1 lần ngài khai sáng
JEWoIdl.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.194
Quay lại
Lên đầu trang