leetcode C#刷题2

面试题 01.06. 字符串压缩

//循环求解
char[] strs = S.ToCharArray();
            string newstr = "";
            int n = 1;
            if(S.Length==1){
                return S;
            }
            for (int i = 0, j = 1; i < S.Length - 1; i++,j++) {
                if (strs[i] == strs[j])
                {
                    n++;
                }
                else {
                    newstr = newstr + strs[i] + n.ToString();
                    n = 1;
                }
                if(j==S.Length-1){
                    newstr = newstr + strs[j] + n.ToString();
                }
            }
            if(newstr.Length >= S.Length)
            {
                return S;
            }

            return newstr;

面试题 01.09. 字符串轮转

//S1+S1 字符串中包含 S2
public bool IsFlipedString(string s1, string s2)
{
if (s1.Length != s2.Length)
                return false;
            string str = s1 + s1;
            return str.Contains(s2);
}

面试题 01.07. 旋转矩阵

// 向左右翻转 ,再对角线翻转
public void Rotate(int[][] matrix) {
                int n = matrix.Length;
                int m = matrix[0].Length;

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m / 2; j++)
                    {
                        int tmp = matrix[i][j];
                        matrix[i][j] = matrix[i][n - 1 - j];
                        matrix[i][n - 1 - j] = tmp;
                    }
                }

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        int tmp = matrix[i][j];
                        matrix[i][j] = matrix[j][i];
                        matrix[j][i] = tmp;
                    }
                }        
    }

面试题 01.08. 零矩阵

//将行 和 列 存到Set  中
public void SetZeroes(int[][] matrix)
        {
            int n = matrix.Length;
            int m = matrix[0].Length;
            HashSet rows = new HashSet();
            HashSet cols = new HashSet();
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (matrix[i][j] == 0)
                    {
                        rows.Add(i);
                        cols.Add(j);
                    }
                }
            }
            for (int i = 0; i < m; i++)
            {
                foreach (int rowIndex in rows)
                {
                    matrix[rowIndex][i] = 0;
                }
            }

            for (int i = 0; i < n; i++)
            {
                foreach (int colIndex in cols)
                {
                    matrix[i][colIndex] = 0;
                }
            }
        }

面试题 02.01. 移除重复节点

//是否重复用HashSet 来判断
//  ListNode header = new ListNode(-1);
        //     header.next = head;
        //     ListNode current = header;
        //     HashSet Hset = new HashSet();
        //     while (current.next != null)
        //     {
        //         if (!Hset.Add(current.next.val))
        //         {
        //             current.next = current.next.next;
        //         }
        //         else
        //         {
        //             current = current.next;
        //         }
        //     }
        //     return header.next;

         if (head == null || head.next == null) return head;
            HashSet Hset = new HashSet();
            var curr = head;
            Hset.Add(curr.val);
            while (curr != null && curr.next != null)
            {
                if (!Hset.Add(curr.next.val))
                {
                    curr.next = curr.next.next;
                }
                else
                {
                    curr = curr.next;
                }
            }

            return head;

你可能感兴趣的:(leetcode C#刷题2)