MasonMaoSuVuong
Senior Member
rating 1k6 vozlit mà củng ko tha
trình gà mấy thím chỉ giúp emfunction 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íJS , pass test mà k qua do time outtrì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;
}
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 đó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 }
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
phá list cũ thế này tí nữa lại ăn gạch của @Cố Trường Ca cho xemsau 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 }
máy móc r. bài này mục tiêu là xóa thì cứ mạnh tay xóa thôiphá list cũ thế này tí nữa lại ăn gạch của @Cố Trường Ca cho xem![]()
xin làm j hả fen, nộp code daily lên đây điểm danh hằng ngày biết chửacho em tham gia với ạ, em đang học lại DS


/**
* 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;
}
}
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;
}
}
ở thớt này có mấy vị cộm cán như việt kiều @freedom.9, vua code @MasonMaoSuVuong với @Cố Trường Ca , với mấy anh dev lương 5k nữa, anh vào đây chơi nhớ nộp daily xong tag mấy anh kia xin cơm thêm là ai cũng quýcho em tham gia với ạ, em đang học lại DS
mình thu hội phí 50k nhé fencho em tham gia với ạ, em đang học lại DS
/**
* 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
Giờ mới hiểu cảm giác của thor khi không có búa trong tay
f33 vùng đất ko lành, quay đầu là bờ my fencyGiờ mới hiểu cảm giác của thor khi không có búa trong tay

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.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, 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


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
}
}
1 ngày học có 1 bài thế lày thì bao h mới xong DSAtrì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, 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