Skip to content

Commit ec4bd97

Browse files
author
atwood
committed
F
1 parent 46af485 commit ec4bd97

File tree

3 files changed

+231
-132
lines changed

3 files changed

+231
-132
lines changed

No149_Math.cs

+111-47
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,123 @@ namespace LeetCode_149
1717
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
1818
// int[][] intArr = new int[][]
1919
// {
20-
// new int[]{1,1},
21-
// new int[]{3,2},
22-
// new int[]{5,3},
23-
// new int[]{4,1},
24-
// new int[]{2,3},
25-
// new int[]{1,4},
20+
// //new int[]{1,1},
21+
// //new int[]{3,2},
22+
// //new int[]{5,3},
23+
// //new int[]{4,1},
24+
// //new int[]{2,3},
25+
// //new int[]{1,4},
26+
// new int[]{0,0},
27+
// new int[]{94911150, 94911151},
28+
// new int[]{94911151, 94911152},
2629
// };
2730
// //int input = int.Parse(input2);
2831
// var res = solution.MaxPoints(intArr);
2932
// Console.WriteLine(res);
3033
// }
3134
//}
3235

33-
public class Solution
34-
{
35-
public int MaxPoints(int[][] points)
36-
{
37-
//Hashtable table = new Hashtable();
38-
Dictionary<float, HashSet<int[]>> dic = new Dictionary<float, HashSet<int[]>>();
39-
for (int i = 0; i < points.Length; i++)
40-
{
41-
for (int j = i + 1; j < points.Length; j++)
42-
{
43-
float slope;
44-
//水平线y为0,给一个特殊值作为斜率
45-
if ((points[i][1] - points[j][1]) == 0)
46-
{
47-
slope = float.MinValue;
48-
}
49-
else
50-
{
51-
//计算两点的斜率
52-
slope = (float)(points[i][0] - points[j][0]) / (float)(points[i][1] - points[j][1]);
53-
}
5436

55-
if (!dic.ContainsKey(slope))
56-
dic.Add(slope, new HashSet<int[]> { points[i], points[j] });
57-
else
58-
{
59-
dic[slope].Add(points[i]);
60-
dic[slope].Add(points[j]);
61-
}
62-
}
63-
}
64-
int max = 0;
65-
foreach (var m in dic)
66-
{
67-
if (((HashSet<int[]>)m.Value).Count > max)
68-
{
69-
max = ((HashSet<int[]>)m.Value).Count;
70-
}
71-
}
72-
return max;
73-
}
74-
}
37+
/// <summary>
38+
/// Experience:当除数和被除数都非常大,且他们之前非常接近时,除法的浮点数运算会求近似值,所以这种情况下就是导致数据失真。
39+
/// </summary>
40+
//public class Solution
41+
//{
42+
// /// <summary>
43+
// /// 失败了!失败了!失败了!失败了!失败了!失败了!失败了!失败了!
44+
// /// 方法不对,越A越累。用斜率不行,除数和被除数大到一定程度之后,微小的除法差异会被算法忽略掉,这样斜率就失效了
45+
// /// </summary>
46+
// /// <param name="points"></param>
47+
// /// <returns></returns>
48+
// public int MaxPoints(int[][] points)
49+
// {
50+
// if (points.Length <= 1)
51+
// return points.Length;
52+
53+
// if (IsAllDuplicatedPoints(points))
54+
// return points.Length;
55+
56+
// Dictionary<double, List<HashSet<int>>> dic = new Dictionary<double, List<HashSet<int>>>();
57+
// for (int i = 0; i < points.Length; i++)
58+
// {
59+
// for (int j = i + 1; j < points.Length; j++)
60+
// {
61+
// //如果横纵坐标完全一致,就去重
62+
// if (points[i][0] == points[j][0] && points[i][1] == points[j][1])
63+
// continue;
64+
65+
// double slope = CaculateSlope(points[i], points[j]);
66+
67+
// if (!dic.ContainsKey(slope))
68+
// dic.Add(slope, new List<HashSet<int>>() { new HashSet<int> { i, j } });
69+
// else
70+
// {
71+
// //逐个尝试同斜率下的不用数组,取其最后一个点用来计算,看是不是同一条线
72+
// var lines = dic[slope];
73+
// bool jointLine = false;
74+
// for (int k = 0; k < lines.Count; k++)
75+
// {
76+
// var line = lines[k];
77+
// //同一条直线
78+
// for (int n = 0; n < line.Count; n++)
79+
// {
80+
// if (CaculateSlope(points[line.ToList()[n]], points[i]) == slope)
81+
// {
82+
// line.Add(i);
83+
// line.Add(j);
84+
// jointLine = true;
85+
// break;
86+
// }
87+
// }
88+
// }
89+
// //遍历所有线之后都没有遇到在同一条线上的
90+
// if (!jointLine)
91+
// lines.Add(new HashSet<int>() { i, j });
92+
// }
93+
// }
94+
// }
95+
// int max = 0;
96+
// foreach (var m in dic)
97+
// {
98+
// foreach (var n in m.Value)
99+
// {
100+
// if (n.Count > max)
101+
// {
102+
// max = n.Count;
103+
// }
104+
// }
105+
// }
106+
// return max;
107+
// }
108+
109+
// private bool IsAllDuplicatedPoints(int[][] points)
110+
// {
111+
// bool isAllduplicated = true;
112+
// for (int i = 1; i < points.Length; i++)
113+
// {
114+
// if (!(points[0][0] == points[i][0] && points[0][1] == points[i][1]))
115+
// {
116+
// isAllduplicated = false;
117+
// break;
118+
// }
119+
// }
120+
// return isAllduplicated;
121+
// }
122+
123+
// private double CaculateSlope(int[] pointA, int[] pointB)
124+
// {
125+
// double slope;
126+
// //水平线y为0,给一个特殊值作为斜率
127+
// if ((pointA[1] - pointB[1]) == 0)
128+
// {
129+
// slope = double.MaxValue;
130+
// }
131+
// else
132+
// {
133+
// //计算两点的斜率
134+
// slope = (double)(pointA[0] - pointB[0]) / (double)(pointA[1] - pointB[1]);
135+
// }
136+
// return slope;
137+
// }
138+
//}
75139
}

No27_Array.cs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_27
6+
{
7+
public class Solution
8+
{
9+
/// <summary>
10+
/// 快慢指针,可能性能看起来相差不大,但是更巧妙,更简洁
11+
/// 时间复杂度:O(n)
12+
/// 空间复杂度:O(1)
13+
/// </summary>
14+
/// <param name="nums"></param>
15+
/// <param name="val"></param>
16+
/// <returns></returns>
17+
public int RemoveElement(int[] nums, int val)
18+
{
19+
int cur_index = 0;
20+
for (int i = 0; i < nums.Length; i++)
21+
{
22+
if (nums[i] != val)
23+
{
24+
nums[cur_index] = nums[i];
25+
cur_index++;
26+
}
27+
}
28+
return cur_index;
29+
}
30+
31+
/// <summary>
32+
/// 另一种快慢指针,优化了不必要的赋值,也很巧妙,不过不如第一个好理解,有点绕
33+
/// </summary>
34+
/// <param name="nums"></param>
35+
/// <param name="val"></param>
36+
/// <returns></returns>
37+
//public int RemoveElement(int[] nums, int val)
38+
//{
39+
// int i = 0;
40+
// int n = nums.Length;
41+
// while (i < n)
42+
// {
43+
// if (nums[i] == val)
44+
// {
45+
// nums[i] = nums[n - 1];
46+
// // reduce array size by one
47+
// n--;
48+
// }
49+
// else
50+
// {
51+
// i++;
52+
// }
53+
// }
54+
// return n;
55+
//}
56+
57+
/// <summary>
58+
/// 第一反应解,然而代码却很丑陋。之前才用过的快慢指针又不知道用了
59+
/// </summary>
60+
/// <param name="nums"></param>
61+
/// <param name="val"></param>
62+
/// <returns></returns>
63+
//public int RemoveElement(int[] nums, int val)
64+
//{
65+
// int res = 0;
66+
// for (int i = 0; i < nums.Length; i++)
67+
// {
68+
// if (nums[i] == val)
69+
// {
70+
// for (int j = i + 1; j < nums.Length; j++)
71+
// {
72+
// if (nums[j] != val)
73+
// {
74+
// Swap(nums, i, j);
75+
// res++;
76+
// break;
77+
// }
78+
// }
79+
// }
80+
// else
81+
// res++;
82+
// }
83+
// return res;
84+
//}
85+
86+
//private void Swap(int[] nums, int source, int target)
87+
//{
88+
// int temp;
89+
// temp = nums[source];
90+
// nums[source] = nums[target];
91+
// nums[target] = temp;
92+
//}
93+
}
94+
}

Program.cs

+26-85
Original file line numberDiff line numberDiff line change
@@ -18,101 +18,42 @@ static void Main(string[] args)
1818
//int input2 = int.Parse(Console.ReadLine());
1919
//int input3 = int.Parse(Console.ReadLine());
2020
string input = Console.ReadLine();
21-
int input2 = int.Parse(Console.ReadLine());
2221
//string input2 = Console.ReadLine();
2322
int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
24-
//int input = int.Parse(input2);
25-
var res = solution.RemoveElement(intArr, input2);
26-
Console.WriteLine(res);
23+
int input2 = int.Parse(Console.ReadLine());
24+
var res = solution.SearchInsert(intArr, input2);
25+
Console.WriteLine(res);
2726
}
2827
}
2928

3029
public class Solution
3130
{
32-
/// <summary>
33-
/// 快慢指针,可能性能看起来相差不大,但是更巧妙,更简洁
34-
/// 时间复杂度:O(n)
35-
/// 空间复杂度:O(1)
36-
/// </summary>
37-
/// <param name="nums"></param>
38-
/// <param name="val"></param>
39-
/// <returns></returns>
40-
public int RemoveElement(int[] nums, int val)
41-
{
42-
int cur_index = 0;
43-
for (int i = 0; i < nums.Length; i++)
31+
public int SearchInsert(int[] nums, int target)
32+
{
33+
int left = 0;
34+
int right = nums.Length;
35+
while (true)
4436
{
45-
if (nums[i] != val)
46-
{
47-
nums[cur_index] = nums[i];
48-
cur_index++;
49-
}
50-
}
51-
return cur_index;
37+
int mid = (left + right) / 2;
38+
if (nums[mid] == target)
39+
return mid;
40+
else if (nums[mid] > target)
41+
right = mid;
42+
else if (nums[mid] < target)
43+
left = mid;
44+
}
5245
}
5346

54-
/// <summary>
55-
/// 另一种快慢指针,优化了不必要的赋值,也很巧妙,不过不如第一个好理解,有点绕
56-
/// </summary>
57-
/// <param name="nums"></param>
58-
/// <param name="val"></param>
59-
/// <returns></returns>
60-
//public int RemoveElement(int[] nums, int val)
61-
//{
62-
// int i = 0;
63-
// int n = nums.Length;
64-
// while (i < n)
65-
// {
66-
// if (nums[i] == val)
67-
// {
68-
// nums[i] = nums[n - 1];
69-
// // reduce array size by one
70-
// n--;
71-
// }
72-
// else
73-
// {
74-
// i++;
75-
// }
76-
// }
77-
// return n;
78-
//}
79-
80-
/// <summary>
81-
/// 第一反应解,然而代码却很丑陋。之前才用过的快慢指针又不知道用了
82-
/// </summary>
83-
/// <param name="nums"></param>
84-
/// <param name="val"></param>
85-
/// <returns></returns>
86-
//public int RemoveElement(int[] nums, int val)
87-
//{
88-
// int res = 0;
89-
// for (int i = 0; i < nums.Length; i++)
90-
// {
91-
// if (nums[i] == val)
92-
// {
93-
// for (int j = i + 1; j < nums.Length; j++)
94-
// {
95-
// if (nums[j] != val)
96-
// {
97-
// Swap(nums, i, j);
98-
// res++;
99-
// break;
100-
// }
101-
// }
102-
// }
103-
// else
104-
// res++;
105-
// }
106-
// return res;
107-
//}
108-
109-
//private void Swap(int[] nums, int source, int target)
110-
//{
111-
// int temp;
112-
// temp = nums[source];
113-
// nums[source] = nums[target];
114-
// nums[target] = temp;
115-
//}
47+
private int Recursive(int[] nums, int left, int right, int target)
48+
{
49+
int mid = (left + right) / 2;
50+
if (nums[mid] == target)
51+
return mid;
52+
if (nums[mid] > target)
53+
return Recursive(nums, mid, right, target);
54+
else if (nums[mid] < target)
55+
return Recursive(nums, left, mid, target);
56+
}
11657
}
11758
}
11859
}

0 commit comments

Comments
 (0)