50,528
社区成员
发帖
与我相关
我的任务
分享
public static boolean checkIsBracketsTwining(String sourceString,
Character[] tags) {
if (tags.length != 2)
return false;
Stack<Character> stack = new Stack<Character>();
boolean isValid = true;
for (Character c : sourceString.toCharArray()) {
if (!stack.isEmpty() && stack.peek() == tags[0]
&& c == tags[1]) {
isValid = true;
stack.pop();
} else {
stack.push(c);
isValid = !(c == tags[0] || c == tags[1]);
}
}
return isValid;
}
下班时候网络不好每提交成功,看来已经倍解答玩了,思路就是栈的操作,碰到哦成对弹出去,然后判断
public static int testString(String input){
int flag=0;
//转化成char数组
char[] oldChar=input.toCharArray();
//new一个新数组 存放 "[" "(" ")" "]"
char[] newChar=new char[oldChar.length];
//遍历char数组 如果存在括号 则存入新的char[]中
for(int i=0;i<oldChar.length;i++ ){
if(oldChar[i] =='[' || oldChar[i] ==']' || oldChar[i] =='(' || oldChar[i] ==')'){
newChar[flag]=oldChar[i];
flag++;
}
}
//将新的数组转换成String 如果String中存在连续的 "[]" 或者"()" 则返回1 否则返回0
String result=new String(newChar);
if(result.indexOf("[]") !=-1 || result.indexOf("()") != -1){
return 1;
}
return 0;
}
public static void main(String[] args) {
String str=args[0];
String kuehaod = ".*(\\[\\]|\\(\\)).*"; //匹配有连续配对的括号
String kuehaom = ".*(\\(|\\)|\\[|\\]).*"; //匹配有任意括号
//如果有连续配对的括号直接回返0,没有的话就检查字符串有没有包括任意括号
int result = str.matches(kuehaod)?0:str.matches(meikuehaom)?1:0;
System.out.println(result);
}
string str = "qweqwe[])(wq";
if (str.IndexOf(')') > -1 && str.IndexOf('(') > -1 && str.IndexOf('[') > -1 && str.IndexOf(']') > -1)//[]()都有
{
str.ToArray();
int sum = 0;
foreach (char c in str)
{
if (c == '[' || c == ']' || c == '(' || c == ')')
sum++;
}
if (sum == 4)//括号没有重复
{
int[] arr = new int[4];
arr[0] = str.IndexOf('[');
arr[1] = str.IndexOf(']');
arr[2] = str.IndexOf('(');
arr[3] = str.IndexOf(')');
int temp;
for (int i = 1; i < arr.Length; i++) //冒泡
{
for (int j = 0; j < arr.Length - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
if (arr[1] == str.IndexOf(']') && arr[2] == str.IndexOf('['))//括号错误情况(中间2位) ][ )( [) (]
Console.WriteLine(1);
else if(arr[1] == str.IndexOf(')') && arr[2] == str.IndexOf('('))
Console.WriteLine(1);
else if(arr[1] == str.IndexOf('[') && arr[2] == str.IndexOf(')'))
Console.WriteLine(1);
else if(arr[1] == str.IndexOf('(') && arr[2] == str.IndexOf(']'))
Console.WriteLine(1);
else
Console.WriteLine(0);
}
else
Console.WriteLine(1);
}
else if(str.IndexOf('[')<0 && str.IndexOf(']')<0 && str.IndexOf('(') < 0 && str.IndexOf(')') < 0)//没有括号
{
Console.WriteLine(0);
}
else
Console.WriteLine(1);
public int checkStr(String s){
int resultValue = 0; // 初始返回值
LinkedList<Byte> ll = new LinkedList<Byte>(); // 用于存放括号的栈
byte[] bs = s.getBytes();
// 遍历String
for (byte b : bs) {
// 如果是左括号,存入栈
if (b == '(' || b == '[') {
ll.add(b);
} else if (b == ')') {
// 如果是‘)’,取栈中最后的括号,判断是否匹配
// 若不匹配,则返回1;
if (ll.getLast() != '(') {
resultValue = 1;
break;
}
} else if (b == ']') {
if (ll.getLast() != '[') {
resultValue = 1;
break;
}
}
}
return resultValue;
}
public static int isMatch(String str){
int result = 0;
LinkedList<Character> list = new LinkedList<Character>();
if(str != null && str.trim().length() > 0){
char[] chars = str.toCharArray();
for(Character c : chars){
if(c == '[' || c == '('){
list.push(c);
}
if(c == ')'){
Character character = list.pop();
if(!(character == '(')){
result = 1;
break;
}
}
if(c == ']'){
Character character = list.pop();
if(!(character == '[')){
result = 1;
break;
}
}
}
}
if(list.size() > 0){
result = 1;
}
return result;
}
public class testHuawei {
public static void main(String[] args) {
testHuawei t=new testHuawei();
System.out.println(t.find("asdlfjh{(})"));
System.out.println(t.find("asdlfjh"));
System.out.println(t.find("asdlfjh()"));
}
public int find(String inputstr){
String s;
int a = 1;
if(inputstr.contains("[")){
if(inputstr.contains("]")){
if(inputstr.indexOf("[") < inputstr.indexOf("}")){
s = inputstr.substring(inputstr.indexOf("["),inputstr.indexOf("}")+1);
if(s.contains("(")){
if(s.contains(")")){
if(s.indexOf("(") < s.indexOf(")")){
a = 0;
}
else{
a = 1;
}
}else{
a = 1;
}
}else{
a = 1;
}
}else{
a = 1;
}
}else{
a = 1;
}
}else{
if(!inputstr.contains("]") && !inputstr.contains("(") && !inputstr.contains(")"))
{
a = 0;
}
}
return a;
}
}
运行结果101,正确的是100