LeeCode Practice Journal | Day43_DP10

300. 最长递增子序列

题目:300. 最长递增子序列 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)

solution
public class Solution {
    public int LengthOfLIS(int[] nums) {
        int result = 1;
        int n = nums.Length;
        int[] dp  = new int[n];
        Array.Fill(dp, 1);

        for(int i = 1; i < n; i ++)
        {
            for(int j = 0; j < i; j ++)
            {
                if(nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + 1);
            }
            result = dp[i] > result ? dp[i] : result;
        }
        foreach(int num in dp)
        {
            Console.WriteLine(num);
        }
        return result;
    }
}
summary

注意:

1、数组元素要初始化为1

2、返回值不是dp[i],是dp数组中的最大值

674. 最长连续递增序列

题目:674. 最长连续递增序列 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
上题的变体,与nums[i]前每一个连续的元素比较变为前一个元素比较

solution
public class Solution {
    public int FindLengthOfLCIS(int[] nums) {
        int n = nums.Length;
        int[] dp = new int[n];
        Array.Fill(dp, 1);
        int result = 1;

        for(int i = 1; i < n; i ++)
        {
            if(nums[i] > nums[i-1]) dp[i] = dp[i-1] + 1;
            result = Math.Max(result, dp[i]);
        }

        return result;
    }
}
summary

718. 最长重复子数组

题目:718. 最长重复子数组 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)

solution
public class Solution {
    public int FindLength(int[] nums1, int[] nums2) {
        int n = nums1.Length;
        int m = nums2.Length;
        int[,] dp = new int[n,m];
        int result = 0;

        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < m; j ++)
            {
                if(i == 0 || j == 0) dp[i,j] = nums1[i] == nums2[j] ? 1 : 0;
                else dp[i,j] = nums1[i] == nums2[j] ? dp[i-1, j-1] + 1: 0;
                result = Math.Max(result, dp[i,j]);
            }
        } 
        return result;
    }
}

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,首先需要在代码中引入SE注意力模块所需的库和函数: ```python import tensorflow as tf def se_block(input_feature, ratio=8): """Squeeze-and-excitation block""" channel_axis = 1 if tf.keras.backend.image_data_format() == "channels_first" else -1 channel = input_feature.shape[channel_axis] se_feature = tf.keras.layers.GlobalAveragePooling2D()(input_feature) se_feature = tf.keras.layers.Reshape((1, 1, channel))(se_feature) se_feature = tf.keras.layers.Dense(channel // ratio, activation='relu', kernel_initializer='he_normal', use_bias=True)(se_feature) se_feature = tf.keras.layers.Dense(channel, activation='sigmoid', kernel_initializer='he_normal', use_bias=True)(se_feature) if tf.keras.backend.image_data_format() == 'channels_first': se_feature = tf.keras.layers.Permute((3, 1, 2))(se_feature) se_tensor = tf.keras.layers.multiply([input_feature, se_feature]) return se_tensor ``` 然后在代码中的残差块中添加SE注意力模块: ```python def residual_block(inputs, filters, strides=(1, 1), use_se=True): shortcut = inputs # first block x = tf.keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=strides, padding='same', kernel_initializer='he_normal')(inputs) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation('relu')(x) # second block x = tf.keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=(1, 1), padding='same', kernel_initializer='he_normal')(x) x = tf.keras.layers.BatchNormalization()(x) # SE block if use_se: x = se_block(x) # shortcut connection if strides != (1, 1) or inputs.shape[-1] != filters: shortcut = tf.keras.layers.Conv2D(filters=filters, kernel_size=(1, 1), strides=strides, padding='same', kernel_initializer='he_normal')(inputs) shortcut = tf.keras.layers.BatchNormalization()(shortcut) x = tf.keras.layers.Add()([x, shortcut]) x = tf.keras.layers.Activation('relu')(x) return x ``` 这样就在代码中添加了SE注意力模块。注意,这里的实现方式是在残差块中嵌入SE注意力模块,而不是在整个模型中添加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值