From 39ba56e781efb9b03f01651528ba15944e80ce37 Mon Sep 17 00:00:00 2001 From: MouCoder <69353905+MouCoder@users.noreply.github.com> Date: Fri, 6 Aug 2021 10:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E9=95=BF=E6=97=A0=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MaxLength | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 MaxLength diff --git a/MaxLength b/MaxLength new file mode 100644 index 0000000000..22bb7c9dd4 --- /dev/null +++ b/MaxLength @@ -0,0 +1,42 @@ +class Solution { +public: + /** + * + * @param arr int整型vector the array + * @return int整型 + */ + int maxLength(vector& arr) { + int begin = 0; + int end = 0; + int max = 0; + while(end < arr.size()) + { + //找是否会重复 + int flag = 0; + int i = begin; + for(i = begin;i < end;i++) + { + if(arr[i] == arr[end]) + { + flag = 1; + break; + } + } + //有重复,begin后移,更新max + if(flag == 1) + { + if(max < end-begin) + max = end-begin; + begin = i+1; + } + else + { + //没有重复,end继续后移 + end++; + } + } + if(end - begin > max) + max = end-begin; + return max; + } +};