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