Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfd8d40

Browse files
author
jsquared21
committed
Add Ex 22.01
1 parent27643e4 commitcfd8d40

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
1.45 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*********************************************************************************
2+
* (Maximum consecutive increasingly ordered substring) Write a program that *
3+
* prompts the user to enter a string and displays the maximum consecutive *
4+
* increasingly ordered substring. Analyze the time complexity of your program. *
5+
*********************************************************************************/
6+
importjava.util.*;
7+
8+
publicclassExercise_22_01 {
9+
publicstaticvoidmain(String[]args) {
10+
// Create a Scanner
11+
Scannerinput =newScanner(System.in);
12+
LinkedList<Character>max =newLinkedList<>();
13+
LinkedList<Character>list =newLinkedList<>();
14+
15+
// Prompt the user to enter a string
16+
System.out.print("Enter a string: ");
17+
Stringstring =input.nextLine();
18+
19+
// Find the maximum consecutive increasingly ordered substring
20+
for (inti =0;i <string.length();i++) {// single loop
21+
if (list.size() >1 &&string.charAt(i) <=list.getLast() &&
22+
list.contains(string.charAt(i))) {
23+
list.clear();// Simple statement
24+
}
25+
26+
list.add(string.charAt(i));// Simple statement
27+
28+
if (list.size() >max.size()) {// Simple statement
29+
max.clear();
30+
max.addAll(list);
31+
}
32+
}
33+
34+
// Display the maximum consecutive
35+
// increasingly ordered substring
36+
for (Characterch:max) {// single loop
37+
System.out.print(ch);// Simple statement
38+
}
39+
System.out.println();
40+
}
41+
42+
/*********************************************************************************
43+
* Analyze the time complexity of your program: *
44+
* 1 single loop * 3 simple statements = 3; *
45+
* 1 single loop * 1 simple statement = 1; *
46+
* *
47+
* T(n) = O(n) Linear time; *
48+
*********************************************************************************/
49+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp