算法是一系列明确定义的步骤,用于定义问题的抽象解决方案。当您的问题与算法设计相关时,请使用此标记。
什么是将分类列表与丢失键的随身携带机制合并的最有效算法? 我与多个有序列表一起工作,每个列表包含表单的元素(键,值),其中每个列表按钥匙按升序排序。我想将这些列表合并到一个单一的列表中...
的元素,其中每个列表按升序按顺序排序。我想将这些列表合并到一个单个有序列表中,该列表包含来自输入列表的每个不同键。合并的输出应为形式的元组列表:
将HTML字符串阅读到树结构创建具有定义边界的树的平坦列表。例如 <table> <tr> <td> 1 </td> </tr> <tr> <td> 2 </td> </tr> </table> tents转换为[table, [tr, [td, [1]]], [tr, [td, [2]]]] 将此列表连接到Myers差异算法以找到差异。 当我获得差异时,就HTML而言,我没有得到最佳的差异(编辑数量最少)。 编码读取HTML并创建列表。 import htmlparser from 'htmlparser2'; import {diffHierarchical} from './beta-myers-diff.js';; /** * Converts HTML string to a hierarchical array structure * @param {string} html - The HTML string to parse * @returns {Array} - A hierarchical array representation of the HTML */ export function htmlToHierarchicalArray(html) { // Parse HTML to DOM using htmlparser2 let dom = null; const handler = new htmlparser.DomHandler((error, nodes) => { if(error) { console.error('Error parsing HTML:', error); return; } // Find root element(s) const tags = nodes.filter(node => node.type === 'tag'); if(tags.length > 0) { dom = tags[0]; // Take the first tag as the root (usually there's only one root) } else if(nodes.length > 0) { dom = nodes[0]; // Take the first node if no tags } }); const parser = new htmlparser.Parser(handler, { decodeEntities: false }); parser.write(html); parser.end(); // Convert DOM to hierarchical array structure if (!dom) { return []; // Return empty array if parsing failed } // Convert the single root element to our hierarchical structure return nodeToHierarchicalArray(dom); } /** * Recursive function to convert DOM nodes to hierarchical array * @param {Array|Object} nodes - DOM node or array of nodes * @returns {Array} - Hierarchical array representation */ function domToHierarchicalArray(nodes) { // Handle single node if(!Array.isArray(nodes)) { return nodeToHierarchicalArray(nodes); } // Handle array of nodes return nodes.map(node => nodeToHierarchicalArray(node)); } /** * Convert a single DOM node to hierarchical array * @param {Object} node - DOM node * @returns {Array} - Hierarchical array representation */ function nodeToHierarchicalArray(node) { // Handle text node if(node.type === 'text') { const text = node.data.trim(); if(text === '') { return null; // Skip empty text nodes } // Return text node as a single-element array to maintain consistency return [node]; } // Handle tag node if(node.type === 'tag') { // Create array with the complete node object as first element const result = [node]; // Process children and add them to the array if(node.children && node.children.length > 0) { const childArrays = node.children .map(child => nodeToHierarchicalArray(child)) .filter(item => item !== null); // Filter out null items (empty text) if(childArrays.length > 0) { result.push(...childArrays); } } return result; } // Other node types (comment, directive, etc.) - skip return null; } /** * Test function for the table row swapping example */ function testTableRowSwapping() { const oldHtml = `<table> <tr> <td> 1 </td> </tr> <tr> <td> 2 </td> </tr> </table>`; const newHtml = `<table> <tr> <td> 2 </td> </tr> <tr> <td> 1 </td> </tr> </table>`; // Parse HTML to hierarchical arrays const oldArray = htmlToHierarchicalArray(oldHtml); const newArray = htmlToHierarchicalArray(newHtml); const editScript = diffHierarchical(oldArray, newArray); } testTableRowSwapping(); Myersdiff代码分散了创建的列表: export class Keep { constructor(line, isSubtree = false) { this.type = 'Keep'; this.line = line; this.isSubtree = isSubtree; } } export class Insert { constructor(line, isSubtree = false) { this.type = 'Insert'; this.line = line; this.isSubtree = isSubtree; } } export class Remove { constructor(line, isSubtree = false) { this.type = 'Remove'; this.line = line; this.isSubtree = isSubtree; } } // Represents a point on the frontier with its history export class Frontier { constructor(x, history) { this.x = x; // x-coordinate in edit graph this.history = history; // sequence of operations to reach this point } } /** * Compare two node objects for equality * This is a critical function that determines when two nodes are considered "the same" */ function areNodesEqual(nodeA, nodeB) { // If both are text nodes, compare their trimmed content if (nodeA.type === 'text' && nodeB.type === 'text') { return nodeA.data.trim() === nodeB.data.trim(); } // If both are tags, compare tag names if (nodeA.type === 'tag' && nodeB.type === 'tag') { return nodeA.name === nodeB.name; } // Different types are never equal return false; } /** * Compare two subtrees for structural equality */ function areSubtreesEqual(subtreeA, subtreeB) { // Both must be arrays if (!Array.isArray(subtreeA) || !Array.isArray(subtreeB)) { return false; } // Must have the same number of elements if (subtreeA.length !== subtreeB.length) { return false; } // Root nodes must be equal if (!areNodesEqual(subtreeA[0], subtreeB[0])) { return false; } // Compare all children recursively for (let i = 1; i < subtreeA.length; i++) { if (!areSubtreesEqual(subtreeA[i], subtreeB[i])) { return false; } } return true; } /** * Hierarchical Myers diff algorithm * @param {Array} old - Old hierarchical array * @param {Array} current - New hierarchical array * @returns {Array} - Edit script */ function hierarchicalMyersDiff(old, current) { // Base case - if either is empty if (old.length === 0) { return current.map(item => new Insert(item, true)); } if (current.length === 0) { return old.map(item => new Remove(item, true)); } // Compare root nodes of the arrays const oldRoot = old[0]; const currentRoot = current[0]; // If the root nodes are equal, we have a potential match if (areNodesEqual(oldRoot, currentRoot)) { // First, check if these are leaf nodes or if their subtrees are equal if (old.length === 1 && current.length === 1) { // Leaf nodes - just keep return [new Keep(currentRoot)]; } // If both have exactly the same structure, keep the entire subtree if (areSubtreesEqual(old, current)) { return [new Keep(current, true)]; } // The roots match but the subtrees differ - recursively diff the children const result = [new Keep(currentRoot)]; // Extract children (everything except the root node) const oldChildren = old.slice(1); const currentChildren = current.slice(1); // Apply standard Myers diff to the children arrays const childDiff = myersDiff(oldChildren, currentChildren); result.push(...childDiff); return result; } // Root nodes are different - try standard Myers diff on the top level return myersDiff(old, current); } /** * Standard Myers diff algorithm for arrays * This is similar to the original myersDiff but adapted for our hierarchical arrays */ function myersDiff(old, current) { // Initialize the frontier with starting point const frontier = { 1: new Frontier(0, []) }; // Convert from 1-based to 0-based indexing function one(idx) { return idx - 1; } const aMax = old.length; // horizontal size of edit graph const bMax = current.length; // vertical size of edit graph // Main loop: try increasing numbers of edits for (let d = 0; d <= aMax + bMax + 1; d++) { // For each value of D, try all possible diagonals for (let k = -d; k <= d; k += 2) { // Determine whether to go down or right const goDown = (k === -d || (k !== d && frontier[k - 1].x < frontier[k + 1].x)); // Find starting point for this iteration let oldX, history; if (goDown) { ({ x: oldX, history } = frontier[k + 1]); var x = oldX; } else { ({ x: oldX, history } = frontier[k - 1]); var x = oldX + 1; } // Clone history to avoid modifying previous paths history = [...history]; let y = x - k; // Calculate y from x and k // Record the edit we made to get here if (y >= 1 && y <= bMax && goDown) { // Insert from new sequence when going down const currentItem = current[one(y)]; // Check if this is a subtree (an array) or a leaf node const isSubtree = Array.isArray(currentItem) && currentItem.length > 0; if (isSubtree) { // Add the entire subtree as a single insert operation history.push(new Insert(currentItem, true)); } else { history.push(new Insert(currentItem)); } } else if (x >= 1 && x <= aMax) { // Remove from old sequence when going right const oldItem = old[one(x)]; // Check if this is a subtree (an array) or a leaf node const isSubtree = Array.isArray(oldItem) && oldItem.length > 0; if (isSubtree) { // Add the entire subtree as a single remove operation history.push(new Remove(oldItem, true)); } else { history.push(new Remove(oldItem)); } } // Follow the snake: match diagonal moves as far as possible while (x < aMax && y < bMax) { const oldItem = old[one(x + 1)]; const currentItem = current[one(y + 1)]; // Both items must be arrays (subtrees) to compare if (Array.isArray(oldItem) && Array.isArray(currentItem)) { // Check if the subtrees are equal if (areSubtreesEqual(oldItem, currentItem)) { x += 1; y += 1; history.push(new Keep(currentItem, true)); } else { // Check if just the root nodes are equal if (oldItem.length > 0 && currentItem.length > 0 && areNodesEqual(oldItem[0], currentItem[0])) { x += 1; y += 1; // Add a Keep for the root node, then recursively diff the children history.push(new Keep(currentItem[0])); // Apply hierarchical diff to the children const oldChildren = oldItem.slice(1); const currentChildren = currentItem.slice(1); if (oldChildren.length > 0 || currentChildren.length > 0) { const childDiff = hierarchicalMyersDiff(...oldChildren, ...currentChildren); history.push(...childDiff); } } else { break; // No match, end of snake } } } else { break; // Different types, end of snake } } // Check if we've reached the end if (x >= aMax && y >= bMax) { return history; // Found solution } else { // Save this point in the frontier frontier[k] = new Frontier(x, history); } } } // If no solution found with the hierarchical approach, fallback to treating each element individually // This should be rare but ensures we always produce a result const fallbackResult = []; // Remove all old elements for (let i = 0; i < old.length; i++) { fallbackResult.push(new Remove(old[i])); } // Insert all new elements for (let i = 0; i < current.length; i++) { fallbackResult.push(new Insert(current[i])); } return fallbackResult; } /** * Process the edit script to expand subtree operations into individual node operations * This is necessary to integrate with the existing system */ export function expandEditScript(editScript) { const expanded = []; for (const operation of editScript) { if (operation.isSubtree) { // Convert subtree operation to individual node operations const node = operation.line[0]; // Root node const children = operation.line.slice(1); // Child subtrees // Add operation for the root node if (operation.type === 'Keep') { expanded.push(new Keep(node)); } else if (operation.type === 'Insert') { expanded.push(new Insert(node)); } else if (operation.type === 'Remove') { expanded.push(new Remove(node)); } // Recursively process children for (const child of children) { const childOperations = expandSubtreeOperation(child, operation.type); expanded.push(...childOperations); } } else { // Single node operation - add as is expanded.push(operation); } } return expanded; } /** * Helper function to expand a single subtree operation into individual node operations */ function expandSubtreeOperation(subtree, operationType) { const result = []; if (!Array.isArray(subtree)) { // Not a subtree, just a single node if (operationType === 'Keep') { result.push(new Keep(subtree)); } else if (operationType === 'Insert') { result.push(new Insert(subtree)); } else if (operationType === 'Remove') { result.push(new Remove(subtree)); } return result; } // Process the root node const rootNode = subtree[0]; if (operationType === 'Keep') { result.push(new Keep(rootNode)); } else if (operationType === 'Insert') { result.push(new Insert(rootNode)); } else if (operationType === 'Remove') { result.push(new Remove(rootNode)); } // Process all children recursively for (let i = 1; i < subtree.length; i++) { const childOperations = expandSubtreeOperation(subtree[i], operationType); result.push(...childOperations); } return result; } /** * Main entry point - applies hierarchical Myers diff and returns expanded edit script */ export function diffHierarchical(old, current) { const editScript = hierarchicalMyersDiff(old, current); return expandEditScript(editScript); } 在HTML字符串上运行Myers差异后的期望: 戈德: <table> <tr> <td> 1 </td> </tr> <tr> <td> 2 </td> </tr> </table> new: <table> <tr> <td> 2 </td> </tr> <tr> <td> 1 </td> </tr> </table> 它返回具有2个编辑的差异,该差异为1个,为1个,插入1个。相反,它返回4个编辑,其中删除了1个,插入了2个,插入了1个,插入了1个,并删除了2个。我该如何解决? sorry共享如此长的代码,但这是我在这个问题中讨论的用例的最小可重复示例。代码的第一部分只是读取HTML并将其编码为列表。第二部分是在第一部分生成的序列上运行的Myers diff实现。 在主题3中,请尝试修复JavaScript import htmlparser from 'htmlparser2'; import { diffHierarchical } from './beta-myers-diff.js'; /** * Converts HTML string to a hierarchical array structure * @param {string} html - The HTML string to parse * @returns {Array} - A hierarchical array representation of the HTML */ export function htmlToHierarchicalArray(html) { let dom = null; const handler = new htmlparser.DomHandler((error, nodes) => { if (error) { console.error('Error parsing HTML:', error); return; } dom = nodes; }); const parser = new htmlparser.Parser(handler, { decodeEntities: false }); parser.write(html); parser.end(); if (!dom) { return []; } return domToHierarchicalArray(dom); } /** * Recursive function to convert DOM nodes to hierarchical array * @param {Array|Object} nodes - DOM node or array of nodes * @returns {Array} - Hierarchical array representation */ function domToHierarchicalArray(nodes) { if (!Array.isArray(nodes)) { return nodeToHierarchicalArray(nodes); } return nodes.map(node => nodeToHierarchicalArray(node)).filter(item => item !== null); } /** * Convert a single DOM node to hierarchical array * @param {Object} node - DOM node * @returns {Array} - Hierarchical array representation */ function nodeToHierarchicalArray(node) { if (node.type === 'text') { const text = node.data.trim(); return text === '' ? null : text; } if (node.type === 'tag') { const result = [node.name]; if (node.attribs && Object.keys(node.attribs).length > 0) { result.push(node.attribs); } if (node.children && node.children.length > 0) { const childArrays = node.children.map(child => nodeToHierarchicalArray(child)).filter(item => item !== null); if (childArrays.length > 0) { result.push(childArrays); } } return result; } return null; } /** * Test function for the table row swapping example */ function testTableRowSwapping() { const oldHtml = `<table> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> </table>`; const newHtml = `<table> <tr> <td>2</td> </tr> <tr> <td>1</td> </tr> </table>`; const oldArray = htmlToHierarchicalArray(oldHtml); const newArray = htmlToHierarchicalArray(newHtml); const editScript = diffHierarchical(oldArray, newArray); console.log(editScript); } testTableRowSwapping();
那里有一个黑客打印第一个N fibonacci号码而无需调用循环 for(int i = 1;i 有黑客打印第一个n fibonacci号码而无需调用循环 for(int i=1; i<n; i++) System.out.println(computeF(n)); 从主要程序? public static int computeF(int n) { if(n==0) { return 0; } else if(n==1) { return 1; } else { return computeF(n-1)+computeF(n-2); } } 可能有一种打印递归中的中间值的方法,该值将打印斐波那契数。 您可以使用尾递归。 public class Fid { static int n1=0; static int n2=1; static int nex=0; public static void fb(int n) { if(n<10) { if(n==0) { System.out.print(" "+n); n++; fb(n); } else if(n==1) { System.out.print(" "+n); n++; fb(n); } else{ nex=n1+n2; System.out.print(" "+nex); n1=n2; n2=nex; n++; fb(n); } } } public static void main(String[] args) { fb(0); } } 使用递归: - class FibonacciRecursion { private static int index = 0; private static int stoppingPoint = 9; public static void main (String[] args) { int n1 = 0; int n2 = 1; fibonacciSequence(n1, n2); } public static void fibonacciSequence(int n1, int n2) { System.out.println("index: " + index + " -> " + n1); // make sure we have set an ending point so this Java recursion // doesn't go on forever. if (index == stoppingPoint) return; // make sure we increment our index so we make progress // toward the end. index++; fibonacciSequence(n2, n1+n2); } } // Java程序打印fibonacci系列最多由用户提供的n术语,而无需使用loop Importjava.util。*; 公共类斐波那契 { 公共静态void main(字符串[]参数) { Scanner s = new Scanner(System.in); System.out.print("Enter the no of terms :"); int no_of_terms= s.nextInt(),a=1,b=0,c=0,count=1; System.out.print("0 ");//printing the first term fib(no_of_terms,a,b,c,count);} 公共静态void fib(int no_of_terms,int a,int a,int b,int c,int count) { //when value of count will be equal to the no of terms given by user the program will terminate if (count==no_of_terms) System.exit(0); else { count++; System.out.print(a+" "); c=b; b=a; a=b+c;//calculating the next term fib(no_of_terms,a,b,c,count);//calling the function again with updated value } } } 您可以使用BINET的公式使其成为一个更简单的数学问题O(log n),但是当然,您必须为要解决的每次迭代运行它,如果您需要计算单个特定的fibinAcci值,这可能非常有用。对于非OP访问者:取决于您可能需要多少个值,可能坚持循环o(n)。 function fibonacci(n) { const phi = (1 + Math.sqrt(5)) / 2; const psi = (1 - Math.sqrt(5)) / 2; return Math.round((Math.pow(phi, n) - Math.pow(psi, n)) / Math.sqrt(5)); } console.log(fibonacci(5)); // Output: 5 console.log(fibonacci(9)); // Output: 34 列 https://allen.in/jee/maths/fibonacci-sequence(了解fibinacci的数学解决方案) https://www.geeksforgeeks.org/find-nth-fibonacci-number-using-susing-using-inets-formula/(javascript解决方案) Https://en.wikipedia.org/wiki/greek_letters_used_in_in_mathematics,\ _science,\_and_engineering(更多地好奇地对希腊字母在数学中) import java.util.*; public class Fibonacci{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a no."); int n= sc.nextInt(),a=1,b=0,c=0; num(n,a,b,c); } public static void num(int n,int a,int b,int c){ if(a<=n){ System.out.println(a); c=b; b=a; a=b+c; num(n,a,b,c); } } }
最有效的算法以在椭圆中找到整数点 我正在尝试在各种3D椭圆中找到所有整数晶格点。 我希望我的程序取一个整数n,并计算形式ax^2 +...
谨慎地,我想在小时的范围内达到n = 1,000,000+的数量
为长度为m + 1的索引拆分列表t,其中第一个元素为0,最后一个元素为n,生成一个长度为n的数组d,使得d [t [i]:t [i + 1]] = i。 例如,给定t = [0、2、5、7],
支持a = 17是一个整数 现在迭代 17-2 = 15 15-2 = 13 13-2 = 11 11-2 = 9 9-2 = 7 7-2 = 5 5-2 = 3 3-2 = 1 总共8个迭代 如何设计算法以计算奇数甚至
我想实现一个可以执行“基于愿景的页面细分”的程序。我需要一些指南和线索。 (我需要实用信息,而不仅仅是学术信息)
我阅读以下文章(VIP:基于视觉的页面细分算法),我认为这可能是一个很好的框架:
附录1:QuickSelect(正确版本)
我有一个整数,数组值代表 怪物。如果他的力量更大,怪物可以击败另一个怪物 而不是等于其力量。 当怪物击败Ano ...
我有一个整数,数组值代表 怪物。如果他的力量更大,怪物可以击败另一个怪物 而不是等于其力量。 当怪物击败另一个怪物时,获胜者赢得了失败者的力量。 如果所有怪物都有机会 为了获胜,找到怪物的零索引,如果战斗的命令最适合该怪物,并且该怪物赢得了所有50/50的战斗。
I具有均匀的随机值扩散在15x50网格上,我想哈希的样品对应于以任何可能的网格位置为中心的5x5单元的平方。
如何计算一组点的哈希码的最佳方法? 我正在寻找用于计算一组双维点的障碍的最佳方法(以便我可以将多边形存储在标签上)。 有一些明显的方法可以做到这一点,例如condenatin ...
在速度/碰撞频谱的另一端,我也可以总结所有坐标,这将导致非常快的代码,但也会造成很多碰撞。
找到最小的数字,该数字不能表示为1,2的总和或其他数字序列中的其他数字 我是C ++的新手,在以下任务中需要逻辑帮助。 给定n个正整数的顺序(n n
我似乎无法从中构建逻辑,为什么最后一个输出为4以及如何在C ++中实现它,任何帮助都将得到极大的赞赏。 到目前为止,这是我的代码:< 10^6; each given integer is less than 10^6), write a program to find the smallest