【LeetCode】Day 1

771. Jewels and Stones

给一字符串 J (大小写敏感)和一个字符串 S, 求S中包含多少J中的字符。
例如:
输入: J = “aA”, S = “aAabbc”,
输出: 3
解释: S中出现’a’, 'A’一共3次

    public int numJewelsInStones(String J, String S) {
        int r = 0;
        for (char c : S.toCharArray()) {//遍历S
            if (J.indexOf(c) != -1) {//判断J中是否包含,有则计数+1
                r++;
            }
        }
        return r;
    }

上述复杂度为O(M*N)
判断J中是否包含那里,可以先把J中的字符放到Set中,这样每次判断是否存在就为O(1)
优化后如下

    public int numJewelsInStones(String J, String S) {
        int r = 0;
        Set<Character> set = new HashSet<>();
        for (char c : J.toCharArray()) {//存入Set
            set.add(c);
        }
        for (char c : S.toCharArray()) {//遍历S
            if (set.contains(c)) {//判断J中是否包含,有则计数+1
                r++;
            }
        }
        return r;
    }

929. Unique Email Addresses

每个电子邮箱以@分隔,分为前缀和后缀。除了小写字母,还能包含"." , “+”, 前缀中,无视任何"." , 无视"+"号后面的字母
例如:
输入: [a.bc@qq.asd.com, ab.c+ssads@qq.com, abc@qq.com]
输出: 2
解释: [abc@qq.asd.com, abc@qq.com]两个

    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for (String email : emails) {
            String[] ss = email.split("@");//以@分隔
            //只处理@前面的部分
            ss[0] = ss[0].replaceAll(".", "").split("+")[0];//先把所有"."去除,然后以"+"分隔,只取"+"前面部分
            set.add(ss[0] + ss[1]);//放进Set中,去重
        }
        return set.size();
    }

709. To Lower Case

字符串转小写

    public String toLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            //或运算0x20(32)(0b0010_0000),相当于把二进制第6位变为1,Ascii码中这一位大写字符都为0,小写都为1,恰好对应。"或"0x20变1转小写,"与"0xDF(0b1101_1111)变0转大写,"异或"0x20大小写互转。
            sb.append((char) (c | 0x20));
        }
        return sb.toString();
    }

944. Delete Columns to Make Sorted

给一个字符串数组,每个字符串长度都相同。给一个删除索引序列 {i},删除每个字符串中第 i 个字符。。
例如: A = [
“abcdef”,
“uvwxyz”
]
删除索引为 {0, 2, 3}的字符后后
A = [
“bef”,
“vyz”
]
从上往下为阅读[“b”, “v”], [“e”, “y”], [“f”, “z”] ,为非降序字符串(b < v, e < y, f < z),
求最少删除多少个,能使剩下的字符串为非降序(即a <= b)

    public int minDeletionSize(String[] A) {
        int c = 0;
        int len = A.length;
        for (int col = 0; col < A[0].length(); col++) {//列
            for (int row = 0; row < len - 1; row++) {//行
                //如果 A[row][col] > a[row+1][col],说明这一列有降序的字符,根据题意要把这列删了,但题目只需要记录数量,所以只需计数器+1
                if (A[row].charAt(col) > A[row + 1].charAt(col)) {
                    c++;
                    break;
                }
            }
        }
        return c;
    }

804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: “a” maps to “.-”, “b” maps to “-…”, “c” maps to “-.-.”, and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-…","-.-.","-…",".","…-.","–.","…","…",".—","-.-",".-…","–","-.","—",".–.","–.-",".-.","…","-","…-","…-",".–","-…-","-.–","–…"]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cba” can be written as “-.-…–…”, (which is the concatenation “-.-.” + “-…” + “.-”). We’ll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.

Example:
Input: words = [“gin”, “zen”, “gig”, “msg”]
Output: 2
Explanation:
The transformation of each word is:
“gin” -> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…--.”
“msg” -> “–…--.”
There are 2 different transformations, “–…-.” and “–…--.”.

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

给出了字符和MorseCode的对应关系,把相应的字符串替换为MorseCode,然后去重

    public int uniqueMorseRepresentations(String[] words) {
        String[] arr = {
                ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
                ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
                "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
        };
        Set<String> set = new HashSet<>();
        StringBuilder sb;
        for (String word : words) {
            sb = new StringBuilder();
            for (char c : word.toCharArray()) {
                sb.append(arr[c - 'a']);//每个字符都替换为相应的Morse Code
            }
            set.add(sb.toString());//去重
        }
        return set.size();
    }

905. Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.
Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

给定一个数组,偶数排前面,奇数排后面

    public int[] sortArrayByParity(int[] A) {
        for (int i = 0, j = A.length - 1; i < j; ) {//从两边向中间遍历
            while (i < j && A[i] % 2 == 0) i++;//从左边开始直到遇到奇数或相遇停下
            while (i < j && A[j] % 2 == 1) j--;//从右边开始直到遇到偶数或相遇停下
            if (i < j) {//交换
                A[i] = A[i] + A[j];
                A[j] = A[i] - A[j];
                A[i] = A[i] - A[j];
            }
        }
        return A;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值