笔面试编程题总结

8/6诺瓦星云

修改程序

c 复制代码
void point(int *p){*p = p[2];};
int main() {
    int c[] ={1,2,3,4,5},*p = c;
    point(p+1);
    for(;p <c+5;)
    {
        printf("%d",*p++);
    }
    return 0;
}

1、分隔字符串 strtok

c 复制代码
//c语言
#include <stdio.h>
#include <string.h>

// 函数声明
char* findLongestWord(char* str);

int main() {
    char* str = "This is a test string";
    char* longestWord = findLongestWord(str);
    if (longestWord != NULL) {
        printf("The longest word is: %s\n", longestWord);
    } else {
        printf("No words found in the string.\n");
    }
    return 0;
}

// 函数定义
char* findLongestWord(char* str) {
    if (str == NULL) return NULL; // 检查输入字符串是否为空

      //分隔
      char *token;
      char *tokens[100]; // 假设最多分割出100个子字符串
      int i = 0;
      char delimiters[] = " "; // 定义分隔符

      // 复制输入字符串,因为strtok会修改原字符串
      char *strCopy = strdup(str);

      // 用strtok分割字符串,要分割的字符串,第一次调用时传入需要分割的字符串,之后传入 NULL
      token = strtok(strCopy, delimiters);
      while (token != NULL) {
          tokens[i++] = token; // 保存分割后的子字符串
          token = strtok(NULL, delimiters); // 继续获取下一个子字符串
      }

      // 打印分割后的结果
      int max = -1;
      char* longestWord = NULL; // 指向最长单词的指针
      for (int j = 0; j < i; j++) {
          //printf("%s\n", tokens[j]);
          int len_tmp = strlen(tokens[j]);
          if(len_tmp>max){
              max = len_tmp;
              longestWord = tokens[j];
          }
      }

      // 释放复制的字符串
      free(strCopy);

    return longestWord;
}

2、后缀法解 最长无重复子串

c 复制代码
//C 最长无重复子串
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//得到字符串最长的无重复的前缀长度
int longestlen(char * p)
{
    int hash[256];
    int len = 0;
    memset(hash,0,sizeof(hash));
    while (*p && !hash[*p])
    {
        hash[*p] = 1;
        ++ len;
        ++ p;
    }
    return len;
}

//使用后缀数组解法
int max_unique_substring4(char * str)
{
    int maxlen = -1;
    int begin = 0;
    char *a[99999];
    int n = 0;
    while(*str != '\0')
    {
        a[n++] = str++;
    }
    for (int i=0; i<n; i++)
    {
        int temlen = longestlen(a[i]);
        if (temlen > maxlen)
        {
            maxlen = temlen;
            begin = i;
        }
    }
    printf("%.*s\n", maxlen, a[begin]);
    return maxlen;
}
int main() {
    char *test_str = "abcabcbb";
    printf("Length of the longest substring without repeating characters: %s\n", max_unique_substring4(test_str));
    return 0;
}

3、双向链表

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

// 定义链表节点结构体
typedef struct list_node_t {
    struct list_node_t *p_before;  // 上一级节点
    struct list_node_t *p_next;    // 下一级节点
    void *p_data;                  // 存储数据节点
    uint32_t data_size;            // 存储数据大小
} list_node_t;

// 插入数据到链表尾部
int insert_data_to_list(void *p_list_head, const void *data, uint32_t data_size) {
    if (!p_list_head || !data) {
        return -2; // 传入指针为空错误
    }
    
    // 创建新节点
    list_node_t *new_node = (list_node_t *)malloc(sizeof(list_node_t));
    if (!new_node) {
        return -1; // 内存申请错误
    }
    
    new_node->p_data = malloc(data_size);
    if (!new_node->p_data) {
        free(new_node);
        return -1; // 内存申请错误
    }
    
    memcpy(new_node->p_data, data, data_size);
    new_node->data_size = data_size;
    new_node->p_before = NULL;
    new_node->p_next = NULL;
    
    // 找到链表尾部
    list_node_t *current = (list_node_t *)p_list_head;
    while (current->p_next) {
        current = current->p_next;
    }
    
    // 插入新节点到尾部
    current->p_next = new_node;
    new_node->p_before = current;
    
    return 0; // 正确
}

// 从链表中删除节点并释放空间
int delete_list_node(void *p_node) {
    if (!p_node) {
        return -2; // 传入指针为空错误
    }

    list_node_t *node = (list_node_t *)p_node;

    // 重新连接链表
    if (node->p_before) {
        node->p_before->p_next = node->p_next;
    }
    if (node->p_next) {
        node->p_next->p_before = node->p_before;
    }

    // 释放节点数据和节点本身
    free(node->p_data);
    free(node);
    return 0; // 正确
}

// 示例用法
int main() {
    list_node_t head = {NULL, NULL, NULL, 0}; // 初始化链表头部
    int data1 = 100;
    int data2 = 20;
    insert_data_to_list(&head, &data1, sizeof(data1));
    insert_data_to_list(&head, &data2, sizeof(data2));
    // 遍历链表并打印数据
    list_node_t *current = head.p_next;
    while (current) {
        printf("%d\n", *(int *)current->p_data);
        current = current->p_next;
    }
    // 删除所有节点
    current = head.p_next;
    while (current) {
        list_node_t *next = current->p_next;
        delete_list_node(current);
        current = next;
    }
    return 0;
}

4、背包问题

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

// 定义链表节点结构体
typedef struct list_node_t {
    struct list_node_t *p_before;  // 上一级节点
    struct list_node_t *p_next;    // 下一级节点
    void *p_data;                  // 存储数据节点
    uint32_t data_size;            // 存储数据大小
} list_node_t;

// 插入数据到链表尾部
int insert_data_to_list(void *p_list_head, const void *data, uint32_t data_size) {
    if (!p_list_head || !data) {
        return -2; // 传入指针为空错误
    }
    
    // 创建新节点
    list_node_t *new_node = (list_node_t *)malloc(sizeof(list_node_t));
    if (!new_node) {
        return -1; // 内存申请错误
    }
    
    new_node->p_data = malloc(data_size);
    if (!new_node->p_data) {
        free(new_node);
        return -1; // 内存申请错误
    }
    
    memcpy(new_node->p_data, data, data_size);
    new_node->data_size = data_size;
    new_node->p_before = NULL;
    new_node->p_next = NULL;
    
    // 找到链表尾部
    list_node_t *current = (list_node_t *)p_list_head;
    while (current->p_next) {
        current = current->p_next;
    }
    
    // 插入新节点到尾部
    current->p_next = new_node;
    new_node->p_before = current;
    
    return 0; // 正确
}

// 从链表中删除节点并释放空间
int delete_list_node(void *p_node) {
    if (!p_node) {
        return -2; // 传入指针为空错误
    }

    list_node_t *node = (list_node_t *)p_node;

    // 重新连接链表
    if (node->p_before) {
        node->p_before->p_next = node->p_next;
    }
    if (node->p_next) {
        node->p_next->p_before = node->p_before;
    }

    // 释放节点数据和节点本身
    free(node->p_data);
    free(node);
    return 0; // 正确
}

// 示例用法
int main() {
    list_node_t head = {NULL, NULL, NULL, 0}; // 初始化链表头部
    int data1 = 100;
    int data2 = 20;
    insert_data_to_list(&head, &data1, sizeof(data1));
    insert_data_to_list(&head, &data2, sizeof(data2));
    // 遍历链表并打印数据
    list_node_t *current = head.p_next;
    while (current) {
        printf("%d\n", *(int *)current->p_data);
        current = current->p_next;
    }
    // 删除所有节点
    current = head.p_next;
    while (current) {
        list_node_t *next = current->p_next;
        delete_list_node(current);
        current = next;
    }
    return 0;
}
相关推荐
uhakadotcom13 分钟前
归因工具:了解国内外顶级产品
算法·面试·github
菜鸟起航ing1 小时前
【Java面试系列】Spring Boot中自动配置原理与自定义Starter开发实践详解 - 3-5年Java开发必备知识
java·spring boot·面试·自动配置·自定义starter
uhakadotcom1 小时前
了解Dapr:构建分布式应用的强大工具
后端·面试·github
uhakadotcom2 小时前
Kibana:数据分析和可视化的强大工具
后端·面试·github
uhakadotcom2 小时前
Grafana:数据可视化和监控平台的强大工具
后端·面试·github
uhakadotcom2 小时前
Unity 中使用 Python 脚本:简明指南
面试·架构·github
拉不动的猪3 小时前
ES2024 新增的数组方法groupBy
前端·javascript·面试
uhakadotcom4 小时前
将游戏上传至 Steamworks 的简单步骤
后端·面试·github
uhakadotcom5 小时前
Google Play SDK 接入指南:一步步轻松集成
javascript·面试·github
uhakadotcom5 小时前
Gemini Code Assist 工具入门指南
后端·面试·github