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 426b4a11c599b43badbc087159e38cdbed8902da..34b3f4344b25c636127a864b6aaf4626bc6fdf91 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