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

Add 1019#160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ashmichheda wants to merge1 commit intofishercoder1534:master
base:master
Choose a base branch
Loading
fromashmichheda:1019
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_1019.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,10 @@

import com.fishercoder.common.classes.ListNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class _1019 {
public static class Solution1 {
public int[] nextLargerNodes(ListNode head) {
Expand DownExpand Up@@ -37,4 +41,26 @@ private int findLength(ListNode head) {
return count;
}
}
public static class Solution2 {
// Store the nodes of linked list into an array list
// Create a stack that stores indexes, which would be needed to find the next greater element of
// element at index i
public int[] nextLargerNodes(ListNode head) {
List<Integer> numList = new ArrayList<>();

for (ListNode temp = head; temp != null; temp = temp.next) {
numList.add(temp.val);
}
Stack<Integer> stack = new Stack<>();
int result[] = new int[numList.size()];
for (int i = 0; i < numList.size(); i++) {

while (!stack.isEmpty() && numList.get(stack.peek()) < numList.get(i)) {
result[stack.pop()] = numList.get(i);
}
stack.push(i);
}
return result;
}
}
}
8 changes: 7 additions & 1 deletionsrc/test/java/com/fishercoder/_1019Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,10 +10,11 @@

public class _1019Test {
private static _1019.Solution1 solution1;

private static _1019.Solution2 solution2;
@BeforeClass
public static void setup() {
solution1 = new _1019.Solution1();
solution2 = new _1019.Solution2();
}

@Test
Expand All@@ -34,4 +35,9 @@ public void test3() {
assertArrayEquals(new int[]{7, 9, 9, 9, 0, 5, 0, 0}, solution1.nextLargerNodes(head));
}

@Test
public void test4() {
ListNode head = LinkedListUtils.contructLinkedList(new int[]{2, 7, 4, 3, 5});
assertArrayEquals(new int[]{7, 0, 5, 5, 0}, solution2.nextLargerNodes(head));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp