
c#
今晚早睡QAQ
这个作者很懒,什么都没留下…
展开
-
C# 四个字节十六进制转单精度浮点数
16转单精度原创 2022-07-29 17:58:25 · 2018 阅读 · 0 评论 -
位运算(让你的代码逼格更高)
1.判断奇偶性与运算:任一数与1求&,则结果为该数二进制的最后一位与1求&,结果为1则为奇数。if(n & 1 == 1){}原创 2021-11-06 13:09:59 · 182 阅读 · 0 评论 -
11.1 串口接受数据问题
问题点①如果串口通讯的数据很长,在进入串口回调函数时,只是刚开始收到前几位数据,如果此时去读取,数据读取会不完整。②如果接受完数据,没有做丢弃串口缓存区数据,会造成上次数据的未接受的部分,出现在本次接受数据的头部优化点①进入回调时,增加延时;(延时时间为串口波特率/8)②增加数据长度判断;(判断本次数据是否为既定的通讯长度)③增加串口缓存区清除步骤。(每次数据处理完成后,将接受缓存区清空,避免数据堆叠) Delay(9600/8); //①***********延时必须要加,延时时间为原创 2021-11-01 13:25:16 · 1056 阅读 · 0 评论 -
leetcode.453. 最小操作次数使数组元素相等(1024提醒自己)
题目描述解题1024写给自己:熟能生巧题目描述解题public class Solution { public int MinMoves(int[] nums) { int min = nums.Min(); int res = 0; foreach(int num in nums) { res += num - min; } return res; .原创 2021-10-24 21:04:56 · 109 阅读 · 0 评论 -
C# 数据结构(未完)
数组二维数组排序列表(List)链表字典字典排序哈希表数组二维数组排序int[][] nums= new int[][] { new int[]{ 0, 8 }, new int[]{ 2, 4 } };Array.Sort(nums, (x, y) => x[0].CompareTo(y[0])); //按每个一维数组的 第一个元素排序(0)Array.Sort(nums, (a, b) => a[0] - b[0]); Array.Sort(nums, (a, b) =&.原创 2021-07-22 17:20:10 · 623 阅读 · 1 评论 -
C#截取指定数组元素区间
int[] nums = { 2, 5, 1, 2, 5 };int[] nums1 = nums.Skip(s).Take(e).ToArray(); // s:开始下标; e:结束下标原创 2021-07-14 16:45:51 · 2861 阅读 · 0 评论 -
leetcode.1418 点菜展示表(哈希表,有序集合,列表排序,字典排序)
题目描述解题知识点1.HashSet2.字典排序3.链表排序题目描述解题看到题的时候就想到了哈希表,以及要排序的问题。public class Solution { public IList<IList<string>> DisplayTable(IList<IList<string>> orders) { ISet<string> Ttems = new HashSet<string>(); //元.原创 2021-07-06 14:02:51 · 229 阅读 · 0 评论 -
C# Dictionary分别按Key,Value值升降序排序
Dictionary<char, int> dic = new Dictionary<char, int>(); dic.Add('a', 1); dic.Add('b', 2); dic.Add('c', 1); dic.Add('d', 3); dic.Add('e', 8);结果:①dicsort1 :按key值升序Dictionary<char, int> dicsort1 = dic.OrderBy(o => o.Key).ToDict.原创 2021-07-05 17:08:17 · 2883 阅读 · 0 评论 -
二进制中1的个数(去1算法:n&(n-1))
题目描述解题public class Solution { public int HammingWeight(uint n) { int sum = 0; while(n!=0) { n &= n-1; //n&(n-1)可消除最右端的一个1 n: 1000 n-1:0111 n&(n-1): 0000 sum++;原创 2021-06-25 16:54:16 · 121 阅读 · 0 评论 -
gcd()求最大公约数,辗转相除法的实现
public int gcd(int a, int b) //求最大公约数 { return b==0?a:gcd(b,a%b); //b为0则无余数,除尽 }原创 2021-06-25 16:26:22 · 104 阅读 · 0 评论 -
leetcode.752 打开转盘锁(BFS,Queue,HashSet,IList,list)
题目描述解题知道是迷宫问题,但无奈技术不够,只能看过官方解释后,自行敲代码测试;按着官方思路写代码,确实出现了一些问题,故记录一下心得。先贴代码:(注释部分为自己写的代码,提交时超时)public class Solution { public int OpenLock(string[] deadends, string target) { if(target.Equals("0000")) { return 0;原创 2021-06-25 14:13:20 · 261 阅读 · 1 评论 -
c# BitCount()(将输入的十进制数转为二进制,并计算其1的数量)
private static int BitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i +原创 2021-06-24 14:55:14 · 444 阅读 · 0 评论 -
leetcode.692 前K个高频单词
题目描述解题方法一:遍历给定的数组,每次循环中判断该循环中元素是否存在数组中,存在则个数+1,并删除该元素,选出出现次数最多的K个。方法二:创建字典,遍历给定数组,设置数组元素为key,个数为值value;字典存在循环元素则value+1,不存在则添加;选出出现次数最多的K个。注:个数相同要根据首字母排序,则需在开始时对给定字符数组进行一次排序。代码方法一:public IList<string> TopKFrequent(string[] words, int k) {原创 2021-05-20 14:37:51 · 115 阅读 · 0 评论 -
小白教程:DotNetBar在Winform中的使用
1.下载地址DotNetBar(1个积分意思一下 Thanks♪(・ω・)ノ)2.项目引用在你的Winform项目中引用下图中两个dll这两个dll在你的DotNetBar安装目录里3.工具箱添加控件在DotNetBar安装目录下将该dll直接鼠标拖入工具箱即可4.项目应用DotNetBar主题①在工具箱中搜索styleManager鼠标拖入窗口空白处②点击styleManager在属性中切换风格即可③项目的.cs中引用dll,然后将项目接口改为OfficeForm④运行原创 2021-04-29 14:42:31 · 1896 阅读 · 0 评论 -
leetcode.861 翻转矩阵后的得分
题目描述解题贪心算法,每行二进制求最大值。①需保证每行第一个元素为1②在每行首元素为1的情况下保证除第一列以外,从左到右的每列1数量尽可能多**Tip:**不需要去翻转矩阵,我刚开始新建一个数组来存放每行是否需要翻转,后看别人代码发现只需要在求每列1数量时对行首元素进行判断即可代码public class Solution { public int MatrixScore(int[][] A) { int m = A.Length; i原创 2020-12-07 17:34:05 · 115 阅读 · 0 评论 -
leetcode.204 计数质数
目录题目描述解题暴力解法(枚举)厄拉多塞筛法题目描述解题暴力解法(枚举)遍历到n的所有正整数,对比自己小的所有的数求余,有一个余数为0,则为合数;static int CountPrimes(int n) { if (n <= 1) return 0; int nums = 0; int flag = 1; for (int i = 2; i < n; i++)原创 2020-12-03 15:47:53 · 124 阅读 · 0 评论 -
力扣(二十四) 两两交换链表中的节点(递归)
题目描述解题最关键点:节点交换顺序。①首先指向第二节点;②接着指向第一节点 or 第一节点指向后续节点(√)若先指向第一节点 ,现有节点无法指向第三节点及以后节点故 应先给第一节点指向第二节点之后节点 再将第二节点指向第一③第二节点指向第一节点④将链表位置更新到未置换位置public class Solution { public ListNode SwapPairs(ListNode head) { if(head==null || head.next==nu原创 2020-08-29 16:29:44 · 164 阅读 · 0 评论 -
力扣(二十) 有效的括号(栈)
题目描述解题思路:①利用栈先进后出的特点;②从左往右遍历,若为左括号则入栈;③若为右括号则与栈顶元素匹配,匹配则继续,不匹配则falsepublic bool IsValid(string s) { Stack<char> Valid = new Stack<char>(); List<char> s1 = new List<char>(s); Dictionary<c原创 2020-06-23 17:38:04 · 202 阅读 · 0 评论 -
力扣(十九)删除链表的倒数第N个节点(快慢指针)
题目描述引入:链表 public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } }①ListNode的head为0则为空链表;②ListNode.next指向下一节点的地址;③ListNode.val当前节点地址的值;解题public class Solution { public ListNode Remo原创 2020-06-23 16:16:28 · 201 阅读 · 0 评论 -
力扣(十八) 四数之和(双指针)
题目描述解题思路:①与之前三数之和类似的题目,多一位则多做一步循环;②双指针法,for循环中采用首尾两位循环,在三数之和基数上在尾端添加新一位;③首位for循环,四数为首(i),首+1(L),尾巴(RR),尾-1(RL),比较和与target的大小,改变L与RL位置,完成后当前I与RR的所有组合后,先改变RR位置,再次循环L与RL;知道当前i的所有组合遍历完成,改变i的值再次遍历。public class Solution { public IList<IList<int&原创 2020-06-23 11:08:39 · 169 阅读 · 0 评论 -
力扣(十七) 电话号码的字母组合(回溯算法)
题目描述引入:回溯回溯算法模板:result = []def backtrack(路径, 选择列表): if 满足结束条件: result.add(路径) return for 选择 in 选择列表: 做选择 backtrack(路径, 选择列表) 撤销选择算法核心:1、路径:也就是已经做出的选择。2、选择列表:也就是你当前可以做的选择。3、结束条件:也就是到达决策树底层,无法再做选原创 2020-06-22 12:22:05 · 353 阅读 · 0 评论 -
力扣(十六) 最接近的三数之和
题目描述解题同上一题,改变判断标准即可。public int ThreeSumClosest(int[] nums, int target) { int result = nums[0] + nums[1] + nums[2]; int distance = Math.Abs(target - result); Array.Sort(nums); for (int i = 0; i < nums.Length - 2原创 2020-06-22 11:12:45 · 211 阅读 · 0 评论 -
力扣(十五) 三数之和
题目描述解答思路:①三重循环 想想就好了;②之前做十一题的时候用到双指针;③单循环+双指针;④去重:可将输入排序,在实现去重的基础上,也可提高单循环效率(当最小数大于0时不存在三数合为0)public IList<IList<int>> ThreeSum(int[] nums) {IList<IList<int>> results = new List<IList<int>>(); Arra原创 2020-06-19 17:30:11 · 133 阅读 · 0 评论 -
力扣(十三) 罗马数字转整数(字典)
题目描述解题1、我的解题public int RomanToInt(string s) { int num = 0; int[] n = new int[] { 900, 400 , 90, 40, 9, 4, 1000, 500, 100, 50, 10, 5, 1 }; string[] Ro = new[] { "CM", "CD", "XC", "XL", "IX", "IV", "M", "D", "C", "L",原创 2020-06-18 15:13:54 · 149 阅读 · 0 评论 -
力扣(十二) 整数转罗马数字(贪心算法)
题目描述解题思路:①用给的数去除每个罗马字母代表的数,能除到则表示有一个 该字符②需考虑到6中特殊情况“IV”、“IX”、“XL”、“XC”、“CD”、“CM”public string IntToRoman(int num) { int[] n = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1}; int[] R = new int[n.Length]; string[原创 2020-06-18 12:37:18 · 283 阅读 · 0 评论 -
力扣(十一)盛最多水的容器(双指针)
题目描述解题思路:实质则求矩形面积,长为两线i的差值,宽为两线较短线的长(装水:木桶效应)。public class Solution { public int MaxArea(int[] height) { int s = 0; for (int i = 0; i < height.Length ; i++) { for (int j = i+1 ; j < height.L原创 2020-06-17 16:58:33 · 159 阅读 · 0 评论 -
力扣(十)正则表达式匹配
题目描述我直接用了正则表达式的算法,不提也罢。官方解题public class Solution { public bool IsMatch(string s, string p) { bool[,] result = new bool[s.Length+1,p.Length+1]; result[s.Length, p.Length] = true; for (int i = s.Length; i >= 0; i原创 2020-06-17 15:39:33 · 460 阅读 · 0 评论 -
力扣(九)回文数(正则表达式)
题目描述解题1、小于0则有-号,肯定不是回文数;2、根据回文数特性,求该数的倒序数,与原数比较。public class Solution { public bool IsPalindrome(int x) { int b = 0; char[] str = x.ToString().ToCharArray(); if (x<0) { return false;原创 2020-06-16 13:34:24 · 425 阅读 · 0 评论 -
力扣(八)字符串转换整数 (atoi)
题目描述大佬解题(+代码解释)using System;using System.Text.RegularExpressions;public class Solution {public int MyAtoi(string str) {str = Regex.Match(str.Trim(), @"^[+-]?\d+").ToString();① bool t_IsParse = int.TryParse(str, out int o_Num);② if (t_IsPar原创 2020-06-12 16:41:01 · 221 阅读 · 0 评论 -
(int)、int.Parse()、int.TryParse()、Convert.ToInt32()
1、(int)和int.Parse(string)不接受NULL;2、 int.Parse(string) 和 Convert.ToInt32() 接受32位 超出则越界报错;3、 int.Parse(string) 和 Convert.ToInt32() 接受的字符串转化为int不能为浮点型;(换为double则可用)...原创 2020-06-12 14:21:42 · 166 阅读 · 0 评论 -
力扣(七)整数反转
题目描述我解题:static int reverse( int x) { try { string In = Math.Abs(x).ToString(); int l = In.Length; string str =""; ...原创 2020-04-28 17:36:55 · 281 阅读 · 0 评论 -
力扣(一) 两数之和
题目描述菜鸟(我)解决思路求数组中两数之和为指定值并返回两数的数组下标。菜鸟首先想到的就是两个for循环遍历数组,if判断两个数和是否为指定值,是则输出下标。for(int j=0;j<nums.length;j++)for(int k=j+1;k<nums.length;k++)if(nums[j]+nums[k]==target)大概就是这个样子,编译运行后再对其进行...原创 2019-09-05 12:58:49 · 168 阅读 · 0 评论 -
力扣(六) Z 字形变换
题目描述菜鸟思路①创建一个二维数组,用二维数组来表示Z型阵列②到达最末行或首行则改变二维数组一位的方向,直至遍历完s代码就不贴了,看过官方解题,我自闭了官方解题static String convert(String s, int numRows) { if (numRows == 1 || s.Length <= numRows) re...原创 2020-04-28 14:38:18 · 258 阅读 · 0 评论 -
力扣(五) 最长回文子串
题目描述回文什么是回文?简单说就是正着读反着读都一样的字符串。“abcba”“oooo”等马拉车算法这个算法我也是看到一些题解才知道的,如果不知道可以去搜一下或者看下面的链接文章,我也是看人家的马拉车算法解题public string LongestPalindrome(string s) { if (s == ""){ return s;}//空字符串直接返...原创 2020-04-27 11:01:43 · 158 阅读 · 0 评论 -
力扣(四) 寻找两个有序数组的中位数
题目描述菜鸟(我)解决思路①将两组放到一起②数组排序③判断元素数量④单数则找最中间数,偶数则最中间两数求平均1.创建新的数组,存放元素public class Solution { public double FindMedianSortedArrays(int[] nums1, int[] nums2) { double mid; ...原创 2020-04-22 17:35:25 · 151 阅读 · 0 评论 -
力扣(三) 无重复字符的最长子串
题目描述大佬解法(借鉴)public class Solution { public int LengthOfLongestSubstring(string s) { List<char> ls = new List<char>(); int n = s.Length; int intMaxLength =...原创 2020-04-22 16:13:04 · 187 阅读 · 2 评论