1. Two sum

题目描述
给定一个整型数组,要求返回两个数的下标,使得两数之和等于给定的目标值,要求同一个下标不能使用两次。
数据保证有且仅有一组解。

样例
给定数组 nums = [2, 7, 11, 15],以及目标值 target = 9,

由于 nums[0] + nums[1] = 2 + 7 = 9,
所以 return [0, 1].
 

算法1:

(暴力枚举)O(n^2)

使用两个循环实现枚举,每一次循环为O(n),两次循环时间复杂度为O(n^2)

C:(执行用时:272ms,内存消耗:7.6M)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int *res = (int *)malloc(sizeof(int) * 2);
    *returnSize = 0;
    for (int i = 0; i < numsSize; i++) {
        for (int j = i + 1; j < numsSize; j++) {
            if (nums[i] + nums[j] == target) {
                res[0] = i;
                res[1] = j;
                *returnSize = 2;
                break;
            }
        }
    }
    return res;
}

C++:(执行时间:220 ms,内存消耗:9.1M)

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (nums[i] + nums[j] == target) {
                    res = vector<int>({i, j});
                    break;
                }
            }
        }
        return res;
    }
};

算法二:使用hash table

通过一次遍历,使用除留余数法和链地址法构造hash table,并且查找

C:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 struct hashData {
     int value;
     int key;
     struct hashData *next;
 };

 typedef struct hashTable {
     struct hashData **elem_head; // array stored the head of the single list
     int width;
 }hashTable;

 int hashInit(hashTable *table, int width) {
     if (width < 0)
        return 1;
     struct hash_data **tmp = malloc(sizeof(struct hash_data *)*width);
     table->elem_head = tmp;
     memset(table->elem_head, 0, width * sizeof(struct hash_data *));
     table->width = width;
     return 0;
 }

 void hashFree(hashTable *table) {
     if (table->elem_head != NULL) {
         for (int i = 0; i < table->width; i++) {
             struct hashData *elem = table->elem_head[i];
             while(elem != NULL) {
                 struct hashData *temp = elem;
                 elem = elem->next;
                 free(temp);
             }
         }
         free(table->elem_head);
         table->elem_head = NULL;
     }
     table->width = 0;
}

int hashFunc(hashTable *table, int value) {
    int addr = abs(value) % table->width;
    return addr;
}

int hashInsert(hashTable *table, int value, int key) {
    int addr = hashFunc(table, value);
    struct hashData *tmp = (struct hashData *)malloc(sizeof(struct hashData));
    if (tmp == NULL) {
        return -1;
    }
    tmp->value = value;
    tmp->key = key;
    tmp->next = table->elem_head[addr];
    table->elem_head[addr] = tmp;
    printf("Insert successfully, value = %d, key = %d\n", tmp->value, tmp->key);
    return 0;
}

struct hashData* hashFind(hashTable *table, int value) {
    int addr = hashFunc(table, value);
    printf("start find value = %d\n", value);
    struct hashData *tmp = table->elem_head[addr];
    while (tmp != NULL) {
        if (tmp->value == value) {
            printf("find value = %d, key = %d\n", tmp->value, tmp->key);
            return tmp;
        }
        tmp = tmp->next;
    }
    printf("cannot find value = %d\n", value);
    return NULL;
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int *res = (int *)malloc(sizeof(int) * 2);
    *returnSize = 0;
    hashTable table;
    hashInit(&table, 100);
    for (int i = 0; i < numsSize; i++) {
        int value = target - nums[i];
        struct hashData *data = hashFind(&table, value);
        if (data != NULL) {
            res[0] = data->key;
            res[1] = i;
            *returnSize = 2;
            printf("res: [%d, %d]\n", data->key, i);
            break;
        }
        hashInsert(&table, nums[i], i);
    }
    hashFree(&table);
    return res;

C++:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        unordered_map<int, int> hash;
        for (int i = 0; i < nums.size(); i++) {
            int value = target - nums[i];
            if (hash.count(value)) {
                res = vector<int>({hash[value], i});
                break;
            }
            hash[nums[i]] = i;
        }
        return res;
    }
};

 

内容概要:本文围绕新一代传感器产品在汽车电子电气架构中的关键作用展开分析,重点探讨了智能汽车向高阶智能化演进背景下,传统传感器无法满足感知需求的问题。文章系统阐述了自动驾驶、智能座舱、电动化与网联化三大趋势对传感器技术提出的更高要求,并深入剖析了激光雷达、4D毫米波雷达和3D-ToF摄像头三类核心新型传感器的技术原理、性能优势与现存短板。激光雷达凭借高精度三维点云成为高阶智驾的“眼睛”,4D毫米波雷达通过增加高度维度提升环境感知能力,3D-ToF摄像头则在智能座舱中实现人体姿态识别与交互功能。文章还指出传感器正从单一数据采集向智能决策升级,强调车规级可靠性、多模态融合与成本控制是未来发展方向。; 适合人群:从事汽车电子、智能驾驶、传感器研发等相关领域的工程师和技术管理人员,具备一定专业背景的研发人员;; 使用场景及目标:①理解新一代传感器在智能汽车系统中的定位与技术差异;②掌握激光雷达、4D毫米波雷达、3D-ToF摄像头的核心参数、应用场景及选型依据;③为智能驾驶感知层设计、多传感器融合方案提供理论支持与技术参考; 阅读建议:建议结合实际项目需求对比各类传感器性能指标,关注其在复杂工况下的鲁棒性表现,并重视传感器与整车系统的集成适配问题,同时跟踪芯片化、固态化等技术演进趋势。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值