✅ 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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 【Python】 基础语法

    Python 语言与 Perl,C 和 Java 等语言有许多相似之处.但是,也存在一些差异. 第一个 Python 程序  欢迎,入坑! Python 标识符 在 Python 里,标识符由字母.数 ...

  2. HTML学习(14)表单

    HTML 表单用于收集不同类型的用户输入. HTML 表单 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入内容,比如:文本域(textarea).下拉列表.单选框(radio-butt ...

  3. Bridge(Ad Hoc)

  4. Flask 教程 第二十一章:用户通知

    本文翻译自The Flask Mega-Tutorial Part XXI: User Notifications 这是Flask Mega-Tutorial系列的第二十一章,我将添加一个私有消息功能 ...

  5. (BFS)1097: Yuchang and Zixiang ‘s maze

    1097: Yuchang and Zixiang ‘s maze Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 863 Solved: 149   De ...

  6. ColorPix——到目前为止最好用的屏幕取色器

    分享一个颜色取色器网页.PPT.EXCEL配色不再烦恼 简单易用 大家做商业.企业报告的时候是不是经常遇到要调色的困扰呢?PPT.EXCEL等颜色选取会对报告有质的影响!!要更专业要更有美感!给大家分 ...

  7. Panda的学习之路(3)——pandas 设置特定的值&处理没有数据的部分

    先设定好我们的dataframe: # pandas 设置特定的值 dates=pd.date_range(',periods=6) # print(dates) df=pd.DataFrame(np ...

  8. 【常见浏览器的UA】

    "所谓 UA(User Agent / 用户代理),最初是指浏览器告知网站管理员,本浏览器支持的类型和框架,让网站管理员以支持的模式结构向浏览器发送页面,呈现给用户浏览.演变到今天,网站管理 ...

  9. VMware克隆centos后需要进行修改配置的地方

    1. 首先在VMware中通过复制现在状态的虚拟机或者快照形式的虚拟机,选择完整复制文件进行克隆. 2.打开克隆的虚拟机之后,需要修改主机名和相应的hosts表 2.1 修改主机名 输入  vi /e ...

  10. iframe重新加载

    方法1: document.getElementById('iframeId').contentWindow.location.reload(true); 方法2: document.getEleme ...