leetcode 0207
✅ 561. 数组拆分 I
https://leetcode-cn.com/problems/array-partition-i

- 评论:
其实就是把从a1到an数组下标为奇数的数都加起来,题目花里胡哨的
比较各个语言的排序算法速度吗?
- 解答
/*
思路:
排序,然后将下标为 0、2、4 ... 个数相加即可。
由于是纯数字,并且限定了数字范围,所以可使用基数排序达到 O(n) 复杂度。
数字范围 [-10000, 10000],所以可创建 n[20001],对每个元素加 10000 使其变为正数。
*/
int arrayPairSum(int* nums, int numsSize)
{
int n[20001] = { 0 }, i, j, sum;
for (i = 0; i < numsSize; i++) //建立值、键哈希表,即基数排序
n[nums[i] + 10000]++; //保证下标为正数
for (i = j = sum = 0; i < 20001; ) //将下标为 0、2、4 ... 的相加
if (n[i]) //判断是否存在该数,若存在则判断是否偶数下标
{
//tt 这里的 i - 10000 比较奇妙
if (j % 2 == 0) sum += i - 10000; //偶数下标,累加
j++; //计数
n[i]--; //该值减 1
}
else i++; //不存在,跳过该值
return sum;
}
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2])
'''执行用时 :
416 ms
, 在所有 Python3 提交中击败了
23.18%
的用户
内存消耗 :
15.5 MB
, 在所有 Python3 提交中击败了
49.94%
的用户'''
✅ 1025. 除数博弈
https://leetcode-cn.com/problems/divisor-game

聪明的数学归纳法:
- 结论是:
奇则输,偶则赢
- 解释:
基本思路:
最终结果应该是占到 2 的赢,占到 1 的输;
若当前为奇数,奇数的约数只能是奇数或者 1,因此下一个一定是偶数;
若当前为偶数, 偶数的约数可以是奇数可以是偶数也可以是 1,因此直接减 1,则下一个是奇数;
因此,奇则输,偶则赢。
return N%2==0
动态规划又来了(没理解,todo 0207):
- 基本思路:
将所有的小于等于 N 的解都找出来,基于前面的,递推后面的。
状态转移: 如果 i 的约数里面有存在为 False 的(即输掉的情况),则当前 i 应为 True;如果没有,则为 False。
笔记:
状态转移: 如果 i 的约数里面有存在为 False 的(即输掉的情况),(tt4)则当前 i 应为 True;如果没有(约数里没有输掉,则当前是输掉,wtf?),则为 False。????
class Solution:
def divisorGame(self, N: int) -> bool:
target = [0 for i in range(N+1)]
target[1] = 0 #若爱丽丝抽到1,则爱丽丝输
if N<=1:
return False
else:
target[2] = 1 #若爱丽丝抽到2,则爱丽丝赢
for i in range(3,N+1):
for j in range(1,i//2):
# 若j是i的余(这里应该说:约数 吧)数且target[i-j]为假(0)的话,则代表当前为真(1)
if i%j==0 and target[i-j]==0:
target[i] = 1 # 对应:tt4 则当前 i 应为 True
break
return target[N]==1
'''作者:pandawakaka
链接:https://leetcode-cn.com/problems/divisor-game/solution/python3gui-na-fa-by-pandawakaka/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。'''
✅ 557. 反转字符串中的单词 III
https://leetcode-cn.com/problems/reverse-words-in-a-string-iii

- my c ans:
char * reverseWords(char * s){
int n = strlen(s);
int begin = 0, end;
for(int i = 0; i < n + 1; i++) {
// i travel to a word end(aka the blank or end0 after a word)
if(s[i] == ' ' || s[i] == '\0') {
for (end = i - 1; begin < end; begin++, end--) {
int tmp = s[begin];
s[begin] = s[end];
s[end] = tmp;
}
begin = i + 1;// begin no locate the start of next word
}
}
return s;
}
执行用时 :
12 ms
, 在所有 C 提交中击败了
58.89%
的用户
内存消耗 :
8.2 MB
, 在所有 C 提交中击败了
87.22%
的用户
py 中的 字符 split 与 列表 倒序的 组合手法疑问
- 结果:

- 代码如下:

✅ 852. 山脉数组的峰顶索引
https://leetcode-cn.com/problems/peak-index-in-a-mountain-array

int peakIndexInMountainArray(int* A, int ASize){
int max = 0;
int soldier = 0;
for (; soldier < ASize - 1; soldier++) {
if(A[soldier] > A[max]) {
max = soldier;
}
}
return max;
}
执行用时 :
16 ms
, 在所有 C 提交中击败了
50.41%
的用户
内存消耗 :
7.6 MB
, 在所有 C 提交中击败了
84.08%
的用户
提升,by knowing 山顶的唯一性
int peakIndexInMountainArray(int* A, int ASize){
int soldier = 0;
for (; A[soldier] < A[soldier + 1]; soldier++) {
//do nothing
}
return soldier;
}
执行用时 :
24 ms
, 在所有 C 提交中击败了
9.92%
的用户
内存消耗 :
7.7 MB
, 在所有 C 提交中击败了
24.08%
的用户
leetcode 0207的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 解决Macbook Pro蓝牙不可用问题
谷歌搜索了下,在威锋网看到一个帖子,需要关机重置电源管理单元和系统NVRAM恢复出厂设置,具体操作如下:1.关机2.同时按下shift+control+option+power,保持5秒左右3.先按下 ...
- [CF]Round510
由于我的codeforces的帐号登不上,所以我错过了这场比赛,只好赛后再抄题解自己做. A Benches 最大的情况就是所有人都挤在那个人最多的长椅上,最小的情况是所有人尽量平均的坐. #incl ...
- Oracle常用函数记录
Oracle函数 --schema:hcf --不带任何参数 http://www.cnblogs.com/wuyisky/archive/2010/05/11/oracle_function.htm ...
- P1216 [IOI1994]数字三角形
史上最水的 dp 题,没有之一(By rxz) 确实很简单,就算是我这个 dp 萌新也一眼看出来了转移方程 首先考虑状态,设 \(f_{i,j}\) 表示选择第 \(i\) 层第 \(j\) 个数时获 ...
- Java compareTo的用法
compareTo() 方法用于将 Number 对象与方法的参数进行比较.可用于比较 Byte, Long, Integer等. 该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比 ...
- 基于icmp的tracert路由追踪程序
https://blog.csdn.net/u013271921/article/details/45488173 #include<winsock2.h> //#include<i ...
- drf大总结
接口 """ 1.什么是接口:url+请求参数+响应数据 | 接口文档 2.接口规范: url:https,api,资源(名词复数),v1,get|post表示操作资源的 ...
- 鬼斧神工:求n维球的体积
原文地址:http://spaces.ac.cn/archives/3154/ 原文作者:苏剑林 标准思路 简单来说,\(n\)维球体积就是如下\(n\)重积分 \[V_n(r)=\int_{x_1^ ...
- Boxes and Candies
问题 G: Boxes and Candies 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 There are N boxes arranged in a row. ...
- MySQL命令行脚本
1. 命令行连接 打开终端,运行命令 mysql -uroot -p 回车后输入密码,当前设置的密码为mysql 退出登录 quit 和 exit 或 ctrl+d 查看版本:select versi ...