电脑怎么做最新系统下载网站,河北省建设主管部门网站,手机制作网页多少钱,哈市建设网站目录 题目1- 思路2- 实现⭐763. 划分字母区间——题解思路 3- ACM 实现 题目
原题连接#xff1a;763. 划分字母区间 1- 思路
思路 目标#xff1a;同样的字母 字符串尽可能的长
问1#xff1a;怎么确定字母数 —— 哈希表问2#xff1a;怎么让字符尽可能的长#… 目录 题目1- 思路2- 实现⭐763. 划分字母区间——题解思路 3- ACM 实现 题目
原题连接763. 划分字母区间 1- 思路
思路 目标同样的字母 字符串尽可能的长
问1怎么确定字母数 —— 哈希表问2怎么让字符尽可能的长—— 统计每个字符出现的最远位置根据单个字符的最远出现位置判断字符串的最远出现位置 如果满足 字符串中所有字符的最远出现位置 当前字符串的最远出现位置这个字符串就是最长的 2- 实现
⭐763. 划分字母区间——题解思路 class Solution {ListInteger res new ArrayList();public ListInteger partitionLabels(String s) {// 1.定义 hashint[] hash new int[26];// 2. 求单个字母最远距离for(int i 0 ; i s.length();i){hash[s.charAt(i) - a] i;}int left 0;int right 0;// 3. 实现逻辑for(int i 0 ; i s.length();i){right Math.max(right,hash[s.charAt(i)-a]);if(iright){res.add(right-left1);left right1;}}return res;}
}3- ACM 实现
public class longestSub {static ListInteger res new ArrayList();public static ListInteger partitionLabels(String str){// 1. 定义 hashint len str.length();int[] hash new int[len];// 2. 求单个字符最远for(int i 0 ; i len;i){hash[str.charAt(i)-a] i;}int left 0;int right 0;// 3. 实现逻辑for(int i 0 ; i len;i){right Math.max(right,hash[str.charAt(i)-a]);if(iright){res.add(right-left1);left right1;}}return res;}public static void main(String[] args) {Scanner sc new Scanner(System.in);String str sc.nextLine();ListInteger forRes partitionLabels(str);System.out.println(forRes.toString());}
}