• Two Pointers Day


    (1)Reverse String

    解题思路简单明了,但是要注意时间复杂度问题!!!

    代码如下:(声明一个与字符串等长的char数组,然后倒序区字符串中的字符,放入数组即可。)

     1 public class Solution {
     2     public String reverseString(String s) {
     3         char[] chars = new char[s.length()];  
     4         int index = 0;  
     5         for (int i = s.length() - 1; i >= 0; i--) {  
     6             chars[index++] = s.charAt(i);  
     7         }  
     8         return new String(chars);  
     9     }
    10 }
    View Code

    (2) Reverse Vowels of a String

    解题思路一:

    使用start和end两个指针,依次找到最前端和最后端的两个元音字母互换位置,然后start++,end-- ,直至start不小于end结束即可。

    代码如下:

     1 public class Solution {
     2     public String reverseVowels(String s) {
     3         if(s == null || s.length()==0) return s;
     4         String vowels = "aeiouAEIOU";
     5         char[] chars = s.toCharArray();
     6         int start = 0;
     7         int end = s.length()-1;
     8         while(start<end){
     9             //while(start<end && !vowels.contains(chars[start]+""))
    10             while(start<end && vowels.indexOf(chars[start]) == -1){
    11                 start++;
    12             }
    13             //while(start<end && !vowels.contains(chars[end]+""))
    14             while(start<end && vowels.indexOf(chars[end]) == -1){
    15                 end--;
    16             }
    17             
    18             char temp = chars[start];
    19             chars[start] = chars[end];
    20             chars[end] = temp;
    21             
    22             start++;
    23             end--;
    24         }
    25         return new String(chars);
    26     }
    27 }
    View Code

    解题思路二:

    使用hashset,首先找到所有元音字母所在的位置并记录,然后依然是最前最后的元素进行互换。

    代码如下:

    public class Solution {
        public String reverseVowels(String s) {
            int[] pos = new int[s.length()];
            int cnt = 0;
            HashSet<Character> vowel = new HashSet<Character>();
            vowel.add('a');
            vowel.add('e');
            vowel.add('i');
            vowel.add('o');
            vowel.add('u');
            vowel.add('A');
            vowel.add('E');
            vowel.add('I');
            vowel.add('O');
            vowel.add('U');
            
            for (int i = 0; i < s.length(); i++) {
                if (vowel.contains(s.charAt(i))) {
                    pos[cnt] = i;
                    cnt++;
                }
            }
            
            char[] ans = new char[s.length()];
            ans = s.toCharArray();
            for (int i = 0; i < cnt; i++) {
                ans[pos[i]] = s.charAt(pos[cnt - i - 1]);
            }
            return String.valueOf(ans);
        }
    }
    View Code

    (3)Valid Palindrome

    解题思路:利用s.replaceAll(字符串要匹配的正则式,替换字符串)来使字符串只含有字母和数字,然后前后依次比较即可。

    代码如下:

     1 public class Solution {
     2     public boolean isPalindrome(String s) {
     3         String regex = "([^A-Za-z0-9])";
     4         String replacement = "";
     5         s = s.replaceAll(regex, replacement);//将不是字母和数字的替换掉
     6         for (int i = 0; i < s.length() / 2; i++) {
     7             if (Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(s.length()-1-i)))
     8                 continue;
     9             else 
    10                 return false;
    11         }
    12         return true;
    13     }
    14 }
    View Code

    (4)Implement strStr()

    strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

    解题思路一:

    在Java中,有一个API函数名indexof(),

    它返回指定子字符串第一次出现在此字符串中的索引。

    代码如下:

    1 public class Solution {
    2     public int strStr(String haystack, String needle) {
    3         int i;  
    4         i = haystack.indexOf(needle);  
    5         return i;  
    6     }
    7 }
    View Code

    解题思路二:不使用indexof()函数。

    从haystack的第一个位置,开始逐个判断是不是子串。如果整个子串都匹配了,那么就返回,否则继续往下挪位置。

     注意要看haystack剩余的长度跟needle比是不是足够多,不够的话也就不用往后比了。

    代码如下:

     1 public class Solution {
     2     public int strStr(String haystack, String needle) {
     3       for (int i = 0; ; i++) {
     4         for (int j = 0; ; j++) {
     5           if (j == needle.length()) {
     6               return i;//返回子字符串第一次出现的位置
     7           }
     8           if (i + j == haystack.length()) {
     9               return -1;
    10           }
    11           if (needle.charAt(j) != haystack.charAt(i + j)) {
    12               break;//跳出内部for循环
    13           }
    14         }
    15       }
    16     }
    17 }
    View Code
  • 相关阅读:
    转 IDEA 解决代码提示功能消失
    模态框居中显示
    DetachedCriteria和Criteria的使用方法
    struts2配置文件详解
    springMVC上传图片
    在linux下运行mongodb
    webSocket客服在线交谈
    接口自动化
    easyui input文本框清除值
    Spring总结
  • 原文地址:https://www.cnblogs.com/struggleli/p/6214392.html
Copyright © 2020-2023  润新知