//循环求解
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;
//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);
}
// 向左右翻转 ,再对角线翻转
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;
}
}
}
//将行 和 列 存到Set 中
public void SetZeroes(int[][] matrix)
{
int n = matrix.Length;
int m = matrix[0].Length;
HashSet<int> rows = new HashSet<int>();
HashSet<int> cols = new HashSet<int>();
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;
}
}
}
//是否重复用HashSet 来判断
// ListNode header = new ListNode(-1);
// header.next = head;
// ListNode current = header;
// HashSet<int> Hset = new HashSet<int>();
// 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<int> Hset = new HashSet<int>();
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;