using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @param target int整型 * @return string字符串一维数组 */ public List<string> addOpt (string num, int target) { // write code here if (string.IsNullOrWhiteSpace(num) || (num.Length == 1 && num[0] != target)) return null; List<string> lsStr = new List<string>(); string strBDS = num.Substring(0, 1); DG(num, target, int.Parse(strBDS), 1, ref lsStr, strBDS); return lsStr; } public static void DG(string num, int target, int nCurR, int nL , ref List<string> lsStr, string strBDS) { // write code here if (nCurR == target && nL == num.Length) { lsStr.Add(strBDS); return; } for (int nIndex = nL + 1; nIndex <= num.Length; nIndex++) { string strLeft = num.Substring(nL, nIndex - nL); int nLeft = int.Parse(strLeft); //'+'加法 DG(num, target, nCurR + nLeft, nIndex, ref lsStr, strBDS + "+" + strLeft); //'-'减法 DG(num, target, nCurR - nLeft, nIndex, ref lsStr, strBDS + "-" + strLeft); //'*'乘法 DG(num, target, nCurR * nLeft, nIndex, ref lsStr, strBDS + "*" + strLeft); ////''拼接成多位数 //int nAdd = strBDS.LastIndexOf('+'); //int nSub = strBDS.LastIndexOf('-'); //int nMul = strBDS.LastIndexOf('*'); //int nMax = Math.Max(Math.Max(nAdd, nSub), nMul); //if (nMax < 0) // nCurR = int.Parse(strBDS + strLeft); //else //{ // string strL // switch (strBDS[nMax]) // { // case '+': // nCurR += // break; // case '-': // break; // case '*': // break; // } //} //DG(num, target, nCurR - nLeft, nIndex, ref lsStr, strBDS + strLeft); } } }