In the second week of October 21, one question per day was deducted

One question per day this week

  • lc482. Key format
  • lc284. Snooping iterator

10-04 lc482. Key format

  • Today is a simple question and a very simple one
  • Give a string and process it
  • Then give the value of K and divide the string into groups of multiple K characters. The first is less than or equal to K, so it's very simple. The first is n%k directly, and all the following are K. of course, if the result is 0, then each is k, and the first is set to K
  • Simple questions, look directly at the code, not much bb
class Solution {
    //Divide the string according to -, and then the first group is less than or equal to K, there is at least one, and the following group must contain exactly k characters
    //Then, because there are only letters, it needs to be capitalized. This has an api
    //That is, the first one has at least 1, but how much is divided according to the following integer division
    public String licenseKeyFormatting(String s, int k) {
        char[] ss = String.join("",s.split("-")).toUpperCase().toCharArray(); //Remove the dash, convert it to uppercase, and convert it to char array
        int n = ss.length;
        //Quantity of each
        int each = n%k==0?k:n%k; //The initial is the quantity of the first one, followed by k
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<n; i++){
                 each = k;
        return sb.toString();

10-05 lc284. Snooping iterator

  • This question is a little confused at first. I don't understand why
  • After looking at the api of java iterator, we can see that the original api provides us with hasNext() method and next() method, and we need to implement peek() method. This method returns the next element without moving the pointer
  • One detail here is that my peek is actually only used once. After I use peek, I will return to the next element and the pointer does not move. Then I peek again, or the previous element, because the pointer does not move
  • So the idea here is to use an int variable peekElement as the cache of peek value
  • If peek is used, then I will call the next method of the original api to save the returned value to peekElement. If I do peek again, I will return to this value. If I call the next method later, I will return peekElement, then make sure it is only used once.
  • That is to say, when I call peek, I have actually called next. I just deceive the user by caching so that the user thinks my pointer hasn't moved
  • Then, for the corresponding hasNext and next methods, the logic should judge the peekElement first. Here, I also add the logic of blank judgment, although the title is not required
class PeekingIterator implements Iterator<Integer> {
    //It is obvious that peek cannot be completed through the native api, so it should be implemented in a separate way
    //Secondly, I can only peek once if I don't want to next, that is, I only need to store one data, which is the previous one
    //As long as it is not 0, it is returned, but can only be used once
    private Iterator<Integer> iterator;
    private int peekElement = 0;// 0 means no data

	public PeekingIterator(Iterator<Integer> iterator) {
	    // initialize any member here.
        this.iterator = iterator;
    // Returns the next element in the iteration without advancing the iterator.
	public Integer peek() {
        if(peekElement!=0) return peekElement;//peek can be used indefinitely
        if(iterator.hasNext()) peekElement =;
        return peekElement;
	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	public Integer next() {
        int res = 0;
            res = peekElement;
            peekElement = 0;//It can only be used once
        else res =;
	    return res;
	public boolean hasNext() {
	    return iterator.hasNext()||peekElement!=0; //Description when peekElement is not 0

Tags: Java leetcode

Posted on Wed, 06 Oct 2021 18:32:35 -0400 by dougp23