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 ...
随机推荐
- bugku 本地包含
本地包含 题目信息 地址:http://123.206.87.240:8003/ <?php include "flag.php"; $a = @$_REQUEST['hel ...
- 微信小程序中showToast 提示
icon可以none,也可以 success wx.showToast({ title: '已提交', icon: 'success', duration: 2000 })
- 截取字符,超出的用省略号代替js实现 substring
可用到截取文字过多的问题,取0到6之间的字符,不包含6title.substring(0,6)+'...';
- Linux list_head
在linux kernel里面链表应用非常广泛. 我们在应用程序中,定义一个链表结构通常要包含数据域,如下: typedef struct _listNode{ int data; struct _l ...
- C#中的注释
帮助程序员便于阅读代码 单行注释 // 多行注释 /* * */ 文档注释 /// <summary> /// ... /// <summary>
- yii2 password hash生成与验证方法
1.生成 $password是明文.如:123456 $this->password_hash = Yii::$app->security->generatePasswordHash ...
- LOJ#6713. 「EC Final 2019」狄利克雷 k 次根 加强版
题目描述 定义两个函数 \(f, g: \{1, 2, \dots, n\} \rightarrow \mathbb Z\) 的狄利克雷卷积 \(f * g\) 为: \[ (f * g)(n) = ...
- 02-Spring的IOC示例程序(通过id获取对象)
*******通过IOC容器创建id对象并为属性赋值******** 整体结构: ①创建一个java工程 ②导包 ③创建log4j.properties日记配置文件 # Global logging ...
- warmup
先简单了解下源码中的2个函数: <?php echo mb_strpos("朋友比生命还重要?或许是吧" . '?',"?"); echo "\ ...
- Uva 136 丑数
n^2暴力就完事,但是上限要高,不然就算不到对应的1500,刘汝佳的写法更好. #include <bits/stdc++.h> using namespace std; const in ...