seo查询整站,网页设计网站排行榜,网站开发维护花费,下载 做网站的原型文件文章目录 一、题目二、C# 题解 一、题目 给定两条线段#xff08;表示为起点 start {X1, Y1} 和终点 end {X2, Y2}#xff09;#xff0c;如果它们有交点#xff0c;请计算其交点#xff0c;没有交点则返回空值。 要求浮点型误差不超过 10^-6。若有多个交点#xff08;… 文章目录 一、题目二、C# 题解 一、题目 给定两条线段表示为起点 start {X1, Y1} 和终点 end {X2, Y2}如果它们有交点请计算其交点没有交点则返回空值。 要求浮点型误差不超过 10^-6。若有多个交点线段重叠则返回 X 值最小的点X 坐标相同则返回 Y 值最小的点。
示例 1 输入 line1 {0, 0}, {1, 0} line2 {1, 1}, {0, -1} 输出 {0.5, 0} 示例 2 输入 line1 {0, 0}, {3, 3} line2 {1, 1}, {2, 2} 输出 {1, 1} 示例 3 输入 line1 {0, 0}, {1, 1} line2 {1, 0}, {2, 1} 输出 {}两条线段没有交点 提示
坐标绝对值不会超过 2^7输入的坐标均是有效的二维坐标 点击此处跳转题目。
二、C# 题解 这题写的心累参考了 LeetCode 官方解法代码如下
public class Solution {public double[] Intersection(int[] start1, int[] end1, int[] start2, int[] end2) {int xa start1[0], xb end1[0], xc start2[0], xd end2[0];int ya start1[1], yb end1[1], yc start2[1], yd end2[1];double[] ans { };if ((xa - xb) * (yc - yd) ! (ya - yb) * (xc - xd)) { // 不平行int r (xd - xc) * (yb - ya) - (yd - yc) * (xb - xa);int p (xc - xa) * (yd - yc) - (yc - ya) * (xd - xc);int q (xa - xc) * (yb - ya) - (ya - yc) * (xb - xa);double m p * -1.0 / r, n q * 1.0 / r;if (0 m m 1 0 n n 1) ans new[] { xa (xb - xa) * m, ya (yb - ya) * m };}else if ((xa - xb) * (yc - ya) (ya - yb) * (xc - xa)) { // 平行且在一条直线上Operation(xa, ya, xc, yc, xd, yd, ref ans);Operation(xb, yb, xc, yc, xd, yd, ref ans);Operation(xc, yc, xa, ya, xb, yb, ref ans);Operation(xd, yd, xa, ya, xb, yb, ref ans);}return ans;}private void Operation(int xp, int yp, int xa, int ya, int xb, int yb, ref double[] ans) {if (xp xa InLine(yp, ya, yb)) Update(xp, yp, ref ans);else if (xp ! xa InLine(xp, xa, xb)) Update(xp, yp, ref ans);}private bool InLine(int p, int a, int b) {return a p p b || b p p a;}private void Update(int x, int y, ref double[] ans) {if (ans.Length 0) ans new double[] { x, y };else if (Math.Abs(x - ans[0]) 1e-6) ans[1] y ans[1] ? y : ans[1];else if (x ans[0]) {ans[0] x;ans[1] y;}}
}时间124 ms击败 66.67% 使用 C# 的用户内存41.04 MB击败 100.00% 使用 C# 的用户 文章转载自: http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.kongpie.com.gov.cn.kongpie.com