LeetCode刷题记197-String(字符串)

842. 将数组拆分成斐波那契序列

class Solution {
    List<Integer> ans;
    char[] chars;
    int n;

    public boolean F(int fro, int f0, int f1, List<Integer> cur) {
        if (fro >= n) return false;
        int f2 = 0;
        for (int i = fro; i < n; i ++) {
            if (i > fro && f2 == 0 && chars[i] != '0') break;  // 0000123 
            if (f2 * 10 + chars[i] - '0' < f2) break;
            f2 = f2 * 10 + chars[i] - '0';
            if (f2 == f0 + f1) {
                List<Integer> tmp = new ArrayList(cur);
                tmp.add(f2);
                if (i == n - 1) {
                    ans = tmp;
                    return true;
                }
                if (F(i + 1, f1, f2, tmp)) {
                    return true;
                } 
            }
        }
        return false;
    }
    public List<Integer> splitIntoFibonacci(String S) {
        chars = S.toCharArray();
        n = S.length();
        ans = new ArrayList<Integer>();

        int f0 = 0, f1 = 0;

        for (int i = 0; i < n; i ++) {
            if (i > 0 && f0 == 0 && chars[i] != '0') break;  // 0000123 
            if (f0 * 10 + chars[i] - '0' < f0) break;
            f0 = f0 * 10 + chars[i] - '0';
            f1 = 0;
            for (int j = i + 1; j < n; j ++) {
                if (j > i + 1 && f1 == 0 && chars[j] != '0') break;  // 0000123 
                if (f1 * 10 + chars[j] - '0' < f1) break;
                f1 = f1 * 10 + chars[j] - '0';
                if(F(j + 1, f0, f1, new ArrayList<Integer>(Arrays.asList(f0, f1)))) return ans;
            }
            if (f1 < 0) break;
        }

        return ans;
    }
}

828. 统计子串中的唯一字符

class Solution {
    char[] chs;
    List<Integer>[] ind;
    public int uniqueLetterString(String s) {
        int mod = 1000000007;
        chs = s.toCharArray();
        ind = new ArrayList[26];  // 每个字母出现过的下标位置
        
        int ans = 0;
        int cur = 0;
        for (int i = 0; i < s.length(); i ++) {
            int id = chs[i] - 'A';
            if (ind[id] == null) {
                ind[id] = new ArrayList<Integer>();
                cur = (cur + i + 1) % mod;
            } else {
                int frist = ind[id].get(ind[id].size() - 1);
                int second = -1;
                if (ind[id].size() > 1) {  // 之前不止出现一次
                    second =  ind[id].get(ind[id].size() - 2);
                }
                // 最近一次出现到当前之间的数字都+1
                cur = (cur + (i - frist - 1) + 1) % mod;
                // 最近一次出现的位置到第二近出现的位置之间的数都-1
                cur = (cur + mod - (frist - second)) % mod;
            }
            ans = (ans + cur) % mod;
            ind[id].add(i);
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值