当前位置:网站首页>522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
2022-06-27 14:11:00 【Biqiliang】
522. The longest special sequence II【 Medium question 】【 A daily topic 】
Ideas :【 enumeration 】
Procedures are written in code comments , Refer to the official solution . But my execution time is 2ms~
Code :
class Solution {
public int findLUSlength(String[] strs) {
/** * For a string in the string array strs[i] Come on If a subsequence of it sub It's a special sequence , That is, it is not a subsequence of other strings in the array * So this string strs[i] It is also a special sequence * prove : Reduction to absurdity * hypothesis sub Is a special sequence , that sub It does not appear as a subsequence in any other string * So here you are sub Add any characters , It still doesn't appear in other strings as a subsequence * Because the topic requires finding the longest special sequence , So we just need to enumerate every string in the array , Filter out the longest string that matches the special sequence , That is, the longest special sequence we are looking for * So now the problem is reduced to two steps : * 1. Traverse each string , Determine whether it is a special sequence * 2. If the string meets the special sequence requirements , Update the longest special sequence length max, The function finally returns max * For the first 1 Step by step , Can be converted to , Determine whether the current string is a subsequence of other strings in the array except itself , yes It indicates that the current string is a special sequence , no Is not a special sequence */
int max = -1,n = strs.length;
// Double loop traversal string array , Screening strs[i] Whether it is a special sequence
for (int i = 0; i < n; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if (i == j){
continue;
}
String s1 = strs[i],s2 = strs[j];
int n1 = s1.length(),n2 = s2.length();
//s1 Length ratio of s2 Also long ,s1 It can't be s2 The subsequence , Don't go on judging
if (n1 > n2){
continue;
}else if (n1 == n2){
//s1 The length of s2 equal
// If s1 And s2 Unequal , be s1 No s2 The subsequence
// If s1 And s2 equal , be s1 yes s2 The subsequence , The location of the sign is flase, Inner layer for Loop exit
if (s1.equals(s2)){
flag = false;
break;
}else {
continue;
}
}
// Judge s1 Whether it is s2 The subsequence ( here n1 < n2)
int p1 = 0,p2 = 0;
while (p1 < n1 && p2 < n2){
// If p1 Character and p2 The characters are equal , be p1 And p2 Shift right
if (s1.charAt(p1) == s2.charAt(p2)){
p1++;
p2++;
}else {
// otherwise , Only will p2 Move right
p2++;
}
}
// If s1 End of normal traversal , explain s1 yes s2 The subsequence , The location of the sign is false, Inner layer for Loop exit
if (p1 == n1){
flag = false;
break;
}
}
// After screening ,strs[i] Passed the test , It's a special sequence , Update the length of the longest special sequence
if (flag){
max = Math.max(max,strs[i].length());
}
}
return max;
}
}
The finger of the sword Offer II 101. To divide into equal and subsets 【 Simple questions 】
Ideas :【 Dynamic programming 】
First order dp And second order dp, Write according to the answer ~, This is a dynamic programming problem. I really didn't expect .
Code :【 Second order dp】
class Solution {
public boolean canPartition(int[] nums) {
// It should be divided into two parts , Then the array length is at least 2,
int n = nums.length;
if (n < 2){
return false;
}
// It should be divided into two parts of equal sum , The sum of each part must be half of the total , So you should first find the sum of the arrays sum, In this way, the goal and
int sum = 0,max = 0;
for (int num : nums) {
max = Math.max(max,num);
sum += num;
}
// If sum Is odd , Then the array can not be divided into elements and equal parts
if(sum % 2 != 0){
return false;
}
int target = sum / 2;
//max > target, It is impossible to divide an array into elements and equal parts
if (max > target){
return false;
}
//max == target It must be divided into two parts ,max As part of , The rest of the elements are another part
if (max == target){
return true;
}
// Definition dp Array dp[i][j] Represents array subscript [0,i) Within the scope of , Select whether several elements can make elements and reach j Then we just need to find dp[n-1][target] Is it true that will do
boolean[][] dp = new boolean[n][target+1];
// The boundary conditions : j=0 when ,dp[i][0] = true ; i=0 when ,dp[0][nums[0]] = true.
dp[0][nums[0]] = true;
for (int i = 0; i < n; i++) {
dp[i][0] = true;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= target; j++) {
if (j >= nums[i]){
// No choice nums[i] Elements , be dp[i][j] Depending on dp[i-1][j]
// choice nums[i] Elements , be dp[i][j] Depending on dp[i-1][j-nums[i]]
dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]];
}else {
// because j < nums[i] It is impossible to choose nums[i] Elements
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n-1][target];
}
}
Code :【 First order dp】
class Solution {
public boolean canPartition(int[] nums) {
// It should be divided into two parts , Then the array length is at least 2,
int n = nums.length;
if (n < 2){
return false;
}
// It should be divided into two parts of equal sum , The sum of each part must be half of the total , So you should first find the sum of the arrays sum, In this way, the goal and
int sum = 0,max = 0;
for (int num : nums) {
max = Math.max(max,num);
sum += num;
}
// If sum Is odd , Then the array can not be divided into elements and equal parts
if(sum % 2 != 0){
return false;
}
int target = sum / 2;
//max > target, It is impossible to divide an array into elements and equal parts
if (max > target){
return false;
}
//max == target It must be divided into two parts ,max As part of , The rest of the elements are another part
if (max == target){
return true;
}
// Definition dp Array dp[j] Indicates that the current array is within the optional range , Select whether several elements can make elements and reach j, Then we find that when the optional range is the entire array ,dp[target] Is it true that will do
boolean[] dp = new boolean[target+1];
// The boundary conditions :j = 0 when , Must be true
dp[0] = true;
for (int num : nums) {
for (int j = target; j >= num; j--) {
/*if (j >= nums[i]){ // choice nums[i] Elements , be dp[j] Depending on dp[j-nums[i]] // No choice nums[i] Elements , be dp[j] Immobility dp[j] = dp[j] || dp[j-nums[i]]; }else { //j < nums[i] when , It is impossible to choose nums[i] therefore dp[j] Keep still dp[j] = dp[j]; } So the transfer equations are summarized as follows */
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
}
边栏推荐
- Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
- 美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
- CMOS level circuit analysis
- In the past, domestic mobile phones were arrogant in pricing and threatened that consumers would like to buy or not, but now they have plummeted by 2000 for sale
- 重读经典:《The Craft of Research(1)》
- Awk concise tutorial
- 类模板中可变参的逐步展开
- Getting to know cloud native security for the first time: the best guarantee in the cloud Era
- Leetcode 724. 寻找数组的中心下标(可以,一次过)
- Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
猜你喜欢

Step by step expansion of variable parameters in class templates

高效率取幂运算

做一篇人人能搞懂的ThreadLocal(源码)

深入理解位运算

Number of printouts (solved by recursive method)

基于SSM的Web网页聊天室系统

The second part of the travel notes of C (Part II) structural thinking: Zen is stable; all four advocate structure

国产数据库乱象

美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越

Redis持久化
随机推荐
522. 最长特殊序列 II / 剑指 Offer II 101. 分割等和子集
External memory
[安洵杯 2019]Attack
清华&商汤&上海AI&CUHK提出Siamese Image Modeling,兼具linear probing和密集预测性能!...
R language objects are stored in JSON
Li Kou's 81st biweekly match
类模板中可变参的逐步展开
Integration of entry-level SSM framework based on XML configuration file
为什么 Oracle 云客户必须在Oracle Cloud 季度更新发布后自行测试?
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
Completely solve the problem of Chinese garbled code in Web Engineering at one time
enable_ if
Overseas warehouse knowledge popularization
SFINAE
Deep understanding of bit operations
In the past, domestic mobile phones were arrogant in pricing and threatened that consumers would like to buy or not, but now they have plummeted by 2000 for sale
Summary of basic usage of command line editor sed
Bidding announcement: Oracle all-in-one machine software and hardware maintenance project of Shanghai R & D Public Service Platform Management Center
SFINAE
[WUSTCTF2020]girlfriend