自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 数据结构与算法64

旋转图像解题思路:旋转90度等于把矩阵转置再把每一行反转。代码: public static void Rotate(int[][] matrix) { for(int i=0;i<matrix.Length;i++) { for(int j=i+1;j<matrix.Length;j++) { int t

2020-06-24 23:49:15 136

原创 数据结构与算法练习63

全排列2解题思路:和全排列一样,回溯思想,就是当有重复元素的时候要注意如何去重。代码: public IList<IList<int>> PermuteUnique(int[] nums) { IList<IList<int>> result = new List<IList<int>>(); if (nums.Length == 0)

2020-06-23 21:03:13 130

原创 数据结构与算法练习62

阶乘后的0解题思路:其实很简单,就是要如何做到不超时。这就要知道规律,就是找这个数的阶乘中有多少个2*5,而又由于2的个数会比5的个数多,所以找5的个数就是最后0的个数。代码: public int TrailingZeroes(int n) { int result = n / 5; if (result < 5) return result; else

2020-06-23 20:24:18 118

原创 数据结构与算法练习61

全排列解题思路:回溯。去试探每个结果的正确性。代码: public IList<IList<int>> Permute(int[] nums) { IList<IList<int>> result = new List<IList<int>>();//结果。 IList<int> lgx = new List<int>();/

2020-06-22 11:30:41 129

原创 数据结构与算法练习60

组合总和二解题思路:和组合总和一样的思路。回溯。就是注意每一次起始索引要加一,还有要避免重复。代码: public IList<IList<int>> CombinationSum2(int[] candidates, int target) { IList<IList<int>> result = new List<IList<int>>(); I

2020-06-20 15:56:52 138

原创 数据结构与算法练习59

组合总和解题思路:回溯。深度优先搜索,每一次都从当前位置开始就可以避免重复。代码: public IList<IList<int>> CombinationSum(int[] candidates, int target) { IList<IList<int>> result = new List<IList<int>>(); Array.Sort(

2020-06-19 23:08:55 111

原创 数据结构与算法练习58

Excel表列名称解题思路:就是每次对26取余得到的值就是一个字母,不过运算的时候注意要减一。代码: public string ConvertToTitle(int n) { StringBuilder result = new StringBuilder(); while(n>0) { int m = n % 26; if

2020-06-19 22:19:21 345

原创 数据结构与算法练习57

有效的数独解题思路:三次遍历。一次找行,一次找列,一次找9宫格。主要是9宫格如何确定i,j的初始值,可以看出横竖都只有三个9宫格,且分别开始数为0,3,6,为0,1,2的三倍,所以用m,n来确定i,j的取值。代码: public bool IsValidSudoku(char[][] board) { bool result = true; for(int i=0;i<9;i++) {

2020-06-18 23:14:01 113

原创 数据结构与算法练习56

在排序数组中查找数组的边界解题思路:二分法。代码: public static int[] SearchRange(int[] nums, int target) { int[] result = new int[2] { -1, -1 }; if (nums.Length == 0 || (nums.Length == 1 && nums[0] != target) || nums[nums.Lengt

2020-06-16 22:49:52 90

原创 数据结构与算法练习55

相交链表解题思路:双指针。第一遍的时候A与B不一定同时走到相同节点,但是如果都走A+B那就会同时走到相同节点。代码: public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { ListNode pA = headA; ListNode pB = headB; if (pA == null || pB == null)

2020-06-16 21:27:38 85

原创 数据结构与算法练习54

搜索旋转排序数组解题思路:这道题主要就是考察二分法。代码: public static int Search(int[] nums, int target) { if (nums.Length == 0 || nums == null) return -1; if (nums.Length == 1) return target == nums[0] ? 0

2020-06-10 23:42:50 104

原创 数据结构与算法练习53

验证回文串解题思路:双指针法。主要注意把字母全换成一种格式再比较,并且可能是符号的时候要跳过。代码: public bool IsPalindrome(string s) { s=s.ToLower().Replace(" ", "");//要区分大小写,所以将其全部转换为小写字母,再将s中所有空格去掉。 int left = 0; int right = s.Length-

2020-06-10 23:05:48 114

原创 数据结构与算法练习52

两数相除解题思路:位运算,递归。m/n就是找m中有多少个n,首先可以用暴力法,每次减一个n,但是这样耗时太久。所以可以优化一下,只要还能除,那么m一定是n的2的x次方倍,再m=m-n*2^x计算,每次递归直到m<n,把所有倍数加起来就是除法结果。代码: public int Divide(int dividend, int divisor) { int flag1 = dividend > 0 ? -1 : 1;

2020-06-10 00:20:24 136

原创 数据结构与算法练习51

杨辉三角二解题思路:一:直接沿用杨辉三角的方法,最后返回对应一行就可以。二:空间复杂度为o(k)的方法,就是只用一个数组。代码: public IList<int> GetRow(int rowIndex) { //IList<IList<int>> result = new List<IList<int>>(); //for (int i = 0; i &l

2020-06-09 21:42:22 97

原创 数据结构与算法练习50

两两交换链表中的节点解题思路:递归。找终止条件,找这一步应该做什么,找返回值。代码: public ListNode SwapPairs(ListNode head) { if (head == null || head.next == null) return head;//递归终止条件。 ListNode Next = head.next; head.next

2020-06-08 12:02:27 154

原创 数据结构与算法练习49

杨辉三角解题思路:代码:![在这里插入代码片](https://img-blog.csdnimg.cn/20200608112125884.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoZ3gx,size_16,color_FFFFFF,t_70)

2020-06-08 11:30:00 113

原创 数据结构与算法练习48

括号生成解题思路:递归。代码: public static IList<string> GenerateParenthesis(int n) { IList<string> result = new List<string>(); string lgx = string.Empty; find(n, n, ref result, lgx);

2020-06-07 16:34:00 87

原创 数据结构与算法练习47

路径总和解题思路:递归。找准返回条件以及递归条件。代码: /*public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } }*/ publ

2020-06-07 15:28:47 101

原创 数据结构与算法练习46

删除链表的倒数第n个节点解题思路:双指针法。一个指针先走n步后,在一起走,当头指针为null时,second就在要删除的结点的前一个位置,就可以进行删除结点了。代码: /* public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } }*/

2020-06-04 21:20:41 117

原创 数据结构与算法练习45

二叉树的最小深度解题思路:这个和最大深度是一样的。只是到这道题我才发现原来深度只是到叶子节点路径中结点的个数,之前理解错了。之前一直以为[1,2]这样的是两个深度,一个2一个1原来只有2,左边的才算深度。代码: public int MinDepth(TreeNode root) { if (root == null) return 0; if (root.left != null &am

2020-06-04 20:54:36 94

原创 数据结构与算法练习44

四数之和解题思路:思路和之前的三数之和一模一样,就是每次固定的指针多了一个,每次双指针循环完先移动这个再移动第一个指针。代码: public static IList<IList<int>> FourSum(int[] nums, int target) { IList<IList<int>> results = new List<IList<int>>();

2020-06-03 22:07:04 91

原创 数据结构与算法练习43

平衡二叉树解题思路:这里复用了求二叉树的最大深度的代码。递归,反复比较左右二叉树深度的差值。代码: /*public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; }

2020-06-03 21:17:15 100

原创 数据结构与算法练习42

将有序数组转为二叉平衡搜索树解题思路:二分法。每次取排序数组中间的数作为节点,左边的作为左子节点,右边的作为右子节点,递归直到left>right,才返回。代码: public class Solution { /*public class TreeNode { public int val; public TreeNode left;

2020-06-02 17:47:23 104

原创 数据结构与算法练习41

整数转罗马数字解题思路:看数字能被哪个整除,就有几个这个罗马数字,一共13个罗马数字,所以循环找13次。代码: public static string IntToRoman(int num) { int[] nums = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; string[] roman = { "M", "CM", "D", "CD", "

2020-06-02 17:36:30 232

原创 数据结构与算法练习40

电话号码的字母组合解题思路:回溯。通过递归找到所有可能的组合。代码:public class solution { public IList<string> LetterCombinations(string digits) { IList<string> results = new List<string>(); if (string

2020-06-01 23:32:05 127

原创 数据结构与算法练习39

二叉树的层次遍历解题思路:由上往下层次遍历再反转顺序。代码: public IList<IList<int>> LevelOrderBottom(TreeNode root) { IList<IList<int>> result = new List<IList<int>>(); Queue<TreeNode> queue = new Q

2020-06-01 20:31:52 96

原创 数据结构与算法练习38

字符串相乘解题思路:竖式相乘法。理清楚自己手算两个数字的方法,让其代码实现。代码: public static string Multiply(string num1, string num2) { StringBuilder lgx = new StringBuilder(); for(int i=0;i<num1.Length+num2.Length;i++) { lgx.App

2020-05-30 10:06:40 109

原创 数据结构与算法练习37

最接近的三数之和解题思路:和三数之和思路是一样的,双指针法就行。代码: for(int i=0;i<nums.Length-2;i++) { int left = i + 1; int right = nums.Length - 1;//双指针法 while(left<right) {

2020-05-28 22:03:27 126

原创 数据结构与算法练习36

三数之和解题思路:暴力法可以解,但时间太长。所以还是双指针法,每次固定一个数,另外两个数由两个指针指向,每次要移动指针,又要避免重复,所以在算之前先进行排序,每次相同的数就跳过就不会重复了。代码: Array.Sort(nums);//先排序,直接调用函数难得自己写了。 IList<IList<int>> result = new List<IList<int>>(); if (

2020-05-27 18:10:28 147

原创 数据结构与算法练习35

盛水最多的容器解题思路:首先想到的暴力法,两层for循环逐一找到最大值。其次更优化的双指针法:一个从头,一个从尾,计算,每次找出值最小(即容器边最低)的一边前进,用max记录下最大值。代码: public static int MaxArea(int[] height) { int i = 0; int j = height.Length-1; int max = 0;

2020-05-26 15:07:14 181

原创 数据结构与算法练习34

字符串转换整数解题思路:去掉前空格后,首先判断是否长度就为0了,若不是,则把开头的符号和后面的数字赋值给一个新的字符串,最后try转换下,若不行,则说明只有符号或超出界限。代码: public int MyAtoi(string str) { string lgx = str.TrimStart(); if (lgx.Length == 0) return 0;//

2020-05-25 12:42:26 169

原创 数据结构与算法练习33

z字形变换解题思路:直接按行排序,用flag标识是往下走还是往上,直到行数为0或numrows才变。代码: public string Convert(string s, int numRows) { if(numRows==1||s.Length<numRows)//如果行数等于1或者s的长度小于行数,直接返回s。 return s; List<StringBuilder&gt

2020-05-24 14:55:25 118

原创 数据结构与算法练习32

外观数列解题思路:很显然,要得到第n个数就要知道滴n-1个数,所以需要用到递归。代码: public string CountAndSay(int n) { if(n==1) return "1"; if(n==2) return "11"; string lgx=CountAndSay(n-1); i

2020-05-24 11:40:15 124

原创 数据结构与算法练习31

最长回文子串解题思路:中心扩展法。for循环遍历,每次以mid=i/2.0为中心,用双指针往两边比较,直到不相等。代码: public string LongestPalindrome(string s) { string result=string.Empty; int max=0; for(int i=0;i<2*s.Length-1;i++)//为什么是到2*s.Length-1

2020-05-23 21:41:50 109

原创 数据结构与算法练习30

单词替换解题思路:遍历句子中每个单词,判断是否有个词根的开始位置在它打首位,且找出长度最短的,若有,记录下来,并在后面加上这个词根,否则就直接加temp,最后注意去掉末尾的空格再返回。代码: public string ReplaceWords(IList<string> dict, string sentence) { string s=string.Empty; foreach(var temp in sent

2020-05-22 21:34:06 141

原创 数据结构与算法练习29

下一个排列解题思路:后序遍历找到前一个比后一个小的数,用 j 记录下位置,再从 j 忘后找到最小的比它大的数,交换,交换后不一定就是最后的结果,要把 j 后面的数升序排列就得到最终结果。代码: public void NextPermutation(int[] nums) { int i=nums.Length-1; int j=0; while(i>0) {

2020-05-21 12:48:23 109

原创 数据结构与算法练习28

两数相加解题思路:主要是要进位以及最后在判断是不是要比原来的长度多一位。代码:/* Definition for singly-linked list. public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } } */ public

2020-05-20 17:49:40 57

原创 数据结构与算法练习27

课程表2解题思路:和课程表1的思路一模一样,只是加个返回数组。我这是用链表,每次从队列中弹出数时就加入链表中,最后返回之前先判断是否可以学完,再返回。代码: public int[] FindOrder(int numCourses, int[][] prerequisites) { List<int> lgx=new List<int>();//多了个返回数组。 if(prerequisites.

2020-05-19 12:39:35 156

原创 数据结构与算法练习26

课程表解题思路:拓扑排序,分别用数组记录下每门课的入度和后继课程,首先把入度为0的课先入队,每次入队就使未学的课程-1,每次取出队首,使它的后继课程的入度-1,若入度变为0,则入队,直到队列中无元素。最后判断若未学的课为0则返回true。代码: public bool CanFinish(int numCourses, int[][] prerequisites) { if(prerequisites.Length==0)

2020-05-19 12:13:50 222

原创 数据结构用于算法练习25

二叉树的最近公共祖先解题思路:递归。从根节点开始,直到为null或找到等于其中一个再返回,否则去找它的左右子节点,再判断,如果两边返回的都不是null,就返回root,否则返回非空的一边返回的值。代码:/** * Definition for a binary tree node. public class TreeNode { public int val; ...

2020-04-29 21:48:38 79

空空如也

空空如也

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

TA关注的人

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