Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
直覺寫法
public class Solution {
public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
var merged = nums1.Concat(nums2);
var mergedArray = merged.OrderBy(a => a).ToArray();
if (mergedArray.Length % 2 > 0)
{
var median = mergedArray[(mergedArray.Length - 1) / 2];
return median;
}
else
{
var medianFirst = mergedArray[((mergedArray.Length) / 2)-1];
var medianLast = mergedArray[((mergedArray.Length) / 2)];
return ((double)medianFirst + (double)medianLast) / 2;
}
}
}
結果就這樣過了,到底為什麼是Hard?
看了某些人也是這樣寫 Complexity: Time complexity: The time complexity is dominated by the sorting operation, which takes O(N log N) time, where N is the total number of elements in nums1 and nums2. Space complexity: The space complexity is determined by the size of the combined list nums, which is O(N), where N is the total number of elements in nums1 and nums2.
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY ["dotnetapp-discover.csproj", "dotnetapp-discover/"]
RUN dotnet restore "dotnetapp-discover/dotnetapp-discover.csproj"
COPY . ./dotnetapp-discover
WORKDIR "/src/dotnetapp-discover"
RUN dotnet build "dotnetapp-discover.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dotnetapp-discover.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dotnetapp-discover.dll"]
Access to XMLHttpRequest at 'https://dev.azurewebsites.net:5001/api/Auth/app-login' from origin 'https://dev.azurewebsites.net:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
代表被preflight預檢請求擋掉了
這個時候在Api的Program.cs
var app = builder.Build();
//之後加上
app.UseCors(options =>
options
.WithOrigins("https://dev.azurewebsites.net:3000", "*")
//note Allow Any Origin或直接"*"不會過
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
//之前加上
app.UseHttpsRedirection();
註2∶若你的store procedure中有使用到External DataSource,語法中有使用With (nolock)的話,會出現 – “Table hints are not supported on queries that reference external tables”.的錯誤訊息