【LeetCode 21. 合并两个有序链表(C#实现)——链表,递归,迭代】教程文章相关的互联网学习教程文章

LeetCode 数组转二叉树 C#【代码】

把LeetCode二叉树题目的测试数组,转换成二叉树classTreeNode{publicint val;publicTreeNode left;publicTreeNode right;publicTreeNode(int x){ val = x;}}classTree{publicstaticTreeNodeCreateNode(int? val){if(val ==null)returnnull;returnnewTreeNode((int)val);}publicstaticTreeNodeCreateTree(int?[] arr){if(arr.Length<=0|| arr[0]==null){returnnull;}TreeNode root =Tree.CreateNode(arr[0]);Queue<TreeNode>queue=n...

LeetCode #3. Longest Substring Without Repeating Characters C#【代码】

Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the length of 1.Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. Solution:Use two-pointer and HashTable to s...

LeetCode算法题-C#代码实现-栈(一)【代码】

20. Valid Parentheses有效的括号解题思路将括号比较后者后,不同的入栈,相同的出栈,最后字符串遍历结束后栈为空则匹配成功。publicbool IsValid(string s) {//声明字典,括号匹配键值对Dictionary<char, char> dict = new Dictionary<char, char>();dict.Add(‘)‘, ‘(‘);dict.Add(‘]‘, ‘[‘);dict.Add(‘}‘, ‘{‘);Stack<char> stack = new Stack<char>();//遍历字符s,直到遍历s所有字符结束循环for (int i = 0; i < s...

C#解leetcode 152. Maximum Product Subarray【代码】

Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 分析:这个题目就是让你求连续的子数组的乘积的最大值。(1)在数组中没有0的情况下,最大值要么是nums[0]+nums[1]+。。。。+nums[i]或者是nums[j]+nums[j+1]+。。。。+nums[n-1]. 当数组中全是整数的时...

LeetCode 280. Wiggle Sort C#【代码】

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. Solution:Loop through, when odd index num should be greater then the previous num; when even index num should be smaller then the previous num;So if even index num is greater then the p...

LeetCode 33. 搜索旋转排序数组(C#实现)——二分查找【代码】

问题:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/submissions/假设按照升序排序的数组在预先未知的某个点上进行了旋转。( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。你可以假设数组中不存在重复的元素。你的算法时间复杂度必须是 O(log n) 级别。示例 1:输入: nums = [4,5,6,7,0,1,2], target = 0 输出: 4...

[C#]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable【代码】

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note:You may assume that the array does not change. There are many calls to sumRange function.给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。 示例:...

leetcode2.两数相加【c#】问号的用法。【图】

题: 题解: 设置进位值num,将每次l1,l2,num三个值加起来为mid,位数为mid%10,大于10则进位为mid/10。 思路很清晰,没什么好说的。 这里记录一下c#中问号的用法。 ? 可空类型修饰符,使值类型也可为空。 a??b 空合并运算符。当a为null时则返回b,a不为null时则返回a本身。 ?. NULL检查运算符。如果对象为NULL,则不进行后面的获取成员的运算,直接返回NULL。在这里判断l1,l2是否为空,用于解决l1、l2链表长度不一样的情况。

LeetCode:对称二叉树(c#)【代码】

题目内容给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/ \2 2/ \ / \ 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/ \2 2\ \3 3说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。实现方式一(递归实现) /*** Definition for a binary tree node.* public class TreeNode {* public int val;* public TreeNode left;* public Tree...

LeetCode 21. 合并两个有序链表(C#实现)——链表,递归,迭代【代码】

一、问题 https://leetcode-cn.com/problems/merge-two-sorted-lists/将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例:输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4二、GitHub实现:https://github.com/JonathanZxxxx/LeetCode/blob/master/MergeTwoListsClass.csBlog:https://www.cnblogs.com/zxxxx/ 三、思路  1、递归:判断两个链表的头元素大小,递归的决定下一个...

leetcode26 删除排序数组中的重复项(C#)【代码】

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

一道水题:LeetCode(无重复字符的最长子串)C#版

题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是...

978. 最长湍流子数组--leetcode --c#

题目描述: 当 A 的子数组 A[i], A[i+1], ..., A[j] 满足下列条件时,我们称其为湍流子数组: 若 i <= k < j,当 k 为奇数时, A[k] > A[k+1],且当 k 为偶数时,A[k] < A[k+1]; 或 若 i <= k < j,当 k 为偶数时,A[k] > A[k+1] ,且当 k 为奇数时, A[k] < A[k+1]。 也就是说,如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是湍流子数组。 返回 A 的最大湍流子数组的长度。 示例 1: 输入:[9,4,2,10,7,...

一道水题:LeetCode(最大子序和)C#版

题目: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转...

C#leetcode刷题929独特的电子邮件地址【代码】

题目描述 每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔。 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名。 除了小写字母,这些电子邮件还可能包含 ',' 或 '+'。 如果在电子邮件地址的本地名称部分中的某些字符之间添加句点('.'),则发往那里的邮件将会转发到本地名称中没有点的同一地址。例如,"alice.z@leetcode.com” 和 “alicez@leetcode.com” 会转发到同一电子邮件地址。 (请...