当前位置:网站首页>Daily question brushing record (26)
Daily question brushing record (26)
2022-07-19 11:04:00 【Unique Hami melon】
List of articles
The first question is : 290. Rules of words
LeetCode: 290. Rules of words
describe :
Give a rule pattern And a string s , Judge s Follow the same rule .
there follow Finger perfect match , for example , pattern Each letter and string in s There is a two-way connection between each non empty word in .
Their thinking :
- Here we use hash table to solve problems .
- First of all, will
sString into string array .- If At this time, the length of the string array and
patternThe length is different , Go straight back to false- If the current hash table does not exist
pattern.charAt(i)The elements of , First judge , Corresponding value Whether the value repeats . If it repeats , Just go back to false. There is no repetition Add directly to In the hash table .- If... Exists in the current hash table
pattern.charAt(i)The elements of , Just check the correspondingvalueWhether the value is the same as the currentstr[i]Whether it is equal or not , Equality means that the correspondence is regular . Otherwise return to false
Code implementation :
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] str = s.split(" ");
if(pattern.length() != str.length) return false;
Map<Character,String> map = new HashMap<>();
for(int i = 0;i < str.length ;i++){
if(!map.containsKey(pattern.charAt(i))) {
if(map.containsValue(str[i])){
return false;
}else{
map.put(pattern.charAt(i),str[i]);
}
}
}
for(int i = 0; i < str.length; i++) {
if(!map.get(pattern.charAt(i)).equals(str[i])){
return false;
}
}
return true;
}
}
The second question is : 402. move away K Digit number
LeetCode: 402. move away K Digit number
describe :
Give you a nonnegative integer expressed as a string num And an integer k , Remove the k Digit number , Keep the remaining numbers to a minimum . Please return the smallest number as a string .
Their thinking :
- Here, first of all, the current
numString to judge , If k Equal to the length of the string return "0"- Use an auxiliary stack to solve problems .
- If the current corresponding number , It should be larger than the top element , Just go straight out of the stack .
- otherwise , Check whether the current number is 0, If 0, And If the stack is not empty, enter it ,
- The current array is not 0 Just go straight to the stack .
- After traversing once see k Is it empty , That is, whether the number of times out of the stack is k Time .
- here k Not empty Out of the stack again .
- If the stack is empty , Just go back to “0”
- String splicing is performed when the stack is not empty .
Code implementation :
class Solution {
public String removeKdigits(String num, int k) {
if(k == num.length()) return "0";
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < num.length(); i++) {
int tmp = num.charAt(i) -'0';
while(k != 0 && !stack.isEmpty() && stack.peek() > tmp) {
stack.pop();
k--;
}
if(tmp != 0 || !stack.isEmpty()) {
stack.push(tmp);
}
}
while(k != 0 && !stack.isEmpty()) {
stack.pop();
k--;
}
if(stack.isEmpty()) return "0";
StringBuilder res = new StringBuilder();
while(!stack.isEmpty()) {
res.append(stack.pop());
}
return res.reverse().toString();
}
}
Third question : 409. Longest palindrome
LeetCode: 409. Longest palindrome
describe :
Given a string of uppercase and lowercase letters s , return Constructed from these letters The longest palindrome string .
In the process of construction , Please note that Case sensitive . such as “Aa” Can't be treated as a palindrome string .
Their thinking :
- First, use a hash table to record the number of occurrences of each character .
- If a character appears an even number of times , Directly add the number of occurrences of this character to the result set .
- If a character appears an odd number of times , To add this number -1 Time . for example
aaa, There is 3 Time , But you can use his occasional times , 3-1=2.- End of traversal , If there is an odd number , then +1, For example, you recorded
aaa, Only occasionally recorded , After that , You can add it againa
Code implementation :
class Solution {
public int longestPalindrome(String s) {
Map<Character,Integer> map = new HashMap<>();
for(char ch : s.toCharArray()) {
map.put(ch,map.getOrDefault(ch,0)+1);
}
int count = 0;
int ret = 0;
for(Map.Entry<Character,Integer> entry : map.entrySet()) {
if(entry.getValue() % 2 == 0) {
count += entry.getValue();
}else{
count += entry.getValue()-1;
ret = 1;
}
}
return count + ret;
}
}
Fourth question : 459. Repeated substrings
LeetCode: 459. Repeated substrings
describe :
Given a non empty string s , Check whether it can be formed by repeating one of its substrings multiple times .

Their thinking :
- Whether it can be refactored here can be understood as , Move the left character to the right , Whether it can be changed into the original character .
- for example
abab, The first move isbaba, The second move isabab, The third move isbaba.- Here you can make the string
sSplice twice , Then remove the head and tail , And look for Whether there iss
Code implementation :
class Solution {
public boolean repeatedSubstringPattern(String s) {
String str = s + s;
return str.substring(1,str.length()-1).contains(s);
}
}
Fifth question : 516. The longest palindrome subsequence
LeetCode: 516. The longest palindrome subsequence
describe :
Give you a string s , Find the longest palindrome subsequence , And return the length of the sequence .
Subsequence is defined as : Without changing the order of the remaining characters , A sequence formed by deleting some characters or not deleting any characters .

Their thinking :
- Here we use dynamic programming to solve problems .
- state F(i,j) : Express i ~ j The length of the longest palindrome subsequence .
- State transition equation :
- F(i,j) = F(i+1)(j-1) + 2 (s[i] = s[j])
- F(i,j) = Math.max(F(i+1)(j),F(i)(j-1)) (s[i] != s[j])
- The initial state : F(i,i) = 1
- Return results : F(0,len-1)
Because some characters can be deleted , All if s[i] != s[j] When , You can count one more character .
Code implementation :
class Solution {
public int longestPalindromeSubseq(String s) {
int[][] dp = new int[s.length()][s.length()];
for(int i = 0; i < s.length(); i++){
dp[i][i] = 1;
}
for(int i = s.length()-2; i >= 0; i--) {
for(int j = i+1; j < s.length(); j++){
if(s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i+1][j-1] + 2;
}else{
dp[i][j] = Math.max(dp[i+1][j],dp[i][j-1]);
}
}
}
return dp[0][s.length()-1];
}
}
Sixth question : 520. Detect capital letters
LeetCode: 520. Detect capital letters
describe :
We define , When , The capitalization of words is correct :
- All the letters are uppercase , such as “USA” .
- All the letters in a word are not capitalized , such as “leetcode” .
- If a word contains more than one letter , Only the first letter is capitalized , such as “Google” .
Give you a string word . If capitalized correctly , return true ; otherwise , return false .
Their thinking :
- Use
upCountRecord the number of times capital letters appear .- Use
lowCountRecord the number of lowercase letters .- Traversal string ,
- If
upCount == word,length(), Then the conditions are met 1.- If
lowCount == word.length(), Then the conditions are met 2.- If
upCount == 1 && word.charAt(0) As the capital, Then the conditions are met 3.- Not satisfied with this 3 individual Just go back to false
Code implementation :
class Solution {
public boolean detectCapitalUse(String word) {
int upCount = 0;
int lowCount = 0;
for(char ch : word.toCharArray()){
if(ch>='a' && ch<='z') {
lowCount++;
}else{
upCount++;
}
}
if(lowCount == word.length()) {
return true;
}
if(upCount == word.length()) {
return true;
}
if(upCount == 1 && word.charAt(0) >= 'A' && word.charAt(0) <= 'Z'){
return true;
}
return false;
}
}
边栏推荐
- Input number pure digital input limit length limit maximum value
- OpenCV编程:OpenCV3.X训练自己的分类器
- 2022/7/14
- Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
- LeetCode 558. 四叉树交集
- ROS duplicate name
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- If you use mybatics to access Damon database, is it exactly the same? Because the SQL syntax has not changed. Right?
- 最大半连通子图(tarjan缩点+拓扑排序+dp最长链)
- Win10 install Apache Jena 3.17
猜你喜欢

Pytoch and weight decay (L2 norm)

Google Earth Engine——Hansen Global Forest Change v1.8 (2000-2020) 森林覆盖度和森林损失量数据集

Pytoch framework learning record 1 cifar-10 classification

Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想

每日刷题记录 (二十六)

发明闪存能赚多少钱?这是一个日本的狗血故事

Leetcode ugly number problem solution

如何在 RHEL 9 中更改和重置忘记的root密码

Evaluation method of machine learning model

Documents required for military product development process - advanced version
随机推荐
LeetCode 2335. Minimum total time required to fill the cup
Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比
Win10 install Apache Jena 3.17
Huawei machine test: Message decompression
Win10 start key click no response
Introduction to the universal theme system of SAP appgyver
LeetCode 2315. Statistical asterisk (string)
Evaluation method of machine learning model
leetcode-08
Pytoch framework learning record 1 cifar-10 classification
Model comparison of material inventory management between sap ECC and s4hana material
2022/7/16
如何在 RHEL 9 中更改和重置忘记的root密码
LeetCode 2319. Judge whether the matrix is an X matrix
军品研制过程所需文件-进阶版
37. Flex layout
Data Guard Broker的概念和Data Guard Broker的配置过程
过拟合与欠拟合
[LeetCode周赛复盘] 第 302 场周赛20220717