This is the 20th day of my participation in the August More Text Challenge


Related articles

LeetCode brush questions summary: LeetCode brush questions

1. Title Description


Gems and Stones

Given the string J for the type of gem in the stone, and the string S for the stone you own. Each character in S represents a type of stone you own, and you want to know how many of the stones you own are gems.

The letters in J are not repeated, and all characters in J and S are letters. The letters are case sensitive, so “A” and “A” are different types of stone.

Start with simple questions to brush, exercise their thinking ability, for the interview preparation ~

Second, train of thought analysis


  • Look at the examples in the title, let’s clarify the idea ~

  • Example 1:

    Input: J = "aA", S = "aAAbbbb" output: 3Copy the code
  • Example 2:

    Input: J = "z", S = "ZZ" Output: 0Copy the code
  • The topic is very simple, we can first follow the topic of thought ~

  • First determine what kind of gems we have.

  • And then we loop in the comparison, and if we’re right, we add one to the total.

  • Finally return the total number can!

AC code


  • Brute force cracking:

    Class Solution {public numJewelsInStones(String J, String S) {public numJewelsInStones(String J, String S) {public numJewelsInStones(String J, String S); Int [] type = new int[256]; for(int i = 0; i < len; i++){ type[J.charAt(i)] = 1; } int ans = 0; len = S.length(); for(int i = 0; i < len; i++){ ans += type[S.charAt(i)]; } return ans; }}Copy the code
    • The charm of violence aesthetics ~
    • The idea of brute force is pretty straightforward, iterate over the string, for each character in stones, iterate over the string jewels, if it’s the same as one of the characters in Jewels, it’s a jewel.

Four,

  • Thousands of ideas to solve the problem, whether this method is good, or whimsical solution, can solve is a good way! White cat black cat can catch mice cat is a good cat!
  • Here are a few other LeetCode solutions for reference!
  • Click to jump: official solution
  • Click jump: Draw solution algorithm

I see a long way to go, I’m sure I’ll keep searching ~ ** If you think I’m a good blogger! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah