✅ 292. Nim 游戏

https://leetcode-cn.com/problems/nim-game

你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。

你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。

  • 示例:
输入: 4
输出: false
解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛;
  因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。



bool canWinNim(int n){
return n % 4 != 0;
} 执行用时 :
0 ms
, 在所有 C 提交中击败了
100.00%
的用户
内存消耗 :
6.6 MB
, 在所有 C 提交中击败了
92.20%
的用户

✅ 933. 最近的请求次数

https://leetcode-cn.com/problems/number-of-recent-calls

解释:https://leetcode-cn.com/problems/number-of-recent-calls/solution/ti-mu-jie-shi-ji-si-lu-by-bai-li-xi-2/

我的解释:

//首先有个列表
List<int> l = new List<int>() {} ; // 每次我们enqueue 这个列表一些 数字,依次是1, 100, 3001, 3002
input: 1
output: l = {1} input: 100
output: l = {1,100} input: 3001
output: l = {1,100,3001} input: 3002
output: l = {1,100,3001,3002}

然后是人工解答:

当 input = 1
l = {1}
按照之前我们理解的意思,我们需要找到 l 中所有大于等于 0( 原为1 - 3000,但是时间没有负数,所以返回0,上面讨论过) 并且小于等于1的数的总个数。我们遍历 l发现有1个数,所以返回 1.<<<<< 当 input = 100
l = {1,100} (1为上一步所加进的)
这次我们要找到l中大于等于0 (100-3000 = -2900 => 0)且小于等于100的值,这里有2个,所以返回2 。<<<<< 当 input = 3001
l = {1,100,3001}
这次我们要找到l中大于等于1(3001- 3000 = 1)且小于等于3001的个数,这里为3,所以返回3。<<<<<< 当 input = 3002
l = {1,100,3001,3002}
找到 大于等于2小于等于3002的个数为3(100,3001,3002),所以返回3。<<<<
  • 别人的c 解答:(理解了,需要自己多重复一遍)
#define RET_OK  0
#define RET_ERR 1
#define QUEUE_MAX 3001
typedef struct {
int buf[QUEUE_MAX];
int size;
int cnt;
int head;
int tail;
} RecentCounter; int rcIsEmpty(RecentCounter *rc)
{
if (rc->cnt == 0) {
return RET_OK;
} else {
return RET_ERR;
}
}
//item 定位head
int rcPeekHead(RecentCounter *rc, int *item)
{
if (rc->cnt == 0) {
return RET_ERR;
}
*item = rc->buf[rc->head];
return RET_OK;
}
//item 定位head ,head 指针增加,cnt 减少,
int rcPollHead(RecentCounter *rc, int *item)
{
if (rc->cnt == 0) {
return RET_ERR;
}
*item = rc->buf[rc->head];
rc->head = (rc->head + 1) % rc->size;
rc->cnt--;
return RET_OK;
}
int rcInTail(RecentCounter *rc, int item)
{
if (rc->cnt == QUEUE_MAX) {
return RET_ERR;
}
rc->buf[rc->tail] = item;
rc->tail = (rc->tail + 1) % rc->size;
rc->cnt++;
return RET_OK;
}
RecentCounter* recentCounterCreate() {
RecentCounter* rc = (RecentCounter*) calloc (1, sizeof(RecentCounter));
if (rc == NULL) {
return NULL;
}
rc->size = QUEUE_MAX;
rc->cnt = 0;
rc->head = 0;
rc->tail = 0;
return rc;
}
// int t: 任何处于 [t - 3000, t] 时间范围之内的 ping 都将会被计算在内,
int recentCounterPing(RecentCounter* obj, int t) {
int ret;
int item = INT_MIN;
int cnt = 0; while(rcIsEmpty(obj) != RET_OK) {
if (rcPeekHead(obj, &item) != RET_OK) {
break;
}
if ((item + 3000 )< t) {// 残留 tt tdo 疑问 0206 ;done ; when: item 定位到 的 head 加上 3000 仍旧小于t, 那么 需要做: 队列抛弃头;这个答案的作者,非常知道如何使用队列这个有力的武器。
rcPollHead(obj, &item);
} else {
break;
}
}
rcInTail(obj, t);//ok, 这是因为,我们要把3002 也要算进去,所以又 enqueue t 了。
cnt = obj->cnt;
return cnt;
} void recentCounterFree(RecentCounter* obj) {
if (obj != NULL) {
free(obj);
}
} 作者:jafon
链接:https://leetcode-cn.com/problems/number-of-recent-calls/solution/c-dui-lie-by-jafon/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 我的抄写(下次请用py 是否更快??0206 todo req)
#define YES 1
#define NO 0
#define MAX_QUEUE_SIZE 3001
typedef struct {
int buf[MAX_QUEUE_SIZE];
int size;
int cnt;
int head;
int tail;
} RecentCounter; int rcIsEmpty(RecentCounter *rc) {
if (rc->cnt == 0) {
return YES;
} else {
return NO;
}
}
//soldier locate the head's content
int rcPeekHead(RecentCounter *rc, int *soldier){
if (rc->cnt == 0) {
return NO;
}
*soldier = rc->buf[rc->head];
return YES;
}
//soldier locate the head's content and move forward head
// and decrease cnt
int rcPollHead(RecentCounter *rc, int *soldier){
if (rc->cnt == 0) {
return NO;
}
*soldier = rc->buf[rc->head];
rc->head = (rc->head + 1) % rc->size;
rc->cnt--;
return YES;
}
//jst want to enqueue the soldier in the *rc
int rcInTail(RecentCounter *rc, int soldier){
if(rc->cnt == MAX_QUEUE_SIZE){
return NO;
}
rc->buf[rc->tail] = soldier;
rc->tail = (rc->tail + 1) % rc->size;
rc->cnt++;
return YES;
}
//his timu func
RecentCounter* recentCounterCreate() {
RecentCounter* rc = (RecentCounter *) calloc (1, sizeof(RecentCounter));
if(rc == NULL) {
return NULL;
}
rc->size = MAX_QUEUE_SIZE;
rc->cnt = 0;
rc->head = 0;
rc->tail = 0;
return rc;
}
//his timu func
int recentCounterPing(RecentCounter* obj, int t) {
int soldier = -999;
int cnt = 0;
while(rcIsEmpty(obj) == NO) {
if(rcPeekHead(obj, &soldier) != YES) {
break;
}
if((soldier + 3000) < t) {
//tt say: soldier == 200, t == 7000;so 3200 < 7000, we need move
//tt the soldier forward to in between 4000(t-3000) and t(7000)
rcPollHead(obj, &soldier);
} else {
break;
}
}
//enqueue the 't'
rcInTail(obj, t);
cnt = obj->cnt;
return cnt;
}
//his timu func
void recentCounterFree(RecentCounter* obj) {
if(obj != NULL){
free(obj);
}
} /**
* Your RecentCounter struct will be instantiated and called as such:
* RecentCounter* obj = recentCounterCreate();
* int param_1 = recentCounterPing(obj, t); * recentCounterFree(obj);
*/ /*执行用时 :
264 ms
, 在所有 C 提交中击败了
26.67%
的用户
内存消耗 :
38.2 MB
, 在所有 C 提交中击败了
78.33%
的用户*/

✅ 942. 增减字符串匹配

https://leetcode-cn.com/problems/di-string-match

仍旧有需要思考的地方

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* diStringMatch(char * S, int* returnSize){
int left = 0;
int right = strlen(S);
int len = strlen(S);
int *A = malloc(sizeof(int) * (strlen(S) + 1)); // 谨记malloc 语法
int i = 0;
int j = 0;
for(j = 0; j < len; j++){
if(S[j] == 'I') {
A[i++] = left++;
} else {
A[i++] = right--;
}
}
A[i++] = right;//思考这里:为什么单独如此一句呢?
*returnSize = i;
return A;
} 执行用时 :
88 ms
, 在所有 C 提交中击败了
9.36%
的用户
内存消耗 :
12.9 MB
, 在所有 C 提交中击败了
44.53%
的用户

py尝试

class Solution:
def diStringMatch(self, S: str) -> List[int]:
left = 0
right = len(S)
ans = []
for i in S:
if i == 'I':
ans.append(left)
left += 1
else:
ans.append(right)
right -= 1
ans.append(right)
return ans '''
执行用时 :
68 ms
, 在所有 Python3 提交中击败了
79.74%
的用户
内存消耗 :
14.2 MB
, 在所有 Python3 提交中击败了
52.81%
的用户
'''

✅ 977. 有序数组的平方

https://leetcode-cn.com/problems/squares-of-a-sorted-array

class Solution:
def sortedSquares(self, A: List[int]) -> List[int]:
return sorted([i**2 for i in A]) '''
# for sort: you have:
list.sort(cmp=None, key=None, reverse=False)
# for sorted: you have:
sorted()方法,返回一个新的list; ################# 学习点!!!!
# summary:
如果你不需要保留原来的list, 使用list.sort()方法来排序,此时list本身将被修改。通常此方法不如sorted()方便
'''

执行用时 :

312 ms

, 在所有 Python3 提交中击败了

35.49%

的用户

内存消耗 :

15 MB

, 在所有 Python3 提交中击败了

59.04%

的用户

  • 他人语:

  • c 解答 ,两个指针(soldier) i 和 j 在原来的数组上跑
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortedSquares(int* A, int ASize, int* returnSize){
int i = 0, j = ASize - 1;
int k = ASize - 1;
int *ans = (int *)malloc(sizeof(int) * ASize);
while(i <= j) {
if (abs(A[i]) > abs(A[j])) {
ans[k] = A[i] * A[i];
k--;
i++;
} else {
ans[k] = A[j] * A[j];
k--;
j--;
}
}
*returnSize = ASize;
return ans;
}

执行用时 :

132 ms

, 在所有 C 提交中击败了

82.21%

的用户

内存消耗 :

21.5 MB

, 在所有 C 提交中击败了

41.91%

的用户

leetcode 0206的更多相关文章

  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. 解决nginx报错:nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied)

    报错描述: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied) 通过ansible远程给主机更换端口并重新启动ng ...

  2. 13.56Mhz下50欧姆阻抗匹配简易教程

    阻抗匹配(impedance matching) 主要用于传输线上,以此来达到所有高频的微波信号均能传递至负载点的目的,而且几乎不会有信号反射回来源点,从而提升能源效益.信号源内阻与所接传输线的特性阻 ...

  3. Python实现AVL树

    参考: https://www.cnblogs.com/linxiyue/p/3659448.html?utm_source=tuicool&utm_medium=referral class ...

  4. 解决安装VMware Player出错,提示安装程序无法继续,microsoft runtime dll安装程序未能完成安装

    方案一: 以兼容模式运行和管理员方式运行安装程序,右键点击安装文件选择属性,在弹出的面板中修改兼容性如下 方案二: 下载最近版的VMWare player安装包哈哈 方案三: 1.双击VMware P ...

  5. C语言随笔5:函数、函数指针

    函数 C语言中函数参数传递时,将实参的值拷贝到函数参数的存储区中.这种传递参数的方式称为按值传递. 函数不会访问实参本身,访问的是函数存储在栈区的副本,不会改变实参.函数凋用结束,函数在栈区的内容释放 ...

  6. 实现手写体 mnist 数据集的识别任务

    实现手写体 mnist 数据集的识别任务,共分为三个模块文件,分别是描述网络结构的前向传播过程文件(mnist_forward.py). 描述网络参数优化方法的反向传播 过 程 文件 ( mnist_ ...

  7. 计算几何-Graham法-凸包

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 关键一句话 Cr ...

  8. FloatingActionButton 实现类似 闲鱼 App 底部导航凸起按钮

    一.Flutter FloatingActionButton 介绍 FloatingActionButton 简称 FAB,可以实现浮动按钮,也可以实现类似闲鱼 app 的地步凸起导航   child ...

  9. token是个什么东西?怎样生成并携带token

    什么是token及怎样生成token  转载自:https://www.cnblogs.com/lufeiludaima/p/pz20190203.html 什么是token Token是服务端生成的 ...

  10. 每日扫盲(四):java之Netty原理和使用

    转自:https://www.jdon.com/concurrent/netty.html Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Nett ...