当前位置:网站首页>Rehabilitation type force deduction brush question notes D3

Rehabilitation type force deduction brush question notes D3

2022-07-05 06:34:00 Silent moon cold moon

Catalog

Small diary written in front

Code section

topic 1576

topic 26

topic 27

topic 5

java Small knowledge part

topic 1576

topic 27

topic 5


Small diary written in front

        Change the notebook c Face and power supply . Finally, you can support me to press n Key and use the computer without plugging in . There is a small problem , When fixing the damaged mechanical hard disk cable with insulating tape, the original stuck to the back, resulting in power on failure , But it was solved smoothly , Fortunately, the motherboard is not broken , Or I'll just “ Complete madness ”.

Code section

topic 1576

1576. Replace all the question marks

The difficulty is simple 99 Switch to English to receive dynamic feedback

Here's a small alphabet and  '?'  String of characters  s, Please put all the  '?'  Convert to a number of lowercase letters , So that the final string does not contain any   Keep repeating   The characters of .

Be careful : you   You can't   Modify non  '?'  character .

Topic test cases guarantee   except  '?'  character   outside , There are no consecutive repeated characters .

At the completion of all transformations ( There may be no need to convert ) After the return of the final string . If there are multiple solutions , Please return to any of them . Can prove that , Under given constraints , The answer is always there .

        Own code :

class Solution {
    public String modifyString(String s) { //a:97
        int len = s.length();
        char[] S = s.toCharArray();
        if(S[0] == '?')
        {
            if(len >1 && S[1] >= 97 && S[1] < 122) {S[0] = (char)(S[1]+1);}
            else {S[0] = 97;}
        }
        for(int i = 1 ; i < len ; i ++)
        {
            if(S[i] != '?') {continue;}
            else
            {
                int head = (int)S[i-1];
                if(head == 122) {head = 96;}
                if(i != len-1 && S[i+1] != head+1) {S[i] = (char)(head+1);}
                else if(i == len-1) {S[i] = (char)(head+1);}
                else if(i != len-1 && S[i+1] == head+1) 
                {
                    if(head == 121) {S[i] = 97;}
                    else {S[i] = (char)(head+2);}
                }
            }
        }
        String res = new String(S);
        return res;
    }
}

        First judge the special situation . First, judge the first position , Make the first position not a question mark . Start from the second position , Fill in according to the characters in the previous position , Fill in the previous letter +1( The former is z Then fill in a).

topic 26

26. Remove duplicate items from an ordered array

The difficulty is simple 2389 Switch to English to receive dynamic feedback

Here's an ordered array  nums , Would you please   In situ   Delete duplicate elements , Make each element   Only once  , Returns the new length of the deleted array .

Don't use extra array space , You must be there.   In situ   Modify input array   And using O(1) Complete with extra space .

explain :

Why is the return value an integer , But the output answer is array ?

Please note that , The input array is based on 「 quote 」 By way of transmission , This means that modifying the input array in a function is visible to the caller .

You can imagine the internal operation as follows :

// nums  In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments 
int len = removeDuplicates(nums);

//  Modifying the input array in a function is visible to the caller .
//  Based on the length returned by your function ,  It prints out the array   Within this length   All elements of .
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

        Own code :

class Solution {
    public int removeDuplicates(int[] nums) {
        int len = nums.length;
        if(len == 0 || len == 1) {return len;}
        // Double finger needling 
        int insert , search;
        insert = 0;
        for(search = 1 ; search < len ; search ++)
        {
            if(nums[search] == nums[search-1]) {continue;}
            else 
            {
                insert ++;
                nums[insert] = nums[search];
            }
        }
        return insert+1;

    }
}

          Vaguely, I used to write this question , stay acm over there ( Only for a short time ). First judge the special situation . Set double pointer , One for detecting characters , One is used to locate the insertion position . If the probe character is not equal to its previous character , Then put the character at the insertion position , And move the insertion position backward ; If equal , be continue fall , Judge the next character .

topic 27

27. Remove elements

The difficulty is simple 1137 Switch to English to receive dynamic feedback

Give you an array  nums  And a value  val, You need   In situ   Remove all values equal to  val  The elements of , And return the new length of the array after removal .

Don't use extra array space , You have to use only  O(1)  Additional space and   In situ   Modify input array .

The order of elements can be changed . You don't need to think about the elements in the array that follow the new length .

explain :

Why is the return value an integer , But the output answer is array ?

Please note that , The input array is based on 「 quote 」 By way of transmission , This means that modifying the input array in a function is visible to the caller .

You can imagine the internal operation as follows :

// nums  In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments 
int len = removeElement(nums, val);

//  Modifying the input array in a function is visible to the caller .
//  Based on the length returned by your function ,  It prints out the array   Within this length   All elements of .
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

        Own code :

class Solution {
    public int removeElement(int[] nums, int val) {
        int len = nums.length;
        if(len == 0) return 0;
        if(len == 1 && nums[0] != val) return 1;
        if(len == 1 && nums[0] == val) {nums = null; return 0;}
        int search , insert = 0;
        for(search = 0 ; search < len ; search ++)
        {
            if(nums[search] == val)
            {
                continue;
            }
            else
            {
                nums[insert] = nums[search];
                insert ++;
            }
        }
        return insert;
    }
}

          This question is related to 26 The questions are essentially the same , But not compared with the previous character , But with val Compare .

topic 5

5. Longest text substring

Medium difficulty 4545 Switch to English to receive dynamic feedback

Give you a string  s, find  s  The longest palindrome substring in .

        Own code :

class Solution {
    public String longestPalindrome(String s) {
        int len = s.length();
        if(len == 0 || len == 1) {return s;}
        char[] S = s.toCharArray();
        int position = 0 ,lenres = 1;
        for(int i = 0 ; i < len ; i++)
        {
            if(len - i + 1 <= lenres) 
            {
                String res = s.substring(position , position+lenres);
                return res;
             }
            int left , right , right0;
            left = i;
            right0 = right = len;
            while(left < right)
            {
                left = i;
                right = right0-1;
                while(left < right && S[left] != S[right]) {right --;}
                right0 = right;
                while(left < right && S[left] == S[right])
                {
                    left ++;
                    right --;
                }
                if(S[left] == S[right]) 
                {
                    if(right0 - i + 1 > lenres)
                    {
                        lenres = right0 - i + 1;
                        position = i;
                    }
                }
                else {continue;}
            }
        }
        String res = s.substring(position , position+lenres);
        return res;
    }
}

         Turn the string , Use for loop , Gradually shorten the search area . For a search area , Set the header to the header of the region , The tail is the tail of the region , Look for the same character as the head from the tail forward , Set to the new tail , For head and tail simultaneous inward detection , Judge whether palindrome or not . If the paragraph is palindrome , Then compare the length of this segment with the length of the longest palindrome string saved , If it exceeds, the length and the starting position of the palindrome string will be updated ; If not, look for the same character as the head from the tail before detection again , Head return , Judge again . When shortening the search area , Determine whether the new search area is shorter than the length of the longest palindrome string saved , If shorter than , Then there is no need to continue , Go straight back to , If longer than, continue to judge .

java Small knowledge part

topic 1576

        Unicode In code a~z The number of is 97-122(61H-7AH).A~Z The number of is 65-90(41H-5AH).0~9 The number of is 48-57(30H-39H).

        String turn char[] use .toCharArray[];char[] turn String use = new String(S);.

topic 27

        Make sure again ,String String length is .length();int The length of an array is length.

topic 5

        When the area is shortened, it is added to judge whether the area has been smaller than the current maximum length , This can shorten the algorithm time .

        Notice the variation of variables in the loop , Determine which variable values need regression , Which don't need .

原网站

版权声明
本文为[Silent moon cold moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140603467546.html