0915小米笔试

//字符串筛选
public class Main01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] s = sc.nextLine().toCharArray();
        Set<Character> set = new HashSet<>();
        int n = s.length;
        StringBuilder sb = new StringBuilder();
        for(int i=0; i < n; i++) {
            if(!set.contains(s[i]))
                sb.append(s[i]);
            set.add(s[i]);
        }
        System.out.println(sb.toString());
    }
}
//hello, welcome to xiaomi
public boolean isValid(String str) {
    Stack<Character> stk = new Stack<>();
    Map<Character,Character> map = new HashMap<>();
    map.put(')','(');
    map.put(']','[');
    map.put('}','{');
    int n = str.length();
    char[] s = str.toCharArray();
    for(int i=0; i < n; i++) {
        if(s[i] == '(' || s[i] == '{' || s[i] == '[') {
            stk.push(s[i]);
        } else {
            if(stk.isEmpty()) return false;

            if(map.get(s[i]) != stk.peek()) return false;
            else stk.pop();
        }
    }
    return stk.isEmpty();
}
原文地址:https://www.cnblogs.com/lixyuan/p/13676225.html