bkhoang
Senior Member
@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
Ai có đề xuất hoặc cảm thấy ít thì cứ nêu ý kiến nhéMình làm low layer nên 2 câu đều là bit mal fen à, món này mình cũng có biết nhưng mà để nói luyện thuật toán thì mình chưa luyện.
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;
}
}
# 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
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;
}
}
/**
* 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;
};
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ôBài nào cần nhân ma trận thì trư mới dùng Python @anoldvozer1710.v2![]()
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; } }

chuẩn cmnlmấ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ô![]()
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;
}
}
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;
}
}
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
}
}
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;
}
};
Siu kay đỏ lè thì bác có nháp nữa đâucả 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ùmJava: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; } }![]()

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;
};
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
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;
}
}
/**
* 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;
}
}
# 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