当前位置:网站首页>Daily question brushing record (12)
Daily question brushing record (12)
2022-07-04 05:01:00 【Unique Hami melon】
List of articles
- The first question is : The finger of the sword Offer II 012. The sum of the left and right subarrays is equal
- The second question is : The finger of the sword Offer II 014. An anamorphic word in a string
- Third question : The finger of the sword Offer II 016. The longest substring without duplicate characters
- Fourth question : The finger of the sword Offer II 018. Valid palindromes
- Fifth question : The finger of the sword Offer II 019. Delete at most one character to get a palindrome
- Sixth question : The finger of the sword Offer II 020. Number of palindrome substrings
The first question is : The finger of the sword Offer II 012. The sum of the left and right subarrays is equal
LeetCode: The finger of the sword Offer II 012. The sum of the left and right subarrays is equal
describe :
Give you an array of integers nums , Please calculate the of the array Center subscript .
Array Center subscript Is a subscript of the array , The sum of all elements on the left is equal to the sum of all elements on the right .
If the central subscript is at the leftmost end of the array , Then the sum of the numbers on the left is regarded as 0 , Because there is no element to the left of the subscript . This also applies to the fact that the central subscript is at the rightmost end of the array .
If the array has multiple central subscripts , Should return to Closest to the left The one of . If the array does not have a central subscript , return -1 .
Their thinking :
- The sum of all is directly recorded here , to
rightTotal
- Traversal array .
- Give Way leftTotal Record the sum of all elements on the left ,
rightTotal
Record the sum of all elements on the right .- If at present
leftTotal
andrightTotal
The values are equal . Returns the current subscript .
Code implementation :
class Solution {
public int pivotIndex(int[] nums) {
int rightTotal = 0;
int leftTotal = 0;
for(int num : nums) {
rightTotal += num;
}
for(int i = 0; i < nums.length; i++) {
// Notice that the right side is subtracted first Judge again Then add .
rightTotal -= nums[i];
if (leftTotal == rightTotal) return i;
leftTotal += nums[i];
}
return -1;
}
}
The second question is : The finger of the sword Offer II 014. An anamorphic word in a string
LeetCode: The finger of the sword Offer II 014. An anamorphic word in a string
describe :
Given two strings s1 and s2, Write a function to judge s2 Does it include s1 An anamorphic word of .
let me put it another way , One of the permutations of the first string is the... Of the second string Substring .
Their thinking :
- Use sliding windows to solve .
- First use an array record s1 in Number of string occurrences
- Then change all characters that do not appear into -1
- Establish a range of [left,right] Sliding window of .
- If at present right Subscript elements exist in the array
- If the current number is greater than 0, Then the array subscript corresponding to this element is subtracted 1, Make the window bigger (right++).
- If the current number is equal to 0, If at present left and right Subscript elements are different , You need to make the left window smaller (left++). If it's the same , Just slide the window to the right (left++,right++)
- If at present right The subscript element does not exist in the array . If at present left and right Not in the same subscript , It means that the window has been changed , And the current element does not exist , You need to reinitialize the window ,
Code implementation :
class Solution {
public boolean checkInclusion(String s1, String s2) {
int[] arr = new int[26];
for(char ch : s1.toCharArray()) {
arr[ch-'a']++;
}
for (int i = 0; i < 26; i++) {
if (arr[i] == 0) arr[i] = -1;
}
int left = 0;
int right = 0;
int total = s1.length();
while (total > 0 && right < s2.length()) {
int tmp = arr[s2.charAt(right)-'a'];
if (tmp == -1) {
while (left < right) {
arr[s2.charAt(left) - 'a']++;
total++;
left++;
}
left = right + 1;
right = left;
} else {
if (tmp > 0) {
total--;
arr[s2.charAt(right)-'a']--;
right++;
}else {
while(s2.charAt(left) != s2.charAt(right)) {
total++;
arr[s2.charAt(left) - 'a']++;
left++;
}
left++;
right++;
}
}
}
return total == 0;
}
}
Third question : The finger of the sword Offer II 016. The longest substring without duplicate characters
LeetCode: The finger of the sword Offer II 016. The longest substring without duplicate characters
describe :
Given a string s , Please find out that there are no duplicate characters in it Longest continuous substring The length of .
Their thinking :
- Here is to use sliding window to solve problems .
- The scope is [left,right] Sliding window of . Add the string list Record
- If at present right The subscript element exists , Just delete list The first element , Give Way left++, until right Subscript element in list Does not exist in the .
- take right Subscript elements are added list in , Record the size of the current sliding window (right-left+1)
- End of traversal , Returns the maximum sliding window size .
Code implementation :
class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0;
int right = 0;
int ans = 0;
List<Character> list = new ArrayList<>();
while (right < s.length()) {
while(left < right && list.contains(s.charAt(right))) {
list.remove(0);
left++;
}
list.add(s.charAt(right));
ans = Math.max(ans,right-left+1);
right++;
}
return ans;
}
}
Fourth question : The finger of the sword Offer II 018. Valid palindromes
LeetCode: The finger of the sword Offer II 018. Valid palindromes
describe :
Given a string s
, verification s
Whether it is Palindrome string , Only consider Letters and numbers character , The case of letters can be ignored .
In this question , Define an empty string as a valid Palindrome string .
Their thinking :
- Traversal string , As long as the string is numbers and characters, it will become a new string .
- Use double pointers to traverse the string .
- If
left
Subscripts andright
The contents of subscript words are the same , Justleft++. right--;
- If it's not the same , Go straight back to
false
;- Return after traversal
true
;
Code implementation :
class Solution {
public boolean isPalindrome(String s) {
String ret = "1234567890abcdefghijklmnopqlstuvwxyz";
StringBuilder sb = new StringBuilder();
s = s.toLowerCase();
for (int i = 0; i < s.length(); i++) {
if(ret.contains(s.charAt(i)+"")){
sb.append(s.charAt(i));
}
}
int left = 0;
int right = sb.length()-1;
while(left <= right) {
if(sb.charAt(left) == sb.charAt(right)) {
left++;
right--;
}else{
return false;
}
}
return true;
}
}
Fifth question : The finger of the sword Offer II 019. Delete at most one character to get a palindrome
LeetCode: The finger of the sword Offer II 019. Delete at most one character to get a palindrome
describe :
Given a non empty string s, Please judge if most If you delete a character from the string, you can get a palindrome string .
Their thinking :
- First of all, judge , Whether the current palindrome string
- If so, go back true;
- If not, judge the current [left,right-1] or [left+1,right] Is it a palindrome string . As long as there is one is true;
Code implementation :
class Solution {
public boolean validPalindrome(String s) {
int left = 0;
int right = s.length()-1;
while(left <= right) {
if(s.charAt(left) == s.charAt(right)){
left++;
right--;
}else{
return isHui(s,left,right-1) || isHui(s,left+1,right);
}
}
return true;
}
public boolean isHui(String s, int left, int right) {
while(left <= right) {
if(s.charAt(left) == s.charAt(right)) {
left++;
right--;
}else{
return false;
}
}
return true;
}
}
Sixth question : The finger of the sword Offer II 020. Number of palindrome substrings
LeetCode: The finger of the sword Offer II 020. Number of palindrome substrings
describe :
Given a string s , Please calculate how many palindrome substrings there are in this string .
A substring with different start or end positions , Even if it's made up of the same characters , It's also seen as a different substring .
Their thinking :
- Here we use dynamic programming to solve problems .
- state F(i,j) : Express i~j Is it a palindrome string
- State transition equation : At present
j == i when , F(i,j) = true
; Whenj - i == 1 when ,F(i,j)=s.charAt(i) == s.charAt(j)
; Whenj-i > 1 when , F(i,j) = s.charAt(i) == s.charAt(j) && dp[i+1][j-1]
(dp[i+1][j-1] , Namely [i+1,j-1] Is it a palindrome string
)- The initial state : A single character is true;
- Return results :
dp[i][j] by true The number of
Code implementation :
class Solution {
public int countSubstrings(String s) {
boolean[][] dp = new boolean[s.length()][s.length()];
int count = 0;
for(int i = s.length()-1; i >= 0; i--) {
for(int j = s.length()-1; j>=i; j--){
if(j == i) dp[i][j] = true;
else if(j - i == 1) {
dp[i][j] = s.charAt(i) == s.charAt(j);
}else{
dp[i][j] = s.charAt(i) == s.charAt(j) && dp[i+1][j-1];
}
if(dp[i][j]) count++;
}
}
return count;
}
}
边栏推荐
- cmake
- Secondary vocational group network security - memory Forensics
- RPC - grpc simple demo - learn / practice
- How to build your own knowledge engine? Community open application
- 记几个智能手表相关芯片 蓝牙芯片 低功耗
- LeetCode136+128+152+148
- 简单g++和gdb调试
- Exercise bubble sort
- Zhongke panyun-d module analysis and scoring standard
- 关于solidworks standard无法获得许可 8544问题的总结
猜你喜欢
随机推荐
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
Beipiao programmer, 20K monthly salary, 15W a year, normal?
Utiliser des unités de mesure dans votre code pour une vie meilleure
【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it
[matlab] general function of communication signal modulation inverse Fourier transform
Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises
[matlab] matlab simulation - low pass Gaussian white noise
简单g++和gdb调试
Capturing and sorting out external Fiddler -- Conversation bar and filter
Customize a pager needed in your project
远程桌面客户端 RDP
LeetCode136+128+152+148
NTFS 安全权限
EVM proof in appliedzkp zkevm (11)
Share some of my telecommuting experience
【MATLAB】MATLAB 仿真数字带通传输系统 — QPSK 和 OQPSK 系统
全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
附件二:攻防演练保密协议.docx