📣 独立日限时特惠: VIP通行证立减$68
楼主: akdhfikbk
跳转到指定楼层
上一主题 下一主题
收起左侧

Antra ICC 挂经

全局:
随手搜了下只看到了这个
回复

使用道具 举报

🔗
Se7en_ 2020-9-14 12:10:39 | 只看该作者
全局:
lz是java batch吗
回复

使用道具 举报

🔗
capricornhx 2022-4-9 14:42:42 | 只看该作者
全局:
本帖最后由 capricornhx 于 2022-4-9 02:47 编辑
  1. import java.util.List;
  2. import java.util.ArrayList;


  3. public class Solution {
  4.    
  5.    // recursion
  6.    public List<String> splitStr(String input) {
  7.       if (input.equals("")){
  8.          return new ArrayList<String>();
  9.       } else {
  10.          if (!input.startsWith("\"")) {
  11.             int cutoff = input.indexOf(", ");
  12.             List<String> lst = new ArrayList<String>();
  13.             lst.add(input.substring(0, cutoff));
  14.             lst.addAll(splitStr(input.substring(cutoff + 2)));
  15.             return lst;
  16.          } else {
  17.             int cutoff = input.indexOf("\", ");
  18.             List<String> lst = new ArrayList<String>();
  19.             lst.add(input.substring(1, cutoff));
  20.             lst.addAll(splitStr(input.substring(cutoff + 3)));
  21.             return lst;
  22.          }
  23.       }
  24.    }
  25.    
  26.    public static void main(String[] args) {
  27.       String input = "1.hello, 2.this is a test message, \"3.this has quotes, treat me as same message, thanks\", \"4.this is another message\"";
  28.       input = input + ", ";
  29.       Solution sol = new Solution();
  30.       List<String> result = sol.splitStr(input);
  31.       for (int i = 0; i < result.size(); i++){
  32.          System.out.println(result.get(i));
  33.       }
  34.    }
  35. }
复制代码
我试着做了一下这个题,貌似适合用recursion?我写的比较繁琐,不知道楼主有没有更好的写法
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表