字符串(简单)

image.png
public boolean isValid(String s) {
        Stack stack = new Stack();
        for(char c: s.toCharArray()){
            if(c=='(')stack.push(')');
            else if(c=='[')stack.push(']');
            else if(c=='{')stack.push('}');
            else if(stack.isEmpty()||c!=stack.pop()) return false;
        }
        return stack.isEmpty();
    }
image.png
public boolean wordPattern(String pattern, String str) {
        String[] s=str.split(" ");
        if(pattern.length()!=s.length)
            return false;
        HashMap map=new HashMap();
        for(int i=0;i
image.png
HashMap dic;/*=new HashMap<>();*/
    public void WordsFrequency(String[] book) {
        for(String word:book)
        {
            dic.put(word,dic.getOrDefault(word,0)+1);
        }
    }
    public int get(String word) {
        return  dic.getOrDefault(word,0);
    }

反转单词顺序(内部字符不变,空格压缩为一个)

public String reverseWords(String s) {
        //将传进来的字符串以空格拆分
        String[] strings = s.trim().split(" ");
        StringBuffer stringBuffer = new StringBuffer();
        //从尾巴开始遍历
        for (int i = strings.length - 1; i >= 0; i--) {
            if (strings[i].equals("")) {
                continue;
            }
            //到头了,append然后去空格
            if (i == 0) {
                stringBuffer.append(strings[i]);
            } else {
                // 怕有多余的空格,去掉,再加上去
                stringBuffer.append(strings[i]).append(" ");
            }
        }
        //输出String 完事,安排!
        return stringBuffer.toString();
    }
image.png
  • @ACSII

//100% & 25%
public char findTheDifference1(String s, String t) {
        int sum=0;
        for(char a:t.toCharArray())
        {
            sum+=a;
        }
        for(char a:s.toCharArray())
        {
            sum-=a;
        }
        return (char)sum;
    }
  • @异或

 public char findTheDifference(String s, String t) {
        char res = 0;
        int lens = s.length();
        for (int i = 0; i < lens; i ++) {
            res ^= s.charAt(i)^ t.charAt(i);
        }
        res ^= t.charAt(lens);
        return res;
    }
  • @哈希字典

public char findTheDifference1(String s, String t) {
        int[] record = new int[26];
        int lenS = s.length();
        int lenT = t.length();
        for (int i = 0; i < lenS; i++){
            record[s.charAt(i) - 'a'] += 1;
        }
        for (int i = 0; i < lenT; i++){
            record[t.charAt(i) - 'a'] -= 1;
            if (record[t.charAt(i) - 'a'] < 0) return t.charAt(i);

        }
        return ' ';
    }
image.png
public int lengthOfLongestSubstring(String s) {
        int res = 0;
        Set set = new HashSet<>();
        for(int l = 0, r = 0; r < s.length(); r++) {
            char c = s.charAt(r);
            while(set.contains(c)) {
                set.remove(s.charAt(l++));
            }
            set.add(c);
            res = Math.max(res, r - l + 1);
        }
        return res;
    }
image.png
 //双指针,99.8%
    public boolean isPalindrome(String s) {
        if(s == null){
            return true;
        }
        s = s.toLowerCase();
        char[] array1 = s.toCharArray();
        int low = 0;
        int high = array1.length-1;
        while(low < high)
        {
            if((array1[low] >= '0' && array1[low] <= '9') 
                    || (array1[low] >= 'a' && array1[low] <= 'z'))
            {
                if((array1[high] >= '0' && array1[high] <= '9') 
                        || array1[high] >= 'a' && array1[high] <= 'z')
                {
                    if(array1[low] != array1[high])
                    {
                        return false;
                    }else{
                        low++;
                        high--;
                    }
                }else{
                    high--;
                }
            }else{
                low++;
            }
        }
        return true;
    }
//10ms   25%
    public boolean isPalindrome(String s) {
        Stack c1 = new Stack<>();
        Stack c2 = new Stack<>();
        char[] ch1 = s.toCharArray();
        int n=ch1.length;
        for(int i=0;i='a'&&ch1[i]<='z')||(ch1[i]>='0')&&(ch1[i]<='9'))
                c1.push(ch1[i]);
            if(ch1[i]>='A'&&ch1[i]<='Z')
                c1.push((char)(ch1[i]+32));
        }
        for(int i=n-1;i>=0;i--)
        {
            if((ch1[i]>='a'&&ch1[i]<='z')||(ch1[i]>='0')&&(ch1[i]<='9'))
                c2.push(ch1[i]);
            if(ch1[i]>='A'&&ch1[i]<='Z')
                c2.push((char)(ch1[i]+32));
        }
        while (!c1.isEmpty())
        {
            char t1=c1.pop();
            char t2=c2.pop();
            if(t1!=t2)
            {
                return false;
            }
        }
        return true;
    }
//4ms 75%
    public boolean isPalindrome(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'))) sb.append(c);
            if ((c >= 'A') && (c <= 'Z')) sb.append((char)(c+32));
        };
        return sb.toString().equals(sb.reverse().toString());
    }

你可能感兴趣的:(字符串(简单))