LeetCode刷题笔录 Two Sum

生存网络与mlr3proba 通过结合生存网络和mlr3proba,可以使用生存网络模型来预测个体在给定时间点发生事件的概率,并使用mlr3proba提供的工具进行模型的训练、评估和选择最佳模型。iii)基本的机器学习(ML)方法,如重新排序和调整。在本文中,我们将只讨论前五种,因为它们在文献中得到了更好的建立,并且它们具有相同的接口,这简化了调优,我们将在下面看到。我们不会为模型指定自定义架构,而是使用默认架构,如果你熟悉PyTorch,那么你可以选择创建自己的架构,如果你愿意的话,通过将其传递给模型中的custom_net参数。 阅读详情

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

思路:

先把原数组复制一遍,然后进行排序。在排序后的数组中找这两个数。最后再在原数组中找这两个数字的index即可。

时间复杂度O(nlogn)+O(n)+O(n) = O(nlogn)

注意的是结果有可能是两个数是相同的,比如 0 3 4 0, 0要返回1和4,不要返回成1和1或者4和4.

代码:

	public int[] twoSum(int[] numbers, int target) {
        // Start typing your Java solution below
        // DO NOT write main() function
		
		//Copy the original array and sort it
		int N = numbers.length;
		int[] sorted = new int[N];
		System.arraycopy(numbers, 0, sorted, 0, N);
        Arrays.sort(sorted);
        //find the two numbers using the sorted arrays
        int first = 0;
        int second = sorted.length - 1;
        while(first < second){
        	if(sorted[first] + sorted[second] < target){
        		first++;
        		continue;
        	}
        	else if(sorted[first] + sorted[second] > target){
        		second--;
        		continue;
        	}
        	else break;
        }
        int number1 = sorted[first];
        int number2 = sorted[second];
        //Find the two indexes in the original array
        int index1 = -1, index2 = -1;
        for(int i = 0; i < N; i++){
        	if((numbers[i] == number1) || (numbers[i] == number2)){
        		 if(index1 == -1)
        			 index1 = i + 1;
        		 else
        			 index2 = i + 1;
        	}
        		
        }
        int [] result = new int[]{index1, index2};
        Arrays.sort(result);
		return result;
    }

还有个无耻地利用hashmap的O(n)的算法,原理和暴力搜索没有本质区别,只不过hashmap的搜索速度是O(1)。

public int[] twoSum(int[] numbers, int target) {
        // Start typing your Java solution below
        // DO NOT write main() function
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		int n = numbers.length;
		int[] result = new int[2];
		for (int i = 0; i < numbers.length; i++)
        {
            if (map.containsKey(target - numbers[i]))
            {
                result[0] = map.get(target-numbers[i]) + 1;
                result[1] = i + 1;
                break;
            }
            else
            {
                map.put(numbers[i], i);
            }
        }
		return result;
        
    }




使用在UE5中使用AirSim插件Eigen库头文件引用报错,出现报错的解决方式 如图所示,用红线圈出的两条头文件引用会报错,提示无法找到他们,但是可以发现的是,他们的路径书写是没有问的。在终端中运行时候,会出现如下的报错。 阅读详情

相关推荐

手把手教你学PCIE--固件与BIOS配置 - BIOS设置对PCIe性能的影响

本文详细分析了BIOS设置对PCIe性能的影响,并提供了优化建议。通过合理调整链路参数、功耗管理策略和高级功能配置,可以显著提升系统的性能和稳定性。本文将进一步分析BIOS设置如何影响PCIe设备的性能,并提供优化建议。BIOS(或UEFI)是系统启动时的第一个控制层,其设置直接决定了PCIe设备的工作状态和性能表现。BIOS中的功耗管理选项(如C-States、P-States)会影响PCIe链路的低功耗模式(L0s/L1)。MSI/MSI-X中断具有更高的性能和更好的可扩展性,应作为首选方案。

MHD0815的博客 963

leetcode825. 适龄的朋友

在社交媒体网站上有 n 个用户。给你一个整数数组 ages,其中 ages[i] 是第 i 个用户的年龄。如果下述任意一个条件为真,那么用户 x 将不会向用户 y 发送好友请求。否则,x 将会向 y 发送一条好友请求。注意,如果 x 向 y 发送一条好友请求,y 不必也向 x 发送一条好友请求。另外,用户不会向自己发送好友请求。返回在该社交媒体网站上产生的好友请求总数。...

2021dragon的博客 1664

华为OD机试 - 学校的位置 - 数学问(Python/JS/C/C++ 2024 B卷 100分)

华为OD机试 2024E卷库疯狂收录中,

学Java,找哪吒 430

LeetCode:825. 适龄的朋友(排序 + 双指针 Java

排序完之后,遍历,其实就是找0.5 * age + 7到age之间的值而已,所以while寻找找出左右边界,然后加和区间数量即可。

Cosmoshhhyyy的博客 371

(nice!!!)(LeetCode) 825. 适龄的朋友(滑动窗口)

【代码】(nice!!!)(LeetCode) 825. 适龄的朋友(滑动窗口)

记录一些刷过的题并分享学习心得 333

LeetCode 825. 适龄的朋友(计数排序+前缀和)

文章目录1. 目2. 解 1. 目 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄。 当满足以下任一条件时,A 不能给 B(A、B不为同一人)发送好友请求: age[B] <= 0.5 * age[A] + 7 age[B] > age[A] age[B] > 100 && age[A] < 100 否则,A 可以给 B 发送好友请求。 注意如果 A 向 B 发出了请求,不等于 B 也一定会向 A 发出请求。

Michael是个半路程序员 854

leetcode 825. 适龄的朋友 中等

产生的好友请求为 110 -> 100 ,120 -> 110 ,120 -> 100。产生的好友请求为 17 -> 16 ,18 -> 17。另外,用户不会向自己发送好友请求。返回在该社交媒体网站上产生的好友请求总数。如果下述任意一个条件为真,那么用户。2 人互发好友请求。

2401_88085478的博客 301

LeetCode 笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

LeetCode第 842 :将数组拆分成斐波那契序列(C++)

842. 将数组拆分成斐波那契序列 - 力扣(LeetCode) 标准的枚举型了,由于存在不能拆分的情况,所以应该使用带返回值的回溯,需要枚举前两个数字,然后根据前两个数字依次检查后面是否存在合法的序列,如果有,则返回true,否则返回false。 class Solution { public: vector<int> res; bool dfs(string &s, int idx){//idx表示当前下标 if(idx == s.size())

zj 909

leetcode | go | 第842 | 将数组拆分成斐波那契序列

【代码】leetcode | go | 第842 | 将数组拆分成斐波那契序列。

iCling的博客 495

Leetcode:825. 适龄的朋友

在社交媒体网站上有 n 个用户。给你一个整数数组 ages ,其中 ages[i] 是第 i 个用户的年龄。如果下述任意一个条件为真,那么用户 x 将不会向用户 y(x!= y)发送好友请求:否则,x 将会向 y 发送一条好友请求。注意,如果 x 向 y 发送一条好友请求,y 不必也向 x 发送一条好友请求。另外,用户不会向自己发送好友请求。返回在该社交媒体网站上产生的好友请求总数。示例 1:输入:ages = [16,16]输出:2解释:2 人互发好友请求。

2302_79279009的博客 314

842. 将数组拆分成斐波那契序列之深度遍历

文章目录一,842. 将数组拆分成斐波那契序列二, 解思路三, 解代码 一,842. 将数组拆分成斐波那契序列 给定一个数字字符串 S,比如 S = “123456579”,我们可以将它分成斐波那契式的序列 [123, 456, 579]。 形式上,斐波那契式序列是一个非负整数列表 F,且满足: 0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型); F.length >= 3; 对于所有的0 <= i < F.length -

永远积极向上、永远热泪盈眶、永远豪情满怀、永远坦坦荡荡 340

LeetCode 825. 适龄的朋友(双指针)

825. 适龄的朋友 在社交媒体网站上有 n 个用户。给你一个整数数组 ages ,其中 ages[i] 是第 i 个用户的年龄。 如果下述任意一个条件为真,那么用户 x 将不会向用户 y(x != y)发送好友请求: age[y] <= 0.5 * age[x] + 7 age[y] > age[x] age[y] > 100 && age[x] < 100 否则,x 将会向 y 发送一条好友请求。 注意,如果 x 向 y 发送一条好友请求,y 不必也向 .

m0_51630248的博客 448

LeetCode 825 Friends Of Appropriate Ages

LeetCode 825 Friends Of Appropriate Ages 传送门 目分析 Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person. Person A will NOT frien...

博客 1741

LeetCode 842. 将数组拆分成斐波那契序列

目描述 给定一个数字字符串S,比如S = "123456579",我们可以将它分成斐波那契式的序列[123, 456, 579]。 形式上,斐波那契式序列是一个非负整数列表F,且满足: 0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型); F.length >= 3; 对于所有的0 <= i < F.l...

yj_coder的博客 700

LeetCode 825. Friends Of Appropriate Ages

解 看似简单,其实也简单,就是出人故意挖了坑等你跳。 第一坑,冗余条件3,和2一个意思。 第二坑,排序无能,爆时间。 躲了这俩坑就基本明白了,就是桶排序。 ps: 我的代码还可以优化,可以再缩减遍历的空间。 Code int cot[121]; int numFriendRequests(vector&lt;int&gt;&amp; ages) { int n = ages....

蟋蟀的博客 252

leetcode 825. Friends Of Appropriate Ages

leetcode 825. Friends Of Appropriate Ages 原地址:https://leetcode.com/problems/friends-of-appropriate-ages/ 目 Some people will make friend requests. The list of their ages is given and ages[i] is ...

birdreamer的博客 790

【python3】leetcode 825. Friends Of Appropriate Ages (Medium)

825. Friends Of Appropriate Ages (Medium) Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.  Person A will NOT friend request person B...

momottyy的专栏 393

LeetCode - 825 - 适龄的朋友 - Java -在作者看来还算细

文章目录目解析解思维1:代码附图解思维2:附上方法二的代码 目   目解析   解思维1: 将 数组ages 进行排序(升序),其实就是将这批人 按照年龄 从高到低 进行排序。 此时再使用 两个 “左右指针” 进行锁定 x 的交友范围。 确定好了之后,right - left 就是 符合 x 交友条件的人数,也就是目中的 x 会向 y 发送请求。   代码 class Solution { public int numFriendRequests(int[] ag

DarkAndGrey的博客 871

Leetcode 825:适龄的朋友

目描述 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i]表示第 i 个人的年龄。 当满足以下条件时,A 不能给 B(A、B不为同一人)发送好友请求: age[B]<= 0.5 * age[A]+ 7 age[B]> age[A] age[B]> 100 &&age[A]< 100 否则,A 可以给 B 发送好友请...

weixin_35338624的博客 265

Leetcode842叶子相似的树

附上非递归:vector保存中序遍历的结果 bool leafSimilar(TreeNode* root1, TreeNode* root2) { if(root1==NULL||root2==NULL) return false; // bre(root1,root2); ...

weixin_39137699的博客 219

LeetCode】842. Split Array into Fibonacci Sequence 解报告(Python & C++)

LeetCode】842. Split Array into Fibonacci Sequence 解报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 目地址:https://leetcode.com/probl...

负雪明烛 2577

vlc SDK 包

VLC SDK包,2.2.6版本,包含32位 和 64位两个版本,视频开发必备,包含lib库,实测可用

中科大的500-CSL数据集500类中文手语单词数据集已转化为mat格式

中科大的500-CSL数据集500类中文手语单词数据集已转化为mat格式

上一篇: 利用位运算判断整数的正负
下一篇: LeetCode刷题笔录 Median Of Two Sorted Arrays
likecool21
博客等级 码龄14年 14粉丝 123原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值