华为od机试C卷-最长表达式求值

1 题目描述

提取字符串中的最长合法简单数学表达式子串,字符串长度最长的,并计算表达式的值,如果没有返回0。简单数学表达式只能包含以下内容0-9 数字,符号±*

说明:

1.所有数字,计算结果都不超过 long

2.如果有多个长度一样的,请返回第一个表达式的结果

3.数学表达式,必须是最长的,合法的

4.操作符不能连续出现,如 ±-+1 是不合法的

输入

字符串
输出

表达式值

示例一

输入: 1-2abcd

输出: -1

示例二

输入: 1-2abs1-2*3+7dd4-5+6

输出: 2

示例三

输入: a1/0+8d

输出: 8

2、审题

这道题有点难度,有以下几个难点:

1、如何找出字符串中的最长合法数学表达式子串?

2、如何计算该数学表达式子串的值?

3、四则运算有一种特殊情况,除数不为零,这种也必须考虑。

上述三个问题的对应解法:

1、用正则表达式筛选表达式。

2、用递归方法计算,主要优势代码简洁。

3、解法

java 复制代码
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
 
        String longStr = dealWith(str); //找到最长合法数学表达式子串
 
        if (!longStr.isEmpty()) {
            int result = calculate(longStr);
            System.out.println(result);
        } else {
            System.out.println(0);
        }
    }
 
    //判断字符串是否只有数字
    private static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
 
    //利用递归方法计算四则运算,这里+和-放在前面是有特殊原因,因为加法和减法的优先级低于乘法和除法
    private static int calculate(String str) {
        if (isNumber(str)) {
            return Integer.parseInt(str);
        }
 
        if (str.contains("+")) {
            int index = str.indexOf("+");
            return calculate(str.substring(0, index)) + calculate(str.substring(index + 1));
        }
 
        if (str.contains("-")) {
            int index = str.indexOf("-");
            return calculate(str.substring(0, index)) - calculate(str.substring(index + 1));
        }
 
        if (str.contains("*")) {
            int index = str.indexOf("*");
            return calculate(str.substring(0, index)) * calculate(str.substring(index + 1));
        }
 
        if (str.contains("/")) {
            int index = str.indexOf("/");
            return calculate(str.substring(0, index)) / calculate(str.substring(index + 1));
        }
        
        return 0;
    }
 
    //借助正则表达式拆出符合要求的表达式
    private static String dealWith(String str) {
        //将除数等于0时,表达式就不是合法的数学表达式,所以可以简单地用a0给替换,注意string是不可变量,所以要用新的变量代替
        String replaceStr = str.replace("/0", "a0");
 
        int max = 0;
        String longStr = "";
        String regex = "\\d+([\\+\\-*/]\\d+)+"; //这个正则表达式的意思:寻找以数字开头,+-*/其中之一紧随其后,而后又是一个数字的一个或者多个模式
 
        Pattern pattern= Pattern.compile(regex);
        Matcher matcher = pattern.matcher(replaceStr);
 
        while (matcher.find()) {
            String group = matcher.group();
            if (group.length() > max) {
                max = group.length();
                longStr = group;
            }
        }
        
        return longStr;
    }
}

网上很多要付费才能看到。

相关推荐
塔中妖4 天前
【华为OD】分割数组的最大差值
数据结构·算法·华为od
塔中妖4 天前
【华为OD】数字游戏
算法·游戏·华为od
熊文豪6 天前
【华为OD】找出通过车辆最多颜色
算法·华为od
塔中妖6 天前
【华为OD】环中最长子串2
算法·华为od
熊文豪6 天前
【华为OD】区块链文件转储系统
算法·华为od·区块链
塔中妖6 天前
【华为OD】Linux发行版的数量
linux·算法·华为od
熊文豪6 天前
【华为OD】阿里巴巴找黄金宝箱
算法·华为od
塔中妖6 天前
【华为OD】5G网络建设
网络·5g·华为od
塔中妖6 天前
【华为OD】查找接口成功率最优时间段
算法·链表·华为od
塔中妖6 天前
【华为OD】最大子矩阵和
算法·华为od·矩阵