摘要:循環(huán)每個(gè)元素放入堆棧或比較是否和棧頂元素成對(duì)。循環(huán)結(jié)束后,若未拋出,且堆棧為空,說(shuō)明所有都已一一對(duì)應(yīng)。
Problem
Given a string containing just the characters "(", ")", "{", "}", "[" and "]", determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Note建立堆棧stack。循環(huán)每個(gè)元素放入堆棧或比較是否和棧頂元素成對(duì)。
循環(huán)結(jié)束后,若未拋出false,且堆棧為空,說(shuō)明所有parenthese都已一一對(duì)應(yīng)。
public class Solution { public boolean isValid(String s) { Mapif-else branchesmap = new HashMap<>(); map.put("(", ")"); map.put("[", "]"); map.put("{", "}"); Stack stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { Character ch = s.charAt(i); switch (ch) { case "{": case "[": case "(": stack.push(ch); break; case ")": case "}": case "]": if (stack.isEmpty() || ch != map.get(stack.pop())) return false; } } return stack.isEmpty(); } }
public class Solution { /** * @param s: A string * @return: whether the string is a valid parentheses */ public boolean isValidParentheses(String s) { char[] str = s.toCharArray(); if (str.length % 2 != 0) return false; Stackstack = new Stack<>(); for (char ch: str) { if (ch == "(" || ch == "[" || ch == "{") { stack.push(ch); } else { if (stack.isEmpty()) { return false; } else { char top = stack.pop(); if ((ch == ")" && top != "(") || (ch == "]" && top != "[") || (ch == "}" && top != "{")) { return false; } } } } return stack.isEmpty(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/64936.html
摘要:在問(wèn)題中,我們可以用來(lái)檢驗(yàn)括號(hào)對(duì),也可以通過(guò)來(lái)檢驗(yàn)。遇到就加一,遇到就減一。找到一對(duì)括號(hào)就在最終結(jié)果上加。我們用來(lái)表示當(dāng)前位置的最長(zhǎng)括號(hào)。括號(hào)之間的關(guān)系有兩種,包含和相離。 Longest Valid Parentheses Given a string containing just the characters ( and ), find the length of the lon...
摘要:假設(shè)是從下標(biāo)開(kāi)始到字符串結(jié)尾最長(zhǎng)括號(hào)對(duì)長(zhǎng)度,是字符串下標(biāo)為的括號(hào)。如果所有符號(hào)都是,說(shuō)明是有效的。 Longest Valid Parentheses Given a string containing just the characters ( and ), find the length of the longest valid (well-formed) parentheses...
Problem Given a string containing just the characters ( and ), find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: (()Output: 2Explanation: The longest valid pa...
摘要:如棧中是,來(lái)一個(gè)變成,再來(lái)一個(gè),變成。注意棧在或者操作之前要驗(yàn)證非空,否則會(huì)拋出。代碼最后要判斷棧的大小,如果循環(huán)結(jié)束后棧內(nèi)還有元素,說(shuō)明也是無(wú)效的代碼 Valid Parentheses Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is...
摘要:題目要求原題地址一個(gè)括號(hào)序列,求出其中成對(duì)括號(hào)的最大長(zhǎng)度思路一使用堆棧這題可以參考我的另一篇博客這篇博客講解了如何用堆棧判斷括號(hào)序列是否可以成對(duì)。我們可以將堆棧的思路延續(xù)到這里。在這里需要先遍歷一遍字符串,再遍歷一下非空的堆棧。 題目要求 原題地址:https://leetcode.com/problems... Given a string containing just the c...
閱讀 1634·2021-10-25 09:46
閱讀 3229·2021-10-08 10:04
閱讀 2376·2021-09-06 15:00
閱讀 2777·2021-08-19 10:57
閱讀 2084·2019-08-30 11:03
閱讀 980·2019-08-30 11:00
閱讀 2384·2019-08-26 17:10
閱讀 3554·2019-08-26 13:36