当前位置:网站首页>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
numsAnd a valueval, You need In situ Remove all values equal tovalThe 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, findsThe 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 .
边栏推荐
- Leetcode backtracking method
- Design specification for mobile folding screen
- [learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
- 阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
- How to answer when you encounter a jet on CSDN?
- Chapter 6 relational database theory
- ollvm编译出现的问题纪录
- 时间很快,请多做有意义的事情
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- SQL三种连接:内连接、外连接、交叉连接
猜你喜欢

【高德地图POI踩坑】AMap.PlaceSearch无法使用

Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations

博弈论 AcWing 891. Nim游戏

ollvm编译出现的问题纪录

Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
![LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation](/img/1f/30407bce35c324cc90f139b6f09fd1.jpg)
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation

Redis-01.初识Redis

Find the combination number acwing 888 Find the combination number IV

Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software

2021apmcm post game Summary - edge detection
随机推荐
博弈论 AcWing 891. Nim游戏
how to understand the “model independent.“
Bit of MySQL_ OR、BIT_ Count function
2022-5-the fourth week daily
高斯消元 AcWing 884. 高斯消元解异或線性方程組
【LeetCode】Easy | 20. Valid parentheses
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
博弈论 AcWing 894. 拆分-Nim游戏
Record the process of configuring nccl and horovod in these two days (original)
容斥原理 AcWing 890. 能被整除的数
ollvm编译出现的问题纪录
Huawei bracelet, how to add medicine reminder?
Find the combination number acwing 887 Find combination number III
在新线程中使用Handler
Find the combination number acwing 888 Find the combination number IV
[QT] QT multithreading development qthread
P3265 [jloi2015] equipment purchase
5. Oracle tablespace
La redirection de l'applet Wechat ne déclenche pas onload
TypeScript入门