LeetCode 数据结构基础 Day7 Java

LeetCode 数据结构基础 Day7 Java,第1张

LeetCode 数据结构基础 Day7 Java

290. 单词规律

问题描述:

 代码:

class Solution {
    public boolean wordPattern(String pattern, String s) {
        char []p=pattern.toCharArray();
        Map hx = new HashMap<>();
        String[] arr = s.split(" ");
        if(p.length!=arr.length)
            return false;
        for(int i=0;i 

思路:

String[] arr = s.split(" ");  根据空格来分割字符串

哈希containsValues

HashMap yyds!

763. 划分字母区间

问题描述:

 代码:

class Solution {
    public List partitionLabels(String s) {
        int n=s.length();
        int []ans=new int[26];
         for (int i = 0; i < n; i++) {
            ans[s.charAt(i) - 'a'] = i;
        }
     int start = 0, end = 0;
        List list = new linkedList<>();
         for (int i = 0; i < n; i++) {
            end = Math.max(end, ans[s.charAt(i) - 'a']);
            if (i == end) {
                list.add(end - start + 1);
                start = end + 1;
            }
        }
            return list;
    }
}

思路:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5660513.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存