Clear
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().Generate(300);
|
||||
foreach(var r in res)
|
||||
foreach (var r in res)
|
||||
Console.WriteLine(String.Join(" ", r));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,19 @@ public class Solution
|
||||
public IList<IList<int>> Generate(int numRows)
|
||||
{
|
||||
List<IList<int>> triangle = new List<IList<int>>() { new List<int> { 1 } };
|
||||
if(numRows > 1)
|
||||
triangle.Add(new List<int>() { 1,1 });
|
||||
if (numRows > 1)
|
||||
triangle.Add(new List<int>() { 1, 1 });
|
||||
numRows -= 2;
|
||||
while (numRows-- > 0)
|
||||
{
|
||||
IList<int> prev = triangle[^1];
|
||||
int nextCount = prev.Count;
|
||||
List<int> row = new List<int>() { 1 };
|
||||
for(int i = 1; i < nextCount; i++)
|
||||
for (int i = 1; i < nextCount; i++)
|
||||
{
|
||||
row.Add(prev[i-1] + prev[i]);
|
||||
row.Add(prev[i - 1] + prev[i]);
|
||||
}
|
||||
row.Add(1);
|
||||
row.Add(1);
|
||||
triangle.Add(row);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,16 @@ public class Solution
|
||||
{
|
||||
public ListNode? DetectCycle(ListNode head)
|
||||
{
|
||||
if(head == null)
|
||||
if (head == null)
|
||||
return null;
|
||||
ListNode? cur = head;
|
||||
int bit = 1 << 28;
|
||||
int curAbs = 0;
|
||||
while (cur!=null && (curAbs & bit) == 0)
|
||||
while (cur != null && (curAbs & bit) == 0)
|
||||
{
|
||||
cur.val ^= bit;
|
||||
cur = cur.next;
|
||||
if(cur != null)
|
||||
if (cur != null)
|
||||
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
|
||||
}
|
||||
cur = head;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var arr = new int[] { -2,-3,4 };
|
||||
var arr = new int[] { -2, -3, 4 };
|
||||
var res = new Solution().TwoSum(arr, -5);
|
||||
Console.WriteLine(String.Join(" ", res));
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ public class Solution
|
||||
{
|
||||
public int[]? TwoSum(int[] numbers, int target)
|
||||
{
|
||||
for(int i = 0; i < numbers.Length; i++)
|
||||
for (int i = 0; i < numbers.Length; i++)
|
||||
{
|
||||
int cur = numbers[i];
|
||||
int nextTar = target - cur;
|
||||
@@ -14,7 +14,7 @@ public class Solution
|
||||
int val = numbers[index];
|
||||
if (cur + val == target)
|
||||
{
|
||||
if(i>index)
|
||||
if (i > index)
|
||||
{
|
||||
i = i + index;
|
||||
index = i - index;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var arr = new int[] { 1, 2, 3, 4, 5, 6};
|
||||
var arr = new int[] { 1, 2, 3, 4, 5, 6 };
|
||||
new Solution().Rotate(arr, 3);
|
||||
Console.WriteLine(String.Join(" ", arr));
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class Solution
|
||||
k %= nums.Length;
|
||||
if (k == 0)
|
||||
return;
|
||||
int oneCycleLen = nums.Length/ GCD(nums.Length, k);
|
||||
int oneCycleLen = nums.Length / GCD(nums.Length, k);
|
||||
for (int i = 0; i < nums.Length / oneCycleLen; i++)
|
||||
MoveTo(nums, nums[i], i + k, k, oneCycleLen);
|
||||
}
|
||||
@@ -30,5 +30,5 @@ public class Solution
|
||||
return b == 0 ? a : GCD(b, a % b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int[][] matrix = new int[][] { new int[] { 1, 1,1,1 } };
|
||||
int[][] matrix = new int[][] { new int[] { 1, 1, 1, 1 } };
|
||||
Console.WriteLine(new Solution().SearchMatrix(matrix, 2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ public class Solution
|
||||
{
|
||||
public void MoveZeroes(int[] nums)
|
||||
{
|
||||
for(int i = 0; i < nums.Length-1; i++)
|
||||
for (int i = 0; i < nums.Length - 1; i++)
|
||||
{
|
||||
if (nums[i] != 0)
|
||||
continue;
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Solution
|
||||
HashSet<char> rowSet = new HashSet<char>();
|
||||
foreach (char c in row)
|
||||
{
|
||||
if(c != '.')
|
||||
if (c != '.')
|
||||
{
|
||||
int prevCount = rowSet.Count;
|
||||
rowSet.Add(c);
|
||||
@@ -28,10 +28,10 @@ public class Solution
|
||||
|
||||
private bool CheckColumns(char[][] board)
|
||||
{
|
||||
for(int columnIndex = 0; columnIndex < board[0].Length; columnIndex++)
|
||||
for (int columnIndex = 0; columnIndex < board[0].Length; columnIndex++)
|
||||
{
|
||||
HashSet<char> column = new HashSet<char>();
|
||||
for(int rowIndex = 0; rowIndex < board[columnIndex].Length; rowIndex++)
|
||||
for (int rowIndex = 0; rowIndex < board[columnIndex].Length; rowIndex++)
|
||||
{
|
||||
char ch = board[rowIndex][columnIndex];
|
||||
if (ch != '.')
|
||||
@@ -48,7 +48,7 @@ public class Solution
|
||||
|
||||
private bool CheckSubBoxes(char[][] board)
|
||||
{
|
||||
for(int i = 0; i < 27; i+=3)
|
||||
for (int i = 0; i < 27; i += 3)
|
||||
{
|
||||
int y = i / 9 * 3;
|
||||
int x = i % 9;
|
||||
@@ -61,12 +61,12 @@ public class Solution
|
||||
private bool CheckSubBox(char[][] board, int startY, int startX)
|
||||
{
|
||||
HashSet<char> set = new HashSet<char>();
|
||||
for (int y = startY; y < startY+3; y++)
|
||||
for (int y = startY; y < startY + 3; y++)
|
||||
{
|
||||
for(int x = startX; x < startX+3; x++)
|
||||
for (int x = startX; x < startX + 3; x++)
|
||||
{
|
||||
char ch = board[y][x];
|
||||
if(ch!='.')
|
||||
if (ch != '.')
|
||||
{
|
||||
int prev = set.Count;
|
||||
set.Add(ch);
|
||||
|
||||
@@ -5,21 +5,21 @@ public class Solution
|
||||
public int LongestPalindrome(string s)
|
||||
{
|
||||
Dictionary<char, int> dict = new Dictionary<char, int>();
|
||||
foreach(var ch in s)
|
||||
foreach (var ch in s)
|
||||
{
|
||||
if(dict.ContainsKey(ch))
|
||||
if (dict.ContainsKey(ch))
|
||||
dict[ch]++;
|
||||
else
|
||||
dict.Add(ch, 1);
|
||||
}
|
||||
int sum = 0;
|
||||
bool hasOdd = false;
|
||||
foreach(var v in dict.Values)
|
||||
foreach (var v in dict.Values)
|
||||
{
|
||||
sum += v / 2;
|
||||
if(v % 2 == 1)
|
||||
if (v % 2 == 1)
|
||||
hasOdd = true;
|
||||
}
|
||||
return sum*2 + (hasOdd ? 1 : 0);
|
||||
return sum * 2 + (hasOdd ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().MatrixReshape(new int[][] { new int[] { 1,2 }, new int[] { 4, 5 } }, 2, 4);
|
||||
foreach(var row in res)
|
||||
var res = new Solution().MatrixReshape(new int[][] { new int[] { 1, 2 }, new int[] { 4, 5 } }, 2, 4);
|
||||
foreach (var row in res)
|
||||
Console.WriteLine(String.Join(" ", row));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Solution
|
||||
int size = r * c;
|
||||
if (size != height * width)
|
||||
return mat;
|
||||
for(int i = 0; i < size; i++)
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int ySource = i / width;
|
||||
int xSource = i % width;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int[][] matrix = new int[][] { new int[] { 1 }};
|
||||
int[][] matrix = new int[][] { new int[] { 1 } };
|
||||
Console.WriteLine(new Solution().SearchMatrix(matrix, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var l1 = ListNode.Create(new int[] { 1,2,3,4,5});
|
||||
var l2 = ListNode.Create(new int[] { 1,2,3,4,5,6});
|
||||
var l1 = ListNode.Create(new int[] { 1, 2, 3, 4, 5 });
|
||||
var l2 = ListNode.Create(new int[] { 1, 2, 3, 4, 5, 6 });
|
||||
Console.WriteLine(new Solution().MiddleNode(l1).val);
|
||||
Console.WriteLine(new Solution().MiddleNode(l2).val);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Solution
|
||||
public ListNode MiddleNode(ListNode? head)
|
||||
{
|
||||
ListNode result = head!;
|
||||
while(head?.next != null)
|
||||
while (head?.next != null)
|
||||
{
|
||||
head = head?.next?.next;
|
||||
result = result!.next!;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().SortedSquares(new int[] { -7, -2,-1 });
|
||||
var res = new Solution().SortedSquares(new int[] { -7, -2, -1 });
|
||||
Console.WriteLine(String.Join(" ", res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class Solution
|
||||
{
|
||||
int[] res = new int[nums.Length];
|
||||
int firstNonNegative = -1;
|
||||
for(int i = 0; i < nums.Length; i++)
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (nums[i] >= 0)
|
||||
{
|
||||
@@ -17,12 +17,12 @@ public class Solution
|
||||
|
||||
int resIndex = 0;
|
||||
int positiveIndex = firstNonNegative;
|
||||
int negativeIndex = firstNonNegative!=-1 ? positiveIndex - 1 : nums.Length-1;
|
||||
while(resIndex < nums.Length)
|
||||
int negativeIndex = firstNonNegative != -1 ? positiveIndex - 1 : nums.Length - 1;
|
||||
while (resIndex < nums.Length)
|
||||
{
|
||||
if(negativeIndex>=0 && positiveIndex < nums.Length && positiveIndex>=0)
|
||||
if (negativeIndex >= 0 && positiveIndex < nums.Length && positiveIndex >= 0)
|
||||
{
|
||||
if (nums[negativeIndex]*-1 < nums[positiveIndex])
|
||||
if (nums[negativeIndex] * -1 < nums[positiveIndex])
|
||||
{
|
||||
res[resIndex++] = nums[negativeIndex] * nums[negativeIndex];
|
||||
negativeIndex--;
|
||||
@@ -33,12 +33,12 @@ public class Solution
|
||||
positiveIndex++;
|
||||
}
|
||||
}
|
||||
else if(negativeIndex >= 0)
|
||||
else if (negativeIndex >= 0)
|
||||
{
|
||||
res[resIndex++] = nums[negativeIndex]* nums[negativeIndex];
|
||||
res[resIndex++] = nums[negativeIndex] * nums[negativeIndex];
|
||||
negativeIndex--;
|
||||
}
|
||||
else if(positiveIndex >= 0)
|
||||
else if (positiveIndex >= 0)
|
||||
{
|
||||
res[resIndex++] = nums[positiveIndex] * nums[positiveIndex];
|
||||
positiveIndex++;
|
||||
|
||||
Reference in New Issue
Block a user