LeetCode "Sword Finger Offer", with ideas (27)

Hello everyone, I do 方圆
n’t have it, but I am familiar with it


Sword refers to Offer 57-II. The sequence of continuous positive numbers whose sum is s

Insert picture description here

  • Idea : For this kind of ordered array, first think of double pointers and sliding windows
class Solution {
    
    
    public int[][] findContinuousSequence(int target) {
    
    
        int i = 1;
        int j = 1;
        int sum = 0;
        List<int[]> res = new ArrayList<>();

        while(i <= target / 2){
    
    
            if(sum < target){
    
    
                sum += j;
                j++;
            }else if(sum > target){
    
    
                sum -= i;
                i++;
            }else{
    
    
                int[] arr = new int[j - i];
                for(int k = i;k < j;k++){
    
    
                    arr[k - i] = k;
                }
                res.add(arr);
                sum -= i;
                i++;
            }
        }

        return res.toArray(new int[res.size()][]);
    }
}

Sword Finger Offer 58-I. Reverse word order

Insert picture description here

class Solution {
    
    
    public String reverseWords(String s) {
    
    
        s = s.trim();
        int i = s.length() - 1,j = i;
        StringBuilder res = new StringBuilder();

        while(i >= 0){
    
    
            while(i >= 0 && s.charAt(i) != ' ')
                i--;//将i指向单词前的空格
            res.append(s.substring(i + 1,j + 1) + " ");
            while(i >= 0 && s.charAt(i) == ' ')
                i--;//去掉两单词间多余的空格
            j = i;
        }

        //去掉尾部多余的一个空格
        return res.toString().trim();
    }
}

Sword Finger Offer 58-II. Rotate String Left

Insert picture description here

  • Idea: Cut into two sections
class Solution {
    
    
    public String reverseLeftWords(String s, int n) {
    
    
        return s.substring(n,s.length()) + s.substring(0,n);
    }
}

Sword Finger Offer 59-I. Maximum sliding window

Insert picture description here

class Solution {
    
    
    public int[] maxSlidingWindow(int[] nums, int k) {
    
    
        if(nums == null || k == 0) return new int[0];

        int[] res = new int[nums.length - k + 1];
        int i = 0,j = k - 1;
        int n = 0;

        while(j < nums.length){
    
    
            int flag = Integer.MIN_VALUE;
            for(int a = 0;a < k;a++)
                flag = Math.max(flag,nums[i + a]);
            i++;
            j++;
            res[n++] = flag;
        }

        return res;
    }
}

The sword refers to Offer 59-II. The maximum value of the queue

Insert picture description here

class MaxQueue {
    
    

    private Queue<Integer> queue;
    private LinkedList<Integer> max;

    public MaxQueue() {
    
    
        queue = new LinkedList<>();
        max = new LinkedList<>();
    }
    
    public int max_value() {
    
    
        return max.size() != 0 ? max.getFirst() : -1;
    }
    
    public void push_back(int value) {
    
    
        queue.add(value);
        //这里是while循环条件!!!
        while(max.size() != 0 && max.getLast() < value)
            max.removeLast();
        max.add(value);
    }
    
    public int pop_front() {
    
    
        if(max.size() != 0 && max.getFirst().equals(queue.peek()))
            max.removeFirst();
        
        return queue.size() != 0 ? queue.poll() : -1;
    }
}

Come on!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107423272