for loop half of array size, add elements in 4 directions
publicstaticList<Integer>matrixInSpiralOrder(List<List<Integer>> squareMatrix){List<Integer> result =newArrayList<>();for (int i =0; i <Math.ceil(squareMatrix.size() *0.5); i++){helper(squareMatrix, result, i); }return result;}privatestaticvoidhelper(List<List<Integer>> squareMatrix,List<Integer> result,int offset){int size =squareMatrix.size();if(offset *2+1== size){result.add(squareMatrix.get(offset).get(offset));return; }for (int i = offset; i < size - offset -1; i++){result.add(squareMatrix.get(offset).get(i)); }for (int i = offset; i < size - offset -1; i++){result.add(squareMatrix.get(i).get(size -1- offset)); }for (int i = size -1- offset; i > offset ; i--){result.add(squareMatrix.get(size -1- offset).get(i)); }for (int i = size -1- offset; i > offset ; i--){result.add(squareMatrix.get(i).get(offset)); }}
395. Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.
Find pivots whose's count less than k,split the string by pivots,untill no pivot in substring
publicintlongestSubstring(String s,int k) {char[] c =s.toCharArray();returnhelper(c,0,s.length(), k); }privateinthelper(char[] c,int start,int end,int k){if(end-start < k) return0;// count the frequency of charMap<Character,Integer> map =newHashMap<>();for(int i=start; i<end; i++){map.put(c[i],map.getOrDefault(c[i],0)+1); }// D & Cfor(Map.Entry<Character,Integer> entry:map.entrySet()){if(entry.getValue() < k){for(int i=start; i<end; i++){if(c[i] ==entry.getKey()){int left =helper(c, start, i, k);int right =helper(c, i+1, end, k);returnMath.max(left, right); } } } }return end-start;}