成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

282. Expression Add Operators

Caicloud / 2235人閱讀

摘要:唯一需要注意的就是乘法的情況,產(chǎn)生出,到達的時候,算出不包含的值,這里是,是乘號以前的算式值,算乘法部分。

Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators 
(not unary) +, -, or * between the digits so they evaluate to the target value.

"123", 6 -> ["1+2+3", "1*2*3"] 
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []
public class Solution {
    public List addOperators(String num, int target) {
        List res = new ArrayList();
        dfs(num, target, res, "", 0, 0, 0);
        return res;
    }
    
    public void dfs(String num, int target, List res, String path, int pos, long eval, long mult){
        if(pos == num.length()){
            if(eval == target)
                res.add(path);
            return;
        }
        
        for(int i = pos; i < num.length(); i++){
            if(i != pos && num.charAt(pos) == "0") break;
            long cur = Long.parseLong(num.substring(pos, i+1));
            if(pos == 0){
                dfs(num, target, res, path + cur, i+1, cur, cur);
            } else {
                dfs(num, target, res, path + "+" + cur, i+1, eval + cur, cur);
                dfs(num, target, res, path + "-" + cur, i+1, eval - cur, -cur);
                /*
                 唯一需要注意的就是乘法的情況,"345" 產(chǎn)生出 3+4*5, 
                 到達5的時候,eval = 7, mul 4
                 eval-mult 算出不包含4的值,這里是7-4=3, 4是乘號以前的算式值,4*5, 算乘法部分。
                 參考Basic calculator II
                */
                dfs(num, target, res, path + "*" + cur, i+1, (eval-mult) + mult*cur , mult*cur);
            }
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66992.html

相關(guān)文章

  • 282. Expression Add Operators

    摘要:題目鏈接動態(tài)規(guī)劃問題,最后要求全部滿足條件的。還有個問題是取數(shù)字的時候可能超過的范圍,用來處理。的做法,討論切分點從到,本質(zhì)和做法是一樣的,復(fù)雜度也不會降低。關(guān)鍵是求值,又變成原來的問題了,所以這題感覺不能加。 282. Expression Add Operators 題目鏈接:https://leetcode.com/problems... 動態(tài)規(guī)劃問題,最后要求全部滿足條件的pa...

    enda 評論0 收藏0
  • [LeetCode] 282. Expression Add Operators

    Problem Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value...

    wangjuntytl 評論0 收藏0
  • [Leetcode] Basic Calculator/Evaluate Expression 設(shè)

    摘要:雙棧法四則運算括號復(fù)雜度時間空間思路算符優(yōu)先算法,核心維護兩個棧,一個操作數(shù)棧,一個操作符棧。 Basic Calculator 2 Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers...

    starsfun 評論0 收藏0
  • [Leetcode] Expression Add Operators 添加運算符

    摘要:問題在于如何將問題拆分成多次搜索。然而,乘法如何處理呢這里我們需要用一個變量記錄乘法當(dāng)前累乘的值,直到累乘完了,遇到下一個加號或減號再將其算入計算結(jié)果中。這樣的計算結(jié)果就是。注意第一次搜索不添加運算符,只添加數(shù)字,就不會出現(xiàn)這種表達式了。 Expression Add Operators Given a string that contains only digits 0-9 and...

    sumory 評論0 收藏0
  • Java中多個ifelse語句的替代設(shè)計

    摘要:但是有可能嵌套的語句只是轉(zhuǎn)移到了工廠類,這違背了我們的目的。這樣可以減少嵌套語句的數(shù)量,并將責(zé)任委托給單個值。一個評估規(guī)則和返回基于輸入的結(jié)果。首先,我們將定義一個接口其次,讓我們實現(xiàn)一個所述接受一個表達對象,并返回結(jié)果。概述 ifelse是任何編程語言的重要組成部分。但是我們編寫了大量嵌套的if語句,這使得我們的代碼更加復(fù)雜和難以維護。 接下來,讓我們探索如何簡化代碼的中的ifelse語句...

    izhuhaodev 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<