publicstaticList<Integer>partitionLabels(StringS){List<Integer> list =newLinkedList<>();char[] chars =S.toCharArray();int[] edges =newint[26];for(int i =0; i < chars.length; i++){edges[chars[i]-'a']= i;}int index =0, pre =-1;for(int i =0; i < chars.length; i++){index =Math.max(index, edges[chars[i]-'a']);if(i == index){list.add(i - pre);pre = i;}}return list;}
加油站问题(LeetCode 134)
publicstaticintcanCompleteCircuit(int[] gas,int[] cost){int totNum =0;int curNum =0;int start =0;for(int i =0; i < gas.length; i++){curNum += gas[i]- cost[i];totNum += gas[i]- cost[i];if(curNum <0){curNum =0;start = i +1;}}if(totNum <0){return-1;}return start;}