Middle-题目99:227. Basic Calculator II

本文介绍了一个简单的表达式计算器实现,该计算器能解析并计算包含+,-,*,/运算符及空格的表达式字符串,通过两次遍历字符串完成计算。
部署运行你感兴趣的模型镜像

题目原文:
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
“3+2*2” = 7
” 3/2 ” = 1
” 3+5 / 2 ” = 5
题目大意:
计算一个表达式字符串的值,其中含有+,-,*,/,空格和数字
题目分析:
既然用JavaScript的eval水过去会错,那就认真算一下。
首先没有括号,不用考虑括号的问题。那么从左到右先乘除后加减即可。遍历字符串两遍。
源码:(language:java)

public class Solution {
    public int calculate(String s) {
        s=s.replace(" ","");
        String[] nums = s.split("[\\+\\-\\*\\/]");
        List<Integer> numberList = new ArrayList<>();
        for(String num : nums)
            numberList.add(Integer.parseInt(num));
        int index = 0;
        for (int i = 0; i<s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '+' || ch == '-') 
                index++;
            else if (ch == '*' || ch == '/') {
                int op1 = numberList.get(index);
                int op2 = numberList.get(index+1);
                numberList.remove(index);
                numberList.remove(index);
                numberList.add(index, operate(op1,ch,op2));
            }
        }

        int result = numberList.get(0); index = 1;
        for(int i = 0;i<s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '+' || ch == '-') 
                result = operate(result, ch, numberList.get(index++));
        }
        return result;

    }
    private Integer operate(int a, char op, int b) {
        if(op=='+')
            return a+b;
        else if(op=='-')
            return a-b;
        else if(op=='*')
            return a*b;
        else 
            return a/b;
    }
}

成绩:
90ms,beats 13.83%,众数26ms,3.63%
Cmershen的碎碎念:
主要浪费时间还是在split上。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

内容概要:本文为《科技类企业品牌传播白皮书》,系统阐述了新闻媒体发稿、自媒体博主种草与短视频矩阵覆盖三大核心传播策略,并结合“传声港”平台的AI工具与资源整合能力,提出适配科技企业的品牌传播解决方案。文章深入分析科技企业传播的特殊性,包括受众圈层化、技术复杂性与传播通俗性的矛盾、产品生命周期影响及2024-2025年传播新趋势,强调从“技术输出”向“价值引领”的战略升级。针对三种传播方式,分别从适用场景、操作流程、效果评估、成本效益、风险防控等方面提供详尽指南,并通过平台AI能力实现资源智能匹配、内容精准投放与全链路效果追踪,最终构建“信任—种草—曝光”三位一体的传播闭环。; 适合人群:科技类企业品牌与市场负责人、公关传播从业者、数字营销管理者及初创科技公司创始人;具备一定品牌传播基础,关注效果可量化与AI工具赋能的专业人士。; 使用场景及目标:①制定科技产品全生命周期的品牌传播策略;②优化媒体发稿、KOL合作与短视频运营的资源配置与ROI;③借助AI平台实现传播内容的精准触达、效果监测与风险控制;④提升品牌在技术可信度、用户信任与市场影响力方面的综合竞争力。; 阅读建议:建议结合传声港平台的实际工具模块(如AI选媒、达人匹配、数据驾驶舱)进行对照阅读,重点关注各阶段的标准化流程与数据指标基准,将理论策略与平台实操深度融合,推动品牌传播从经验驱动转向数据与工具双驱动。
行 3278: 11-02 14:48:13.375 1707 5019 D OplusStartingWindowManager: we don't revise window flags for system activity :ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091} 行 3281: 11-02 14:48:13.375 1707 5019 D OplusStartingWindowManager: allowUseSnapshot ==> record =:ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091},newTask =:true,taskSwitch =:true,processRunning =:true,activityCreated =:false 行 3285: 11-02 14:48:13.376 1707 5019 D OplusStartingWindowManager: updateStartingRecords activity=ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091}, isAdd=true, size=156, call by:com.android.server.wm.OplusAppQuickStartController.attachExStartingSurfaceInner:811 com.android.server.wm.OplusAppQuickStartController.attachExStartingSurface:767 com.android.server.wm.QuickStartController.attachExStartingSurface:113 com.android.server.wm.ActivityRecordExtImpl.attachExStartingSurface:3549 com.android.server.wm.ActivityRecord.addStartingWindow:2843 行 3344: 11-02 14:48:13.389 1707 5019 D OplusStartingWindowManager: hookAddStartingWindow: not support forcePreview! ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091} 行 3345: 11-02 14:48:13.389 1707 5019 D OplusStartingWindowManager: hasSplashWindowFlag: hasFlag=false, mStartingWindowFlags=0x0 行 3346: 11-02 14:48:13.389 1707 5019 D OplusStartingWindowManager: hookAddStartingWindow: not supportSnapshotPreview! 行 3349: 11-02 14:48:13.391 1707 5019 D OplusStartingWindowManager: updateStartingRecords activity=ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091}, isAdd=true, size=156, call by:com.android.server.wm.ActivityRecordExtImpl.updateStartingRecords:3219 com.android.server.wm.ActivityRecord.scheduleAddStartingWindow:2906 com.android.server.wm.ActivityRecord.addStartingWindow:2851 com.android.server.wm.ActivityRecord.showStartingWindow:8911 com.android.server.wm.ActivityRecord.showStartingWindow:8806 行 3378: 11-02 14:48:13.409 1707 5019 D OplusStartingWindowManager: reviseWindowFlagsForStarting ar =:ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091}, activityCreated =:false, RUS version:20251009 行 3379: 11-02 14:48:13.409 1707 5019 D OplusStartingWindowManager: hasSplashWindowFlag: hasFlag=false, mStartingWindowFlags=0x0 行 3380: 11-02 14:48:13.409 1707 5019 D OplusStartingWindowManager: we don't revise window flags for system activity :ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091} 行 3402: 11-02 14:48:13.410 1707 5019 D OplusStartingWindowManager: notifyNoStartingWindow: act=com.coloros.calculator/com.android.calculator2.Calculator, code=2304 行 3734: 11-02 14:48:13.502 1707 4669 D OplusStartingWindowManager: addStartingBackColorLayerIfNeed no need, win:Window{5d8a833 u0 Splash Screen com.coloros.calculator},isStartingBackColorLayerAdded:false 行 3769: 11-02 14:48:13.568 1707 1749 D OplusStartingWindowManager: removeStartingBackColorLayerIfNeed no need, win:Window{5d8a833 u0 Splash Screen com.coloros.calculator} 行 3785: 11-02 14:48:13.576 1707 4723 D OplusStartingWindowManager: isLayerMatchToStartingWindow packageClassName:com.katanlabs.bubblepop/com.google.firebase.MessagingUnityPlayerActivity forceWaitLayer:null 行 3786: 11-02 14:48:13.576 1707 4723 D OplusStartingWindowManager: isLayerMatchToStartingWindow , activityHasSurfaceView:false, isLayerMatchToRecord:false, isSurfaceViewLayer:false, layerName:Splash Screen com.coloros.calculator#1437878, mStartingRecords:[UniqueWeakReference@99eeeac[null], UniqueWeakReference@d705209[null], UniqueWeakReference@93d9af5[null], UniqueWeakReference@69d33dd[null], UniqueWeakReference@8832ef5[null], UniqueWeakReference@aa96ca2[null], UniqueWeakReference@79425c0[null], UniqueWeakReference@5c708b3[null], UniqueWeakReference@df43acf[null], UniqueWeakReference@f751d09[null], UniqueWeakReference@adf5f7e[null], UniqueWeakReference@649f25f[null], UniqueWeakReference@a6ddef6[null], UniqueWeakReference@497f1bf[null], UniqueWeakReference@716dae7[null], UniqueWeakReference@ca733cc[null], UniqueWeakReference@427fe74[null], UniqueWeakReference@97b337c[null], UniqueWeakReference@ff7e429[null], UniqueWeakReference@fc4d12d[null], UniqueWeakReference@f170cd2[null], UniqueWeakReference@2223634[null], UniqueWeakReference@ecc6a7e[null], UniqueWeakReference@f72df2e[null], UniqueWeakReference@9b2e991[null], UniqueWeakReference@52aa757[null], UniqueWeakReference@316726[null], UniqueWeakReference@fee5d65[null], UniqueWeakReference@40a48cc[null], UniqueWeakReference@102cbb0[null], UniqueWeakReference@2b313ee[null], UniqueWeakReference@e3bde1f[null], UniqueWeakReference@8dfbfbf[null], UniqueWeakReference@1def358[null], UniqueWeakReference@1c9017[null], UniqueWeakReference@67bcb4a[null], UniqueWeakReference@86e82c6[null], UniqueWeakReference@6b65cd6[null], UniqueWeakReference@e1266cf[null], UniqueWeakReference@5e8da71[null], UniqueWeakReference@7faddc5[null], UniqueWeakReference@b41e182[null], UniqueWeakReference@a32dc4b[null], UniqueWeakReference@a5a59b1[null], UniqueWeakReference@5be32b9[null], UniqueWeakReference@f49b2bc[null], UniqueWeakReference@1c80d84[null], UniqueWeakReference@d625eff[null], UniqueWeakReference@c3c778e[null], UniqueWeakReference@be5a3d3[null], UniqueWeakReference@1606e32[null ... 行 4661: 11-02 14:48:14.210 1707 4508 D OplusStartingWindowManager: addStartingBackColorLayerIfNeed no need, win:Window{5c5b9c5 u0 com.coloros.calculator/com.android.calculator2.Calculator},isStartingBackColorLayerAdded:false 行 5242: 11-02 14:48:14.561 1707 1749 D OplusStartingWindowManager: interceptRemoveStartingWindow: packageName=com.coloros.calculator, componentName=com.coloros.calculator/com.android.calculator2.Calculator, isSnapshot=false, startingSurfaceRunnable=null, isDrawCompleted=false, hasSurfaceView=false, inTransition=false 行 5244: 11-02 14:48:14.561 1707 1749 D OplusStartingWindowManager: hasSplashWindowFlag: hasFlag=false, mStartingWindowFlags=0x0 行 5245: 11-02 14:48:14.561 1707 1749 D OplusStartingWindowManager: interceptRemoveStartingWindow: hasSplashWindowFlag() return false for activity=ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091} 行 5286: 11-02 14:48:14.586 1707 4508 D OplusStartingWindowManager: hasRemoveStartingWindowImmediatelyFlag: has=false, mStartingWindowFlags=0x0 行 5287: 11-02 14:48:14.586 1707 4508 D OplusStartingWindowManager: hasSplashWindowFlag: hasFlag=false, mStartingWindowFlags=0x0 行 5289: 11-02 14:48:14.586 1707 4508 D OplusStartingWindowManager: createStartingWindowExitAnimationForPackage duration =:0,windowState =:Window{5d8a833 u0 Splash Screen com.coloros.calculator} 行 5364: 11-02 14:48:14.607 1707 1749 D OplusStartingWindowManager: supportWindowPreviewWhenStartingWindowExit Entry =: StartingWindowAttributeCache Entry : [ windowPreviewType : 0,windowSnapshotPreviewToken : null,supportSnapshotPreview : false,supportTitlePreview : false,startingWindowTitle : null ],spend time =: 24077 行 5365: 11-02 14:48:14.607 1707 1749 D OplusStartingWindowManager: putSnapshotForTaskPreview: mRequestStoreAppTokenSnapshot=false, mAllowAppSnapshot=true, activity=ActivityRecord{90637794 u0 com.coloros.calculator/com.android.calculator2.Calculator t19091} 行 5465: 11-02 14:48:14.630 1707 6817 D OplusStartingWindowManager: skip remove starting window for activity = ActivityRecord{9972313 u0 com.android.launcher/.Launcher t19232} ,ar != topActivityContainsStartingWindow on activityStopped 行 5469: 11-02 14:48:14.631 1707 6817 D OplusStartingWindowManager: updateStartingRecords activity=ActivityRecord{9972313 u0 com.android.launcher/.Launcher t19232}, isAdd=false, size=156, call by:com.android.server.wm.OplusStartingWindowManager.onActivityStopped:2920 com.android.server.wm.ActivityRecordExtImpl.onActivityStopped:1826 com.android.server.wm.ActivityRecord.activityStopped:7991 com.android.server.wm.ActivityClientController.activityStopped:306 android.app.IActivityClientController$Stub.onTransact:710 行 6443: 11-02 14:48:15.436 1707 4388 D OplusStartingWindowManager: addStartingBackColorLayerIfNeed no need, win:Window{3dd44f2 u0 Toast},isStartingBackColorLayerAdded:false
11-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值