hot100_两数之和_2刷

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

暴力枚举
时间复杂度:O(n*n)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        List<Integer> result =new ArrayList<>();
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if(nums[i]+nums[j]==target){
                    result.add(i);
                    result.add(j);
                }
            }
        }
        return result.stream().mapToInt(i->i).toArray();
    }
}

哈希表
时间复杂度:O(n*1)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> hashtable = new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;++i){
            if(hashtable.containsKey(target-nums[i])){
                return new int[]{hashtable.get(target-nums[i]),i};
            }
            hashtable.put(nums[i],i);
        }
        return new int[0];
    }
}

数组是顺序情况下,可以
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.lengths;
int l=0,r=n-1;
while (l<=r){
if(nums[l]+nums[r]==target){
return [nums[l],nums[r]];
}
else if(nums[l]+nums[r]<target){
l++;
}
else{
r–;
}
}
return [];
}
}

1.nums.length 获取数组长度
2.return new int[]{l,r}; 返回数组,先new int[]{l,r};
3.return new int[0]; 返回数组,先new int[]{0};
如果数组是乱序的,就不行。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        int l=0,r=n-1;
        while (l<=r){
            if(nums[l]+nums[r]==target){
                return new int[]{l,r};
            }
            else if(nums[l]+nums[r]<target){
                l++;
            }
            else{
                r--;
            }
        }
        return new int[0];
    }
}


乱序情况下
暴力方法

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for(int i=0;i<=n-1;i++){
            for(int j=i+1;j<=n-1;j++){
                if(nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return new int[0];
    }
}

哈希表HashMap
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
HashMap hash_cnt <Intager,Intager> = HashMap<Intager,Intager>;
for(int i=0;i<n;i++){
if(hash_cnt.contins(target-nums[i])){
return new int[]{nums[i],target-nums[i]}
}
else{
hash_cnt.put(nums[i])
}
}
return new int[0];
}
}
哈希表<Intager,Intager>的创建: Map<Intager,Intager> hashtable = new HashMap<Intager,Intager>();
哈希表<Intager,Intager>包含值:hashtable.containsKey(target-nums[i])
哈希表<Intager,Intager>取键:hashtable.get(target-nums[i])
哈希表<Intager,Intager>写入键值对:hashtable.put(nums[i],i)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        // HashMap hash_cnt <Intager,Intager> = HashMap<Intager,Intager>;
        Map<Integer,Integer> hashtable = new HashMap<Integer,Integer>();
        for(int i=0;i<n;i++){
            // if(hashtable.contins(target-nums[i])){
            if(hashtable.containsKey(target-nums[i])){
                // return new int[]{nums[i],target-nums[i]};
                return new int[]{hashtable.get(target-nums[i]),i};
            }
            else{
                // hashtable.put(nums[i]);
                hashtable.put(nums[i],i);
            }
        }
        return new int[0];
    }
}

​Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
这行代码创建了一个 HashMap 实例,并将其赋值给 hashtable 变量。
Map<Integer, Integer> 表示这个 HashMap 的键和值都是 Integer 类型。

hashtable.containsKey(target - nums[i]):
这个方法用于检查 hashtable 中是否包含指定的键(key)。
如果存在,返回 true;否则返回 false。

hashtable.get(target - nums[i]):
这个方法用于根据键(key)获取对应的值(value)。

hashtable.put(nums[i], i):
这个方法用于将键值对(key-value pair)存储到 hashtable 中。
nums[i] 是键(key),i 是值(value)。
如果键已经存在,则更新对应的值;如果键不存在,则添加新的键值对。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值