Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. การจัดเรียงอาร์เรย์แบบไม่สม่ำเสมอ (Uneven Sort) ใน JavaScript

    ปัญหา จัดเรียงอาร์เรย์ให้เป็น pattern: arr[0] arr[2] < arr[3] ... (wiggle sort) ตัวอย่าง input: [1, 5, 1, 1, 6, 4] output: [1, 6, 1, 5, 1, 4] (หนึ่งในคำตอบ) ตัวอย่างโค้ด a - b); let mid = Math.floor(arr.length / 2); if (arr.length % 2 === 1) { mid += 1; } let even = arr.slice(0, mid)

  2. การนับวิธีการรวมตัวเลขให้ได้ผลรวมเป้าหมาย (允许重复) ใน JavaScript

    ปัญหา กำหนดอาร์เรย์ unique integers และ target นับจำนวน combination (ลำดับสำคัญ, 允许重复) ที่รวมได้ target ตัวอย่าง 7 ตัวอย่างโค้ด { if (target === 0) { return 1; } if (typeof map[target] != undefined) { return map[target]; } let res = 0; for (let i = 0; i = nums[i]) { res

  3. การหาคู่ตัวเลขจาก 2 อาร์เรย์ที่มีผลรวมน้อยที่สุด k คู่ใน JavaScript

    ปัญหา กำหนด 2 sorted arrays arr1, arr2 หา k คู่ (arr1[i], arr2[j]) ที่มีผลรวมน้อยที่สุด ตัวอย่าง arr1 = [1,1,2], arr2 = [1,2,3], k = 2 output: [[1,1], [1,1]] ตัวอย่างโค้ด { let flag = Infinity; for (let i = 0; i < arr1.length; i++) { if (temp[i] = num) { return; } else {

  4. การหาสี่เหลี่ยมผืนผ้าที่มีผลรวมมากที่สุดแต่ไม่เกินค่าเป้าหมายใน JavaScript

    ปัญหา หา rectangle ใน 2D array ที่มีผลรวมมากที่สุดแต่ <= target number ตัวอย่าง arr = [[1,0,1],[0,-2,3]], target = 2 output: 2 (rectangle [[0,1],[-2,3]] sum = 2) ตัวอย่างโค้ด { const rows = arr.length; const cols = arr[0].length; let maxSum = -Infinity; for (let l = 0; l < rows; l++) {

  5. ปัญหา Killing Enemy (วางระเบิดฆ่าศัตรูสูงสุด) ใน JavaScript

    ปัญหา กำหนด 2D grid มี W (wall), E (enemy), 0 (empty) หาตำแหน่งวางระเบิดใน empty cell ที่ฆ่าศัตรูได้มากที่สุด (ระเบิดฆ่าทุก enemy ในแถวและคอลัมน์จนกว่าจะเจอ wall) ตัวอย่าง grid = [ [0,E,0,0], [E,0,W,E], [0,E,0,0] ] output: 3 (วางที่ [1,1] ฆ่าได้ 3 enemies) ตัวอย่างโค้ด 0 ? arr[0].length : 0

  6. การนำฟังก์ชัน f(x) = ax² + bx + c ไปประยุกต์กับอาร์เรย์ที่เรียงแล้วใน JavaScript

    ปัญหา กำหนด sorted array และฟังก์ชัน quadratic f(x) = ax² + bx + c ให้ apply f(x) กับทุก element แล้วคืน sorted array ของผลลัพธ์ ตัวอย่าง arr = [-8, -3, -1, 5, 7, 9], a=1, b=4, c=7 output: [4, 4, 39, 52, 84, 124] ตัวอย่างโค้ด a - b); return result; }; console.log(applyFunction(arr, a, b, c));

  7. การจัดวางตัวอักษรซ้ำให้ห่างกันอย่างน้อย n ตำแหน่งใน JavaScript

    ปัญหา จัดเรียงสตริงใหม่ ให้ตัวอักษรที่เหมือนกันห่างกันอย่างน้อย num ตำแหน่ง หากไม่ได้คืนสตริงว่าง ตัวอย่าง output: mlmklk ตัวอย่างโค้ด { const map = {}; for (let i = 0; i { if (map[a] max) index = 1; count--; } } return res.join(); }; console.log(placeApart(str)); ผลลัพ

  8. นับตัวเลข n หลักที่ทุกหลักไม่ซ้ำกัน (Count Unique Digit Numbers) ใน JavaScript

    ปัญหา รับ num นับจำนวนตัวเลขที่มี num หลัก และทุกหลักไม่ซ้ำกัน ตัวอย่าง const num = 1; 0-9 ทั้งหมด 10 ตัว → 10 โค้ดตัวอย่าง (Dynamic Programming) const num = 1; const uniqueDigits = (num = 1) => { const dp = [1, 10]; const sum = [1, 11]; for (let i = 2; i <= num; i++) { dp[i]

  9. หา Intersection ของอาร์เรย์สองอาร์เรย์รวมค่าซ้ำ (Intersection with Duplicates) ใน JavaScript

    ปัญหา รับอาร์เรย์ arr1, arr2 หา intersection (ค่าที่ซ้ำ) หากค่าใดปรากฏ 2 ครั้งในทั้งสองอาร์เรย์ ให้รวม 2 ครั้งในผลลัพธ์ ตัวอย่าง const arr1 = [2, 7, 4, 6, 7, 4]; const arr2 = [7, 1, 9, 7, 4, 5]; 7 ปรากฏ 2 ครั้งใน arr1 และ 2 ครั้งใน arr2 → ใส่ 2 ครั้ง, 4 ปรากฏ 2 ครั้งใน arr1 และ 1 ครั้งใน arr2 → ใส่

  10. กลับลำดับพยัญชนะในสตริง (Reverse Consonants Only) ใน JavaScript

    ปัญหา รับสตริงพิมพ์เล็ก กลับลำดับพยัญชนะ (consonants) ส่วนสระ (vowels) คงตำแหน่งเดิม ตัวอย่าง const str = somestring; พยัญชนะ: s,m,s,t,r,n,g → กลับ: g,n,r,t,s,m,s → วางคืนตำแหน่งเดิม → gomenrtiss โค้ดตัวอย่าง (Two Pointers) const str = somestring; const reverseConsonants = (str = ) => { con

  11. สร้าง Lightbox Animation ด้วย CSS และ JavaScript

    ตัวอย่าง <!DOCTYPE html> <html> <style> #parent { margin: 2%; padding: 0; box-sizing: border-box; background: cornflowerblue; text-align: center; } html,body { height:100%; max-height:100%; min-height:100%; } .smart { display: block; margin: 0 auto; width: 150px; height :150px; } .

  12. ตรวจสอบว่าสตริงประกอบด้วย Substring ซ้ำกันหรือไม่ (Repeated Substring Pattern) ใน JavaScript

    ปัญหา รับสตริง str ตรวจสอบว่าเป็นการซ้ำของ substring ย่อยๆ หรือไม่ ตัวอย่าง const str = thisthisthisthis; ซ้ำของ this → true โค้ดตัวอย่าง const str = thisthisthisthis; const repeatedSubstring = (str = ) => { const {length} = str; const checkSubString = ss => { const m = ss.length

  13. เรียงตัวอักษรในสตริงตามความถี่ (Frequency Sort) ใน JavaScript

    ปัญหา รับสตริง เรียงตัวอักษรตามความถี่มากไปน้อย ตัวอย่าง const str = free; e 2 ครั้ง, f 1, r 1 → eefr โค้ดตัวอย่าง const str = free; const frequencySort = (str = ) => { let map = {}; for (const letter of str) { map[letter] = (map[letter] || 0) + 1; } let res = ; let sorted = Object

  14. ลบโหนดที่ต้องการจาก Binary Search Tree (Delete Node in BST) ใน JavaScript

    ปัญหา มีโค้ด BST พร้อม Insert ต้องเพิ่มฟังก์ชัน deleteNode(root, key) ลบโหนดที่มีค่า key คืน root ใหม่ โค้ดตัวอย่าง class Node{ constructor(data) { this.data = data; this.left = null; this.right = null; } }; class BinarySearchTree{ constructor(){ this.root = null; } insert(data){ ... } insertNode(

  15. แบ่งอาร์เรย์เป็น k Subarray ให้ผลรวมมากที่สุดน้อยที่สุด (Split Array Largest Sum) ใน JavaScript

    ปัญหา รับอาร์เรย์ arr (non-negative) และ num แบ่งเป็น num subarray ต่อเนื่อง ให้ค่าสูงสุดของผลรวมแต่ละ subarray น้อยที่สุด ตัวอย่าง const arr = [5, 1, 4, 8, 7]; const num = 2; แบ่ง [5,1,4] (sum=10) และ [8,7] (sum=15) → ค่าสูงสุด = 15 โค้ดตัวอย่าง (Binary Search on Answer) const arr = [5, 1, 4, 8,

  16. หาผลรวมของ Left Leaves ใน Binary Search Tree (JavaScript)

    ปัญหา รับราก BST คำนวณผลรวมข้อมูลใน Left Leaves (โหนดใบที่เป็นลูกซ้าย) ตัวอย่าง BST: 8 / \ 1 10 / \ 5 17 Left leaves: 1 และ 5 → ผลรวม 6 โค้ดตัวอย่าง class Node{ constructor(data) { this.data = data; this.left = null; this.right = null; } }; class BinarySearchTree{ constructor(){ this.root = null;

  17. หาตัวเลขน้อยที่สุดหลังลบ n หลัก (Remove K Digits) ใน JavaScript

    ปัญหา รับตัวเลข m (สตริง) และ n ลบ n หลัก เพื่อให้ตัวเลขที่เหลือน้อยที่สุด ตัวอย่าง const m = 45456757; const n = 3; ลบ 5,6,7 → 44557 โค้ดตัวอย่าง (Greedy + Stack) const m = 45456757; const n = 3; const removeDigits = (m, n, stack = []) => { let arr = m.split().map(Number); for(let el of

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:27/298  20-คอมพิวเตอร์/Page Goto:1 21 22 23 24 25 26 27 28 29 30 31 32 33