【问题标题】:Flutter/Dart: Split string into all possible combinationsFlutter/Dart:将字符串拆分为所有可能的组合
【发布时间】:2020-05-15 10:56:43
【问题描述】:

如何将字符串拆分为所有可用组合的块?例如:

“12345”

会输出:

[1, 12, 123, 1234, 12345, 2、 23, 234, 2345, 3、 34, 345, 4、 45]

据我所知:

String title = "12345";
List<String> keywordsList = List();
String temp = "";
String temp2 = "";
for (int i = 0; i < title.length; i++) {
  temp = temp + title[i];
  if (temp.length > 1) temp2 = temp2 + title[i];
  keywordsList.add(temp);
  if (temp2.length != 0) keywordsList.add(temp2);
 }
 print(keywordsList);
 return keywordsList;

},

结果:

[1、12、2、123、23、1234、234、12345、2345]

现在超级卡住了,将不胜感激。

提前致谢!

【问题讨论】:

  • 我不认为你停留在语言语法上,你需要here提供的逻辑实现。真的不知道这是否算作重复问题。

标签: flutter dart


【解决方案1】:

您可以通过以下方式实现。

String number = '12345';
List<String> listnumber = number.split("");
List<int> output = [];
for (int i = 0; i < listnumber.length; i++) {
  if (i != listnumber.length - 1) {
    output.add(int.parse(listnumber[i]));
  }
  List<String> temp = [listnumber[i]];
  for (int j = i + 1; j < listnumber.length; j++) {
    temp.add(listnumber[j]);
    output.add(int.parse(temp.join()));
  }
}
print(output.toString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 2018-06-03
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多