From f13b9804decf4aa747336215882b79e07d8370d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=9B=E9=AA=8F=E6=B4=8B?= <836157478@qq.com> Date: Sun, 6 Aug 2023 02:20:31 +0000 Subject: [PATCH] =?UTF-8?q?update=20problems/0017.=E5=87=BA=E6=A0=88?= =?UTF-8?q?=E5=90=88=E6=B3=95=E6=80=A7.md.=20=E6=B7=BB=E5=8A=A0=E4=BA=86ja?= =?UTF-8?q?va=E6=80=9D=E8=B7=AF2=E6=A0=88=E6=A8=A1=E6=8B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 盛骏洋 <836157478@qq.com> --- ...10\345\220\210\346\263\225\346\200\247.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0017.\345\207\272\346\240\210\345\220\210\346\263\225\346\200\247.md" "b/problems/0017.\345\207\272\346\240\210\345\220\210\346\263\225\346\200\247.md" index 426b4a1..34b3f43 100644 --- "a/problems/0017.\345\207\272\346\240\210\345\220\210\346\263\225\346\200\247.md" +++ "b/problems/0017.\345\207\272\346\240\210\345\220\210\346\263\225\346\200\247.md" @@ -79,7 +79,36 @@ public class Main { } } ``` +###java思路2 +``` +import java.util.Scanner; +import java.util.Stack; +public class Main { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + while (in.hasNextInt()) { + int n = in.nextInt(); + if (n == 0) break; + int nums[] = new int[n]; + for (int i = 0; i < n; i++) { + nums[i] = in.nextInt(); + } + Stack stack = new Stack<>(); //用栈进行模拟 + int j = 0; + for (int i = 0; i < n; i++) { + stack.push(i + 1); //1-n顺序入栈 + while (!stack.isEmpty() && stack.peek() == nums[j]) { + stack.pop(); + j++; + } + } + if (stack.isEmpty()) System.out.println("Yes"); + else System.out.println("No"); + } + } +} +``` ## python ```python -- Gitee