一些算法1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | // See https://aka.ms/new-console-template for more information int [] nums={0,1,1,2,3,4,5}; int [] stockes={8,5,6,54,5,6,7,8}; int [] b={2,232,4,5,6,8}; int [] c={1,2,3,4,5,6,9}; var x = new List<A>{}; x.Add( null ); x.Add( new A{Name= "AAAA" }); var a= new Test(); System.Console.WriteLine(a.intersect(b,c)); System.Console.WriteLine(a.BuyStock(stockes)); System.Console.WriteLine(a.RemoveDuplicates(nums)); //找重复 System.Console.WriteLine(a.rotate(nums,3)); Console.WriteLine( "Hello, World!" ); class A{ public string Name { get ; set ;} } class Test{ public int RemoveDuplicates( int [] nums) { int left=0; for ( var right=1;right<nums.Length;right++){ if (nums[left]!=nums[right]){ nums[++left]=nums[right]; } } return ++left; } public int BuyStock( int [] Prices) { int total=0; for ( var i=0;i<Prices.Length-1;i++){ total+=Math.Max(Prices[i+1]-Prices[i],0); } return total; } public int [] rotate ( int [] nums, int k) { int Length=nums.Length; int [] temp = new int [Length]; for ( var i=0;i<nums.Length;i++){ temp[i]=nums[i]; } for ( int i=0;i<nums.Length;i++){ nums[(i+k)% Length]=temp[i]; } return nums; } public bool containsDuplicate( int [] nums){ Array.Sort(nums); for ( int ind = 1; ind < nums.Length; ind++) { if (nums[ind] == nums[ind - 1]) { return true ; } } return false ; } public int [] intersect( int [] nums1, int [] nums2) { Array.Sort(nums1); Array.Sort(nums2); List< int > list = new List< int >(); int i=0; int j=0; while (i<nums1.Length && j<nums2.Length){ if (nums1[i]<nums2[j]){ i++; } else if (nums1[i]>nums2[j]){ j++; } else { i++; j++; list.Add(nums1[i]); } } return list.ToArray(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!