当前位置:网站首页>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
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 characterss
, 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
The difficulty is simple 1137 Switch to English to receive dynamic feedback
Give you an array
nums
And a valueval
, You need In situ Remove all values equal toval
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
Medium difficulty 4545 Switch to English to receive dynamic feedback
Give you a string
s
, finds
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 .
边栏推荐
- [moviepy] unable to find a solution for exe
- P2575 master fight
- 博弈论 AcWing 892. 台阶-Nim游戏
- Idea debug failed
- Applicable to Net free barcode API [off] - free barcode API for NET [closed]
- Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
- Redis-02.Redis命令
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- [Gaode map POI stepping pit] amap Placesearch cannot be used
- FFmpeg build下载(包含old version)
猜你喜欢
Game theory acwing 892 Steps Nim game
Find the combination number acwing 887 Find combination number III
Interval problem acwing 906 Interval grouping
What is socket? Basic introduction to socket
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Speedtree01 generator properties
4.Oracle-重做日志文件管理
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
博弈论 AcWing 893. 集合-Nim游戏
随机推荐
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
Leetcode dynamic programming
Redis-01.初识Redis
5. Oracle TABLESPACE
如何正确在CSDN问答进行提问
Stack acwing 3302 Expression evaluation
5.Oracle-錶空間
TypeScript入门
4. Oracle redo log file management
SQL三种连接:内连接、外连接、交叉连接
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
Bit of MySQL_ OR、BIT_ Count function
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
5. Oracle tablespace
3.Oracle-控制文件的管理
[BMZCTF-pwn] ectf-2014 seddit
Redis-02.Redis命令
Huawei bracelet, how to add medicine reminder?
MPLS experiment
Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC