Leetcode 771. Jewels and Stones Java解法

You're given strings J representing the types of stones that are jewels, and Srepresenting the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example 1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

---------------------------------------------------------------------------------------------------------------------------

Approach #1: Brute Force 两个for循环搞定

class Solution {
        public int numJewelsInStones(String J, String S){
            int count = 0;
            for (char s: S.toCharArray())
                for (char j: J.toCharArray())
                    if(j == s){
                        count+=1;
                        break;
                    }
            return count;
        }
}

Complexity:

Time: O(J.length * S.length)
Space: O(J.length * S.length), because of the creation of new arrays.(in Java, Strings cannot be modified.)

知识点:

1. Java对String的处理,使用String.toCharArray(),可以把String转化为一个char的数组。

2. 记住这里这种for循环的写法。

 

Approach #2: Hash Set 

class Solution {
    public int numJewelsInStones(String J, String S) {
        Set<Character> Jset = new HashSet();  // declare a Set<element>
        for (char j: J.toCharArray())
            Jset.add(j);                      // add elements to Set

        int ans = 0;
        for (char s: S.toCharArray())
            if (Jset.contains(s))            // Object.contains()
                ans++;
        return ans;
    }
}

Complexity:

Time: O(J.length+S.length), there are two seperate for loops.

Space: O(J.length), because we created a Set object of J.length.

知识点:

1. HashSet的声明方法及Set的一些method的使用,Object.add(), Object.contains().

boolean add(E e)

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

Specified by:

add in interface Collection<E>

Parameters:

e - element to be added to this set

Returns:

true if this set did not already contain the specified element

Throws:

UnsupportedOperationException - if the add operation is not supported by this set

ClassCastException - if the class of the specified element prevents it from being added to this set

NullPointerException - if the specified element is null and this set does not permit null elements 

IllegalArgumentException - if some property of the specified element prevents it from being added to this set

 

boolean contains(Object o)

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

Specified by:

contains in interface Collection<E>

Parameters:

o - element whose presence in this set is to be tested

Returns:

true if this set contains the specified element

Throws:

ClassCastException - if the type of the specified element is incompatible with this set (optional)

NullPointerException - if the specified element is null and this set does not permit null elements (optional)

boolean remove(Object o)

Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Specified by:

remove in interface Collection<E>

Parameters:

o - object to be removed from this set, if present

Returns:

true if this set contained the specified element

Throws:

ClassCastException - if the type of the specified element is incompatible with this set (optional)

NullPointerException - if the specified element is null and this set does not permit null elements (optional)

UnsupportedOperationException - if the remove operation is not supported by this set

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值