![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 55
木妖喵
这个作者很懒,什么都没留下…
展开
-
Delete Node in a Linked List
题目是这样的: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with原创 2015-07-27 15:52:17 · 314 阅读 · 0 评论 -
[LeetCode][初级数组][树] 50 二叉树的层级遍历
本题使用广度优先遍历的思路,可以轻松用实现出来(不用c的话)广度优先遍历,会用到队列。首先把根节点放进队列,然后以队列不为空为条件,每从队首取出一个节点,就把它的子节点加入队尾,这样就实现了广度优先遍历。* 如果采用先进后出的策略,则是深入优先遍历。# Definition for a binary tree node.# class TreeNode# attr_accessor :...原创 2018-05-20 17:01:43 · 143 阅读 · 0 评论 -
[LeetCode][初级算法][字符串] 32-37
32 反转字符串没啥好说的:char* reverseString(char* s) { int i=0; int len = strlen(s); int j = len-1; while(i<j){ s[i]=s[i]^s[j]; s[j]=s[i]^s[j]; s[i]=s[i]^s[j]; ...原创 2018-05-15 20:10:25 · 166 阅读 · 0 评论 -
[LeetCode][初级算法][树] 51 将有序数组转换为二叉搜索树
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/7/trees/51/将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。转化成平衡二叉搜索树,也就是说树的每个节点 的左右两个子树的高度差的绝对值不超过 1。因为数组是有序的,而BST的性质是左子树所有节点小于根,右子树所有节点大于根,所...原创 2018-05-26 23:39:36 · 196 阅读 · 0 评论 -
[LeetCode][初级算法][链表]43反转链表
要求用两种方法,迭代和递归。迭代方法:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* reverseList(struct ListNode* head) { ...原创 2018-05-16 18:00:11 · 118 阅读 · 0 评论 -
[LeetCode][初级算法][链表]44 合并两个有序链表
这道题想要提高效率,必须考虑到的一点是,当一其中一个链表空了的时候,可以直接把另一个链表剩余部分整个拼上去,不需要一个一个遍历。 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode*...原创 2018-05-16 18:38:38 · 107 阅读 · 0 评论 -
[LeetCode][算法初级][链表]45 回文链表
这道题反而递归的算法比较容易想到了,首先要先递归到链表的末尾,再递归返回时与链表头的元素开始比较。感觉还有优化的空间,比如当tail和front交汇时停止比较?但是反而递归的算法不太容易操作../** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode ...原创 2018-05-16 19:12:33 · 180 阅读 · 1 评论 -
[LeetCode][初级算法][动态规划] 爬楼梯
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/54/这道题使用穷举的方式妥妥的超过时间限制,而使用动态规划则效率很高了。主要可以这样理解,因为我们一次只能跨一步或两步,那么设走第i步有f(i)种方式,就有f(i) = f(i-1)+f(i-2)。由...原创 2018-06-22 14:14:00 · 196 阅读 · 0 评论 -
[LeetCode][初级算法][排序] 合并两个有序数组
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/8/sorting-and-searching/52/乍一看像是归并排序,但是从题目来看明显是可以使用原地算法的。使用额外数组的方法很简单:void merge(int* nums1, int m, int* nums2, int n) { ...原创 2018-06-19 14:24:57 · 213 阅读 · 0 评论 -
[LeetCode][初级算法][动态规划]买卖股票的最佳时机
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/55/ 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票...原创 2018-07-02 11:49:57 · 297 阅读 · 0 评论 -
[LeetCode][简单算法][动态规划] 最大子序和
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/56/ Given an integer array nums, find the contiguous subarray (containing at least one number) wh...原创 2018-08-31 16:04:21 · 97 阅读 · 0 评论 -
[LeetCode][算法初级][链表] 42 删除链表的倒数第n个节点
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/6/linked-list/42/题目中要求只遍历一次,我的思路是,想象一根长度为n的绳子,先把一头定在head,然后遍历,当超出了长度n的时候,把固定的head的那端向后拉扯。这样,当遍历完了的时候,另一端正好停留在倒数第n项上。考虑到有可能要删...原创 2018-05-13 21:00:36 · 130 阅读 · 0 评论 -
[LeetCode][算法初级][字符串]40 最长共同前缀
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/5/strings/40/思路是很简单的,就是逐个比较。但是我的想法是,先两两比较,然后把得到的前缀再和后面的比较,全部比较一遍以后就得到了最大前缀了。int lcp(char *str1, char* str2, int size){ i...原创 2018-05-13 18:14:30 · 133 阅读 · 0 评论 -
[LeetCode][算法初级] [数组] 21-24
1 从排序数组中删除重复元素https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/21/维护两个下标,一个代表处理完成的数组的末尾,一个代表正在被处理的下标。遍历数组,将不重复的数字顺次放进数组前部即可。int removeDuplicates(int* nums, int numsS...原创 2018-05-09 14:25:03 · 131 阅读 · 0 评论 -
[LeetCode][算法初级][数组] 25.只出现一次的数字
25 只出现一次的数字https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/25/用到异或去重的方法。异或(^)运算有如下性质:交换率 a^b = b^a结合律 a^b^c = a^(b^c)a^a=0; a^0=a; a^(-1)=~a;去重利用的当然是a^a=0,a^0=a这2条性...原创 2018-05-09 15:32:45 · 92 阅读 · 0 评论 -
[LeetCode][算法初级][数组]26 两个数组的交集II
26 两个数组的交集IIhttps://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/26/我的原始思路是先排序,再依次比较。对两个数组交替递增下标进行遍历,复杂度是O(n)。因为是已经排序过的数组,只要将相对较小的数组元素的下标递增,直到找到相同元素,或超过另一数组的当前元素,再递增另一数组...原创 2018-05-09 16:37:20 · 133 阅读 · 0 评论 -
[LeetCode][算法初级][数组] 27-29
27 加一https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/27/很简单,数组从最后往前遍历,记录进位的情况,当不需要进位或结束时退出。结束后最后如果还需要进位,则在数组最前插入1.# @param {Integer[]} digits# @return {Integer[]}...原创 2018-05-10 17:53:06 · 80 阅读 · 0 评论 -
[LeetCode][算法初级][数组] 30 有效的数独
30 有效的数独https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/30/写一下题目吧判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3...原创 2018-05-10 18:09:53 · 223 阅读 · 0 评论 -
[LeetCode][算法初级][数组] 31 旋转图像
31 旋转图像https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/31/这个题目牵扯到了矩阵的性质了(?)数学不好真的很虚啊!!总之,虽然不是我想出来的,可以发现,旋转90度等于先把矩阵转置,再把每行置逆。按照这个思路就好操作了。转置就是以对角线为轴翻转,也就是 [i,j] [j,i...原创 2018-05-10 18:17:31 · 87 阅读 · 0 评论 -
[LeetCode][初级算法][排序和搜索]53 第一个错误的版本
就是简单的二分法查找,只不过结束条件不是命中,而是知道了交界点。// Forward declaration of isBadVersion API.bool isBadVersion(int version);int firstBadVersion(int n) { long i = 1; long j = n; long m; while(j-i>0...原创 2018-05-17 11:40:51 · 208 阅读 · 0 评论 -
[LeetCode][初级算法][链表]46 环形链表
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/6/linked-list/46/这题不要求返回环的入口,算是比较初级的题,然而我自己还是没能想出来。判断是否成环,主要是采用了“相遇法”。也就是两个指针同时从head遍历,只不过一个步长为1,一个步长为2,如果成环,则快的那个总会在环中与慢的那个...原创 2018-05-17 16:06:37 · 123 阅读 · 0 评论 -
[LeetCode][算法初级][字符串]38 实现strStr()
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/5/strings/38/也就是一个查找子串的问题,思路很容易想到,从两个字符串相同的第一个字符开始往后比较,如果遇到不同的字符则结束本次比较,将大字符串的指针后移,并重复此过程。我一开始的实现是这样的:int strStr(char* hayst...原创 2018-05-13 18:01:15 · 109 阅读 · 0 评论 -
[LeetCode][算法初级][字符串]39 数数并说 (count and say)
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/5/strings/39/题目挺奇怪的,看了一会才知道是什么意思。。就是让你去数一个字符串的数字,并且把你说出来的内容作为下一个字符串继续数。比如:第一个字符串是“1”,你就要数:"1个1!",同时写下”11“作为下一个字符串。然后你再数”2个1!...原创 2018-05-13 18:08:18 · 171 阅读 · 0 评论 -
[LeetCode][初级算法][动态规划] 打家劫舍
https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/57/你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定...原创 2018-09-03 13:43:53 · 168 阅读 · 0 评论