Reformat the C# codes.

Disable creating new line before open brace.
This commit is contained in:
krahets
2023-04-23 03:03:12 +08:00
parent ac6eece4f3
commit 73dcb4cea9
49 changed files with 561 additions and 1135 deletions

View File

@ -8,17 +8,13 @@ using NUnit.Framework;
namespace hello_algo.chapter_searching;
public class leetcode_two_sum
{
public class leetcode_two_sum {
/* 方法一:暴力枚举 */
public static int[] twoSumBruteForce(int[] nums, int target)
{
public static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
@ -27,16 +23,13 @@ public class leetcode_two_sum
}
/* 方法二:辅助哈希表 */
public static int[] twoSumHashTable(int[] nums, int target)
{
public static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++)
{
if (dic.ContainsKey(target - nums[i]))
{
for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) {
return new int[] { dic[target - nums[i]], i };
}
dic.Add(nums[i], i);
@ -45,8 +38,7 @@ public class leetcode_two_sum
}
[Test]
public void Test()
{
public void Test() {
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 9;