自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(37)
  • 收藏
  • 关注

原创 Leetcode 1042. 不领接植花

题目详情解题思路代码实现(C#)

2020-06-30 18:13:49 132

原创 Leetcode 997.找到小镇的法官

题目详情解题思路图类问题首先考虑出入度 建立两个int[] 一个入度一个出度 找入度=N-1和出度=0的位置 但是题目给的点都从1开始 而不是按照数组特征从0开始 所以要注意代码实现public class Solution { public int FindJudge(int N, int[][] trust) { int[] in_degree = new int[N]; int[] out_degree = new int[N]; i

2020-06-30 17:39:18 124

原创 Leetcode190.颠倒二进制位

题目详情解题思路(1)异或运算(2)取模求和代码实现(异或运算 C#)public class Solution { public uint reverseBits(uint n) { uint res = 0; for (int i = 0; i <= 31; i++) { res ^= (n & (1U << i)) != 0 ? 1U << (31 - i) : 0;

2020-06-30 14:32:36 130

原创 Leetcode 239. 滑动窗口最大值

题目详情算法描述(暴力法)可以 但是超出时间限制(动态规划)(双向队列)解题思路(C#)(动态规划)class Solution { public int[] maxSlidingWindow(int[] nums, int k) { int n = nums.length; if (n * k == 0) return new int[0]; if (k == 1) return nums; int [] left = new int[n];

2020-06-29 20:21:49 84

原创 Leetcode 641.设计循环双端队列

题目详情解题思路因为queue不能头插,而list插入方式自由,所以可用list实现代码实现(C#)(List 实现)public class MyCircularDeque { List<int> lst ; int len; /** Initialize your data structure here. Set the size of the deque to be k. */ public MyCircularDeque(int k) {

2020-06-29 18:39:36 96

原创 Leetcode 210. 课程表II

题目描述代码实现(C#)

2020-06-28 11:09:14 67

原创 Leetcode207. 课程表

题目详情解题思路代码实现(C#) public class Solution { public bool CanFinish(int numCourses, int[][] prerequisites) { //这道题邻接表用List<List<int>>表示 eg.{{2,3},{1,3},{1,2}}表示第一个结点通往2和3 第二个结点通往1和3 第三个结点通往1和2 //入度表用一维数组表示 位置代表结点位置 每位的值代

2020-06-27 21:59:15 147

原创 Leetcode 121.买卖股票的最佳时机I

题目详情解题思路下一个min要么覆盖掉原来的min 要么此时已经有了max 之后只要对比max就知道哪个最大代码实现(C#)public class Solution { public int MaxProfit(int[] prices) { //这道题目的特点是 下一个min要么覆盖掉原来的min 要么此时已经有了max 之后只要对比max就知道哪个最大 int min=int.MaxValue; int max=0; for(int

2020-06-24 11:41:02 60

原创 Leetcode 78.子集

题目描述解题思路方法三:字典排序(二进制排序) 子集 思路该方法思路来自于 Donald E. Knuth。将每个子集映射到长度为 n 的位掩码中,其中第 i 位掩码 nums[i] 为 1,表示第 i 个元素在子集中;如果第 i 位掩码nums[i] 为 0,表示第 i 个元素不在子集中。例如,位掩码 0…00(全 0)表示空子集,位掩码 1…11(全 1)表示输入数组 nums。因此要生成所有子集,只需要生成从 0…00 到 1…11 的所有 n 位掩码。乍看起来生成二进制数很简单

2020-06-23 12:01:52 118

原创 Leetcode 136.只出现一次的数字

题目描述解题思路(用Dictionary)用key-value键值对存储数字和出现的次数。⭐(位运算)代码实现(Dictionary)public class Solution { public int SingleNumber(int[] nums) { Dictionary<int,int> dic = new Dictionary<int,int>(); for(int i=0;i<nums.

2020-06-23 10:43:06 112

原创 Leetcode 344.反转字符串

题目详情解题思路(双指针)直接使用双指针或者双指针+递归代码实现(C#)public class Solution { public void ReverseString(char[] s) { int left=0; int right=s.Length-1; while(left<right) { char a=s[left]; s[left++]

2020-06-22 13:08:18 269

原创 Leetcode14. 最长公共前缀

题目详情解题思路每次只遍历每个元素的一位字符。直到所有字符不一样为止。

2020-05-17 11:39:08 69

原创 Leetcode7. 整数反转

题目详情解题思路要注意的一点是 反转过来的数字溢出的可能(如何判断)溢出条件有两个,一个是大于整数最大值MAX_VALUE,另一个是小于整数最小值MIN_VALUE,设当前计算结果为ans,下一位为pop。从ans * 10 + pop > MAX_VALUE这个溢出条件来看 当出现 ans > MAX_VALUE / 10 且还有pop需要添加 时,则一定溢出 当出现 ans == MAX_VALUE / 10 且 pop > 7 时,则一定溢出,7是2^31 -1的个

2020-05-17 11:03:18 69

原创 Leetcode122. 买卖股票的最佳时机II

题目描述解题思路(贪心法)只要后天价格大于前天,都把差价算入利润中。(动态规划法)未参透代码实现(C#)public class Solution { public int MaxProfit(int[] prices) { int val; int i; int res=0; for(i=0;i<prices.Length-1;i++) { val=prices[i+1]-price

2020-05-13 20:35:19 82

原创 Leetcode236. 二叉树附近的公共祖先

public class Solution { public TreeNode res; public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { IsAncestor(root, p, q); return res; ...

2020-05-07 22:52:56 68

原创 Leetcode104. 二叉树的最大深度

思路运用bfs的方法得出最大深度class Solution {public: int maxDepth(TreeNode* root) { queue<TreeNode*> q; int ans = 0; if (root != nullptr) q.push(root); while (!q.empty...

2020-05-07 22:47:43 58

原创 Leetcode101.对称二叉树

根据100题:判断二叉树是否相同。既然要判断二叉树是否对称,只要二叉树的左子树和右子树对称即可。就是说在遍历的时候,遍历左子树的所访问的节点和遍历右子树所访问的节点是一样的class Solution {public: bool isSymmetric(TreeNode* root) { if(root==NULL) return true; retu...

2020-04-27 20:27:15 94

原创 Leetcode100. 相同的树

递归法终止条件:只要有一个节点为NULL就停止,之后判断是不是只有一个节点为空,class Solution {public: bool isSameTree(TreeNode* p, TreeNode* q) { //递归终止条件, 又一个为空 if (p == NULL || q == NULL){ if ( p != N...

2020-04-27 20:23:40 70

原创 LeetCode 89.格雷编码

class Solution {public: vector<int> grayCode(int n) { if (n < 0) { return vector<int> (); } vector<int> res(1,0); int val = 1; ...

2020-04-21 14:56:15 67

原创 LeetCode 78.子集

class Solution {public: vector<vector<int>> ans; vector<int> tmp; void find(int dep, vector<int>& nums) { if(dep <= 0) { a...

2020-04-21 14:54:25 71

原创 Leetcode137.只出现一次的数字II

class Solution {public: int singleNumber(vector<int>& nums) { int ans = 0; for (int i = 0; i < 32; i++) { int sum = 0; for (int nu...

2020-04-21 14:37:57 73

原创 Leetcode 136.只出现一次的数字

class Solution { public int singleNumber(int[] nums) { Map<Integer, Integer> map = new HashMap<>(); for (Integer i : nums) { Integer count = map.get(i); ...

2020-04-13 19:42:06 51

原创 Leetcode 3. 无重复字符的最长子串

class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s:return 0 left = 0 lookup = set() n = len(s) max_len = 0 cur_len = 0...

2020-04-13 19:39:32 58

原创 leetcode239.

class Solution {public: deque<int> dep; void clean_queue(int idx,int i,vector<int>& nums){ if(!dep.empty()&&idx==dep.front())dep.pop_front(); while(!d...

2020-03-30 20:02:07 124

原创 Leetcode 641. 设计循环双端队列

class MyCircularDeque {public: /** Initialize your data structure here. Set the size of the deque to be k. */ MyCircularDeque(int k) { m_head = 0; m_tail = 0; m_size ...

2020-03-30 19:59:17 135

原创 Leetcode 20. 有效的括号

class Solution: def isValid(self, s: str) -> bool: dic = {'{': '}', '[': ']', '(': ')', '?': '?'} stack = ['?'] for c in s: if c in dic: stack.append(c) ...

2020-03-23 17:43:44 76

原创 Leetcode 155.最小栈

class MinStack: def __init__(self): self.data = [] self.helper = [] def push(self, x): self.data.append(x) if len(self.helper) == 0 or x <= self.helper[-1...

2020-03-23 17:40:34 55

原创 Leetcode ——150.逆波兰表达式

int charToInt(char* str){ int i=str[0]=='-'?1:0,num=0; while(str[i]) num=num*10+str[i++]-'0'; return str[0]=='-'?-num:num;}int evalRPN(char ** tokens, int tokensSize){ int ...

2020-03-23 17:37:19 77

原创 Leetcode 21.——合并两个有序链表

题目详情class Solution: def mergeTwoLists(self, l1, l2): if l1 is None: return l2 elif l2 is None: return l1 elif l1.val < l2.val: l1.n...

2020-03-16 18:12:56 71

原创 Leetcode 83.——删除排序链表中的重复元素

题目详情class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if not (head and head.next): return head i,j = head,head while j: ...

2020-03-16 18:11:14 80

原创 Leetcode —141.环形链表

题目详情public boolean hasCycle(ListNode head) { Set<ListNode> nodesSeen = new HashSet<>(); while (head != null) { if (nodesSeen.contains(head)) { return true; ...

2020-03-16 18:08:07 81

原创 游戏:猜数字

题目如下功能要求生成随机数判断模块判断大小结果输出由于这个实验C++课做过了,想加点新鲜的东西1 .try…catch 抛出异常2.用实例化对象方法实现(简直毫无区别)代码实现 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Co...

2020-03-09 20:16:14 147

原创 LeetCode 53.——最大子序和

题目详情给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。解题思路动态规划法(Dynamic Programming):...

2020-03-02 21:52:49 84

原创 LeetCode 88.合并两个有序数组

题目详情给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。示例:输入:nums1 = [1,2,3,0,0,0], m = 3nums2 ...

2020-03-02 20:04:38 146

原创 LeetCode——1. 两数之和

题目给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]思路(暴力法)...

2020-02-23 13:44:42 56

原创 LeetCode——27. 移除元素

题目给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。示例 1:给定 nums = [3,2,2,3], val = 3,函数应该返回新的长度 2, 并且 nums 中的前两个元...

2020-02-20 23:30:50 111

原创 Leetcode 26. 删除排序数组中重复项

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。示例 1:给定数组 nums = [1,1,2],函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。你不需要考虑数组中超出新长度后面的元素。示例 2:给定 nums ...

2020-02-20 22:09:16 96

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除