1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Python:

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        # for i in range(len(nums)):    ##O(n2)time
        #     for j in range(i+1,len(nums)):
        #         if nums[i] + nums[j] == target:
        #             return [i,j]



        if len(nums) < 1:    ##O(n)time
            return False
        dictt = {}
        for i in range(len(nums)):
            if nums[i] in dictt:
                return [dictt[nums[i]],i]
            else:
                dictt[target - nums[i]] = i ## 若输出为[i,j]  
                                            ## dictt中的key值为target-nums[i],value值为i



C++:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> dictt;
        vector<int> result;

        for(int i = 0; i < nums.size(); ++i){
            int numbertofind = target - nums[i];
            if(dictt.find(numbertofind) != dictt.end()){
                result.push_back(dictt[numbertofind]);
                result.push_back(i);
            }
            else{
                dictt[nums[i]] = i;
            }

        }

        return result;
    }
};
【源码免费下载链接】:https://renmaiwang.cn/s/mgvj5 Ackley函数,作为优化算法测试领域的一个重要工具,它的设计初衷是为了评估和比较不同优化算法在处理复杂优化问题时的能力。这个函数具有多模态、非线性、非凸等特性,使得它成为检验全局搜索性能的理想选择。下面将详细探讨Ackley函数的定义、特点以及其在优化算法测试中的应用。Ackley函数由Dennis B. Ackley于1972年提出,其数学表达式如下:\[ f(x) = -20 \exp\left(-0.2\sqrt{\frac{1}{n}\sum_{i=1}^{n}x_i^2}\right) - \exp\left(\frac{1}{n}\sum_{i=1}^{n}\cos(2\pi x_i)\right) + 20 + e \]其中,\( n \) 是输入向量的维度,\( x_i \) 是输入向量的第\( i \)个元素,\( e \)是自然对数的底数(约等于2.718)。函数的目标是找到使该函数值最小化的\( x \)值。注意,此函数在全局最小值为0的位置处有多个局部极小值,这些极小值通常分布在整个定义域内,增加了求解的难度。 Ackley函数的主要特点如下:1. **多模态**:函数中包含了多个局部最小值,这模拟了实际问题中可能出现的复杂地形。2. **非线性**:函数的形状依赖于输入变量的平方和及余弦函数,这使得问题无法通过简单的线性操作解决。3. **非凸**:函数的等值线不是简单的圆形或椭圆形,而是呈现出复杂的曲面结构,进一步增加了优化的挑战。4. **全局最优解**:尽管存在多个局部最小值,但 Ackley 函数有一个全局最小值,即所有\( x_i = 0 \),函数值为0。在优化算法测试中,Ackley函数常被用来评估算法的全局搜索能力、收敛速度和稳定性。优化算法的目标是
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值