数据结构与算法之LeetCode-640. 求解方程(数学模拟)

news/2024/7/5 0:49:00

640. 求解方程 - 力扣(LeetCode)

  • factor表示变量的系数,val表示常量值
  • 使用系数sign1表示默认系数,解析到等号时,说明解析到等式右边的项,令sign1 = -1
  • 使用sign2表示项的符号,初识时sign2 = sign1 。解析到‘+’,‘-’,要么相应的更改sign2,使用number记录数字,valid表示number是否有效
  • 如果解析到变量,那么相应的更改factor
  • 如果解析到的是常量项,那么相应的更改val
  • 如果factor=0,说明变量x对方程无影响,然后判断val=0 是否成立,成立则说明方程有无数解,返回“Infinite solutions”否则返回“No solution”
  • 其他情况直接返回对应的整数解 x= - val / factor
/**
 * @param {string} equation
 * @return {string}
 */
var solveEquation = function(equation) {
    let factor = 0, val = 0;
    let index = 0, n = equation.length, sign1 = 1; // 等式左边默认系数为正
    while (index < n) {
        if (equation[index] === '=') {
            sign1 = -1; // 等式右边默认系数为负
            index++;
            continue;
        }

        let sign2 = sign1, number = 0;
        let valid = false; // 记录 number 是否有效
        if (equation[index] === '-' || equation[index] === '+') { // 去掉前面的符号
            sign2 = (equation[index] === '-') ? -sign1 : sign1;
            index++;
        }
        while (index < n && isDigit(equation[index])) {
            number = number * 10 + (equation[index].charCodeAt() - '0'.charCodeAt());
            index++;
            valid = true;
        }

        if (index < n && equation[index] === 'x') { // 变量
            factor += valid ? sign2 * number : sign2;
            index++;
        } else { // 数值
            val += sign2 * number;
        }
    }

    if (factor === 0) {
        return val === 0 ? "Infinite solutions" : "No solution";
    }
    return "x=" + (-val / factor);
};

const isDigit = (ch) => {
    return parseFloat(ch).toString() === "NaN" ? false : true;
}

执行结果:通过

执行用时:56 ms, 在所有 JavaScript 提交中击败了88.46%的用户

内存消耗:41.1 MB, 在所有 JavaScript 提交中击败了65.38%的用户

通过测试用例:59 / 59

const CHARS = '-+=x'
function solveEquation(equation){
  equation += '='
  let sign = 1,cur = 0,num = 0, k = 0, left = true, hasVal = false;
  for(let i=0i<equation.length;i++){
    const c = equation.charAt(i);
    if(CHARS.includes(c)){
      if(c == 'x'){
        if(!hasVal && cur == 0 ){
          cur =1;
        }
        k+= left? sign * cur : -sign*cur;
      }else{
        num -= left? sign*cur:-sign*cur;
      }
      cur = 0;
      hasVal = false;
    }
    
    switch(c){
      case '-':
        sign = -1;
        break
      case '+':
        sign = 1;
        break;
      case '=':
        sign = 1;
        left = false;
        break;
      case 'x':
        
        break;
      default:
        cur = cur * 10 +equation.charCodeAt(i) - '0'.charCodeAt(0)
        hasVal = true;
        break;
    }
    if(k == 0){
      return num!=0? "No solution" : "Infinite solutions";
    }
    return num%k == 0? 'x='+Math.floor(num/k):"No solution"
  }
}

参考链接

640. 求解方程 - 力扣(LeetCode)

求解方程 - 求解方程 - 力扣(LeetCode)

[[Python/Java/TypeScript/Go] 模拟 - 求解方程 - 力扣(LeetCode)](


http://www.niftyadmin.cn/n/4426644.html

相关文章

jquery过滤选择器-----------(基本过滤选择器)

1.介绍 2.常见基本过滤器 3.程序 1 <!DOCTYPE html>2 <html>3 <head>4 <meta charset"UTF-8">5 <title>Insert title here</title>6 <style type"text/css">7 div, span, p {8 width: 140px;9 …

Carlos Antollini 封装的ADO类说明

https://www.codeproject.com/Articles/1075/A-set-of-ADO-classes-version-2-20#Sample07

使用tinyXML生成并解析XML文件

使用tinyXML生成并解析XML文件非常简单&#xff0c;网上资源很多&#xff0c;为了节省时间、少走弯路&#xff0c;现归纳出如下的各种文档参考&#xff1a; 1、timyXML库下载地址&#xff1a;https://download.csdn.net/download/u012372584/14035757 2、XML文件语法说明&…

数据结构与算法之LeetCode-662. 二叉树最大宽度 -(DFS,BFS)

662. 二叉树最大宽度 - 力扣&#xff08;LeetCode&#xff09; 根节点的位置为1&#xff08;index左子节点的位置就为1*2右子节点的位置就为1*21 节点下标会非常大&#xff08;2**3000&#xff09;个子节点&#xff0c;超过JS的number范围&#xff0c;因此需要使用bigint避免溢…

运维未来的发展方向是智能运维

近年来运维技术飞速发展&#xff0c;运维团队大多建设好了各种系统&#xff0c;虚拟化、容器化、持续集成等等。但是如何有效的利用这些系统最终实现站点的高可用、高性能、高可扩展&#xff1f;随着智能化技术的发展&#xff0c;为了解决上述运维领域的问题&#xff0c;智能运…

ocilib库的使用

1、ocilib库的下载:http://vrogier.github.io/ocilib/ 2、ocilib库参考手册:http://vrogier.github.io/ocilib/doc/html/index.html 3、ocilib库windows下的配置使用(下面说明来自于官方文件): Using OCILIB on Microsoft Windows OCILIB distribution packages provi…

数据结构与算法之LeetCode-652. 寻找重复的子树

652. 寻找重复的子树 - 力扣&#xff08;LeetCode&#xff09; /*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefi…

N-122基于springboot,vue网上订餐系统

开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk1.8 项目构建&#xff1a;maven 数据库&#xff1a;mysql5.7 前端技术 &#xff1a;VueElementUI 服务端技术&#xff1a;springbootmybatisredis 本系统分用户前台和管理后台两部分&#xff0c;…