2022.03.24(LC_28_实现 strStr())

该博客主要探讨了两种暴力匹配算法的实现方式,用于在一个字符串中查找另一个子串首次出现的位置。这两种方法都遍历了主字符串,并检查每个起始位置是否能完全匹配目标子串。当找到匹配时返回起始索引,否则返回-1。
摘要由CSDN通过智能技术生成

方法:暴力匹配

class Solution {
    public int strStr(String haystack, String needle) {
        if (needle == null || needle.length() == 0) {
            return 0;
        }
        int hLen = haystack.length(), nLen = needle.length();
        for (int index = 0; index <= hLen - nLen; index++) {
            if (haystack.charAt(index) != needle.charAt(0)) {
                continue;
            }
            int ans = index;
            int i = index;
            int j = 0;
            while (i < hLen && j < nLen && haystack.charAt(i) == needle.charAt(j)) {
                i++;
                j++;
            }
            if (j == needle.length()) {
                return ans;
            }
        }
        return -1;
    }
}
class Solution {
    public int strStr(String haystack, String needle) {
        int m = haystack.length(), n = needle.length();
        for (int i = 0; i <= m - n; i++) {
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return i;
            }
        }
        return -1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "main.h"#include "stdio.h"#include "string.h"UART_HandleTypeDef huart1;GPIO_InitTypeDef GPIO_InitStruct;void LED_Control(uint8_t state) { HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, state);}void USART1_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); }}void MX_GPIO_Init(void) { GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);}void AT_SendCommand(char *cmd, char *response) { uint8_t buffer_rx[100]; uint8_t buffer_tx[100]; memset(buffer_rx, 0, sizeof(buffer_rx)); memset(buffer_tx, 0, sizeof(buffer_tx)); sprintf((char *)buffer_tx, "%s\r\n", cmd); HAL_UART_Transmit(&huart1, buffer_tx, strlen((char *)buffer_tx), 1000); HAL_UART_Receive(&huart1, buffer_rx, sizeof(buffer_rx), 5000); if (strstr((char *)buffer_rx, response) == NULL) { printf("AT Command Failed: %s", response); }}int main(void) { HAL_Init(); USART1_Init(); MX_GPIO_Init(); char buffer_rx[100]; memset(buffer_rx, 0, sizeof(buffer_rx)); AT_SendCommand("AT", "OK"); AT_SendCommand("AT+CWMODE=1", "OK"); AT_SendCommand("AT+CWJAP=\"ssid\",\"password\"", "OK"); while (1) { AT_SendCommand("AT+CIPSTART=\"TCP\",\"server_ip\",80", "OK"); AT_SendCommand("AT+CIPSEND=4", ">"); AT_SendCommand("test", "SEND OK"); HAL_Delay(1000); }}
06-03
/* * 这是一个使用STM32和ESP8266模块实现的简单的TCP连接的程序 * 实现的功能是通过ESP8266模块连接到指定的Wi-Fi网络,并连接到指定的IP地址和端口号的TCP服务器 * 然后发送一个字符串"test",并等待1秒,然后再次发送 * * 包含的头文件: * main.h - 包含了所需的头文件和宏定义 * stdio.h - 包含了printf函数 * string.h - 包含了字符串处理函数 * * 使用的外设: * USART1 - 与ESP8266模块通信使用 * GPIO - 控制LED灯 * * 函数列表: * LED_Control - 控制LED灯状态的函数 * USART1_Init - 初始化USART1外设的函数 * MX_GPIO_Init - 初始化GPIO外设的函数 * AT_SendCommand - 向ESP8266模块发送AT指令并等待响应的函数 * main - 主函数 */ #include "main.h" #include "stdio.h" #include "string.h" UART_HandleTypeDef huart1; GPIO_InitTypeDef GPIO_InitStruct; // 控制LED灯状态的函数 void LED_Control(uint8_t state) { HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, state); } // 初始化USART1外设的函数 void USART1_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } // 初始化GPIO外设的函数 void MX_GPIO_Init(void) { GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); } // 向ESP8266模块发送AT指令并等待响应的函数 void AT_SendCommand(char *cmd, char *response) { uint8_t buffer_rx[100]; uint8_t buffer_tx[100]; memset(buffer_rx, 0, sizeof(buffer_rx)); memset(buffer_tx, 0, sizeof(buffer_tx)); // 将AT指令写入发送缓冲区 sprintf((char *)buffer_tx, "%s\r\n", cmd); HAL_UART_Transmit(&huart1, buffer_tx, strlen((char *)buffer_tx), 1000); // 等待接收响应 HAL_UART_Receive(&huart1, buffer_rx, sizeof(buffer_rx), 5000); // 判断响应中是否包含预期的字符串 if (strstr((char *)buffer_rx, response) == NULL) { printf("AT Command Failed: %s", response); } } int main(void) { HAL_Init(); USART1_Init(); MX_GPIO_Init(); char buffer_rx[100]; memset(buffer_rx, 0, sizeof(buffer_rx)); // 发送AT指令连接到Wi-Fi网络 AT_SendCommand("AT", "OK"); AT_SendCommand("AT+CWMODE=1", "OK"); AT_SendCommand("AT+CWJAP=\"ssid\",\"password\"", "OK"); while (1) { // 发送AT指令连接到指定IP地址和端口号的TCP服务器 AT_SendCommand("AT+CIPSTART=\"TCP\",\"server_ip\",80", "OK"); // 发送AT指令设置要发送的字节数 AT_SendCommand("AT+CIPSEND=4", ">"); // 发送要发送的字符串 AT_SendCommand("test", "SEND OK"); // 延时1秒 HAL_Delay(1000); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值