国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 282. Expression Add Operators

wangjuntytl / 1022人閱讀

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.

Example 1:

Input: num = "123", target = 6
Output: ["1+2+3", "123"]
Example 2:

Input: num = "232", target = 8
Output: ["23+2", "2+32"]
Example 3:

Input: num = "105", target = 5
Output: ["1*0+5","10-5"]
Example 4:

Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]
Example 5:

Input: num = "3456237490", target = 9191
Output: []

Solution Save path to String
class Solution {
    public List addOperators(String num, int target) {
        List res = new ArrayList<>();
        if (num == null || num.length() == 0) return res;
        dfs(num, "", 0, target, res, 0, 0);
        return res;
    }
    
    private void dfs(String num, String temp, int index, int target, List res, long val, long multi) {
        if (index == num.length()) {
            if (val == target) res.add(temp);
            return;
        }
        for (int i = index; i < num.length(); i++) {
            if (i != index && num.charAt(index) == "0") break;
            
            long cur = Long.parseLong(num.substring(index, i+1));
            if (index == 0) {
                dfs(num, temp+cur, i+1, target, res, cur, cur);
            } else {
                dfs(num, temp+"+"+cur, i+1, target, res, val+cur, cur);
                dfs(num, temp+"-"+cur, i+1, target, res, val-cur, -cur);
                dfs(num, temp+"*"+cur, i+1, target, res, val-multi+multi*cur, multi*cur);
            }
        }
    }
}
Save path to StringBuilder
class Solution {
    public List addOperators(String num, int target) {
        List res = new ArrayList<>();
        dfs(num, 0, 0, target, 0, new StringBuilder(), res);
        return res;
    }
    private void dfs(String num, int index, long value, int target, long multi, StringBuilder sb, List res) {
        if (index == num.length()) {
            if (value == target) res.add(sb.toString());
            return;
        }
        for (int i = index; i < num.length(); i++) {
            if (num.charAt(index) == "0" && i != index) break;
            long cur = Long.parseLong(num.substring(index, i+1));
            int len = sb.length();
            if (index == 0) {
                sb.append(cur);
                dfs(num, i+1, cur, target, cur, sb, res);
                sb.setLength(len);
            } else {
                sb.append("+").append(cur);
                dfs(num, i+1, value+cur, target, cur, sb, res);
                sb.setLength(len);
                
                sb.append("-").append(cur);
                dfs(num, i+1, value-cur, target, -cur, sb, res);
                sb.setLength(len);
                
                sb.append("*").append(cur);
                dfs(num, i+1, value-multi+multi*cur, target, multi*cur, sb, res);
                sb.setLength(len);
            }
        }
    }
}
Use char[] to store num and use StringBuilder
class Solution {
    public List addOperators(String num, int target) {
        List res = new ArrayList<>();
        dfs(num.toCharArray(), 0, new StringBuilder(), 0, 0, target, res);
        return res;
    }
    private void dfs(char[] digits, int index, StringBuilder sb, long value, long multi, int target, List res) {
        if (index == digits.length) {
            if (value == target) res.add(sb.toString());
            return;
        }
        for (int i = index; i < digits.length; i++) {
            if (digits[index] == "0" && i != index) break;
            long cur = Long.parseLong(new String(digits, index, i-index+1));
            int len = sb.length();
            if (index == 0) {
                sb.append(cur);
                dfs(digits, i+1, sb, cur, cur, target, res);
                sb.setLength(len);
            } else {
                sb.append("+").append(cur);
                dfs(digits, i+1, sb, value+cur, cur, target, res);
                sb.setLength(len);
                
                sb.append("-").append(cur);
                dfs(digits, i+1, sb, value-cur, -cur, target, res);
                sb.setLength(len);
                
                sb.append("*").append(cur);
                dfs(digits, i+1, sb, value-multi+multi*cur, multi*cur, target, res);
                sb.setLength(len);
            }
        }
    }
}

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

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

相關(guān)文章

  • 282. Expression Add Operators

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

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

    摘要:唯一需要注意的就是乘法的情況,產(chǎn)生出,到達(dá)的時(shí)候,算出不包含的值,這里是,是乘號以前的算式值,算乘法部分。 Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * be...

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

    摘要:雙棧法四則運(yùn)算括號復(fù)雜度時(shí)間空間思路算符優(yōu)先算法,核心維護(hù)兩個(gè)棧,一個(gè)操作數(shù)棧,一個(gè)操作符棧。 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 添加運(yùn)算符

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

    sumory 評論0 收藏0
  • [LeetCode] 772. Basic Calculator III

    Problem Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and e...

    BlackHole1 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<