zoukankan      html  css  js  c++  java
  • unique-substrings-in-wraparound-string(好)

    https://leetcode.com/problems/unique-substrings-in-wraparound-string/

    好,我自己做出来的。多总结规律,多思考。

    package com.company;
    
    
    import java.util.HashMap;
    import java.util.Map;
    
    class Solution {
        public int findSubstringInWraproundString(String p) {
            Map<Integer, Integer> mp = new HashMap<>();
            int ret = 0;
            int cur = 0;
            char[] array = p.toCharArray();
            if (array.length < 1) {
                return 0;
            }
            for (int i=0; i<26; i++) {
                mp.put(i, 0);
            }
    
            ret++;
            cur++;
            mp.put(array[0]-'a', 1);
            for (int i=1; i<array.length; i++) {
                if ((array[i]-array[i-1]+26) % 26 == 1)  {
                    cur++;
                }
                else {
                    cur = 1;
                }
    
                if (cur > mp.get(array[i]-'a')) {
                    ret += cur - mp.get(array[i]-'a');
                    mp.put(array[i]-'a', cur);
                }
            }
            return ret;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            String p = "zab";
    
            Solution solution = new Solution();
            int ret  = solution.findSubstringInWraproundString(p);
    
            // Your Codec object will be instantiated and called as such:
            System.out.printf("ret:%d
    ", ret);
    
            System.out.println();
    
        }
    
    }
  • 相关阅读:
    java.lang.ArrayIndexOutOfBoundsException异常分析及解决
    Android_开发片段(Part 2)
    保存错误日志回传服务器之回传错误“信息文件”
    node.js
    拼接json
    CommonJS / Node.js/ Vue学习资料
    合并PDF
    java 多线程
    linux 运行jar包
    mvn 命令
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6158535.html
Copyright © 2011-2022 走看看