leetcode数组或者字符串常用方法总结

1.暴力法

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

public class Solution {
    public List<String> restoreIpAddresses(String s) {
        List<String> res = new ArrayList<String>();
        int len = s.length();
        for(int i = 1; i<4 && i<len-2; i++){
            for(int j = i+1; j<i+4 && j<len-1; j++){
                for(int k = j+1; k<j+4 && k<len; k++){
                    String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
                    if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){
                        res.add(s1+"."+s2+"."+s3+"."+s4);
                    }
                }
            }
        }
        return res;
    }
    public boolean isValid(String s){
        if(s.length()>3 || s.length()==0 || (s.charAt(0)=='0' && s.length()>1) || Integer.parseInt(s)>255)
            return false;
        return true;
    }
}
String操作中 subString不包括后面的

2.排序的思想在贪心算法中还是很常见的

3.在数组不需要排序时有需要o(n)复杂度时,就要找到题目的规律,考虑到左右两边的最大值或者最小值的思想有时有用。

比如:leetcode 42.Trapping Rain Water(数组注水量多少)

此题的思路:对每一个位置的水面高度,需要同时考虑它左边能承受的最大高度和右边能承受的最大高度,再取min.

再比如:Longest Consecutive Sequence

此题的思路:We will use HashMap. The key thing is to keep track of(记录) the sequence length and store that in the boundary(边界) points of the sequence. For example, as a result, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5.因为下一次肯能有连接的时候只会和边界的值进行连接,所以能够达到准确记录sequence的长度问题。达到复杂度是o(n).

4.关于数组中连续子串的长度的问题

参考:Longest Consecutive SequenceFirst_Missing_Positive都有用到

主要代码如下:

for (int i = 0; i < k; i++) {
    if (!map.containsKey(m[i])) {
        int left = map.containsKey(m[i] - 1) ? map.get(m[i] - 1) : 0;
        int right = map.containsKey(m[i] + 1) ? map.get(m[i] + 1) : 0;
        int sum = left + right + 1;
        map.put(m[i], sum);

        map.put(m[i] - left, sum);
        map.put(m[i] + right, sum);

    } else {
        continue;
    }
}

5.正则表达式的使用

第一题:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

解答:此题字符串中可能有标点符号或者空格,所以需要先清洗字符串,使用到正则表达式。replaceAll的第一个参数可以是正则表达式

    public boolean isPalindrome(String s) {
        String actual = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
        String rev = new StringBuffer(actual).reverse().toString();
        return actual.equals(rev);
    }

第二题:

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".
解答:

public boolean detectCapitalUse(String word) { return word.matches("[A-Z]+|[a-z]+|[A-Z][a-z]+");}











  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值