一来就给LeetCode跪了

作者从熟悉的OJ平台HackerRank转向LeetCode,面对首个编程题,采用暴力法尝试解决,后意识到排序并非最佳方案。通过分析问题和尝试优化,最终理解并解决了题目的关键点,分享了从审题到解决的过程,包括对排序的思考、代码实现的挑战以及最终的收获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前的OJ战场在 HackerRank ,今天终于禁不住铺天盖地的诱惑,加入 LeetCode 大军


按照学编程就从“Hello world!”开始的尿性,刷OJ当然也从 1 号题出发微笑




Aha~~~!看起来是非常简单的问题呢!


先来个暴力for for法!

什么?!不行?!

这我当然知道!玩玩儿你!


如果不对数组进行排序,那么对于一般的方法,必然是需要进行两两相加比较的(由不加证明的脑内剧场给出……)


好,排序。

<span style="font-family:Microsoft YaHei;">std::sort(numbers.begin(), numbers.end());</span>

要输出序号,当然要配合一个map

<span style="font-family:Microsoft YaHei;">std::map<int, int> index_num_map;</span>


可是这些思路形成后,想到了一些比较奇怪的问题。

如果输入是 [1,-2,7,-1,6],  5 怎么破?该输出 -2,7 的下标还是 -1,6 的下标?

大概是输出优先成对的吧

可是排序之后必然使得优先顺序打乱……

如果没有配对值,题目接口又必须返回一个vector,这个vector应该包含什么?

空的吧

又或者[0,0]吧


于是又否定了排序的思路,开始想着在原方法上简化

例如使用各种 if 手段跳过某些循环项


这么尝试了各种办法,N次(后面会看到,真的是很多次),还是不行


不过在这些试验中,通过错误数据渐渐发现是我想多了 ORZ……


日啊,审题审题!!!这个初中高中就被各科老师强调无数次的东西,还是中计了!!!

“YOU MAY ASSUME THAT EACH INPUT WOULD HAVE EXACTLY ONE SOLUTION”

说多了都是泪,直接造成最后我的提交就是这样的哭↓↓↓



总结一下:

1、LeetCode的题目确实别于其他的OJ,更加侧重于解决某个特殊的实际问题

2、函数接口给定,不需要自己写IO什么的,这点和第一条一致

3、代码无法直接复制进IDE调试,要么肉眼Debug(甚至连std::cout都不会输出!),要么自己动手写Client

4、正因为各种限定较多,更需要仔细阅读题目,理解题目

5、O(N^2)就是 O(N^2),不能指望通过 if 来降低复杂度。

leetcode 凑硬币 :open_file_folder: 算法 :page_facing_up: :open_file_folder: 黑客等级 :open_file_folder: 数组 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 堆 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: Linked_Lists :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 队列 :open_file_folder: 3_-_硬 :page_facing_up: :open_file_folder: 递归 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 排序 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 堆栈 :open_file_folder: 3_-_硬 :page_facing_up: :page_facing_up: :open_file_folder: 力码 :open_file_folder: 大批 :open_file_folder: 1_-_简单 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 2_-_中 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 3_-_硬 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 位操作 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: DP :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: 2_-_中 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 深度优先_搜索,广度优先_搜索 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: 2_-_中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值