LeetCode - 771. Jewels and Stones

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。


You’re given strings J representing the types of stones that are jewels, and S representing 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


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




class Solution {
    public int numJewelsInStones(String J, String S) {
        int count = 0;
        for (int i = 0; i < S.length(); i++) {
            if (J.indexOf(S.charAt(i)) >= 0)
        return count;



class Solution {
    public int numJewelsInStones(String J, String S) {
        return S.replaceAll("[^" + J + "]", "").length();




Runtime: 1 ms
Memory Usage: 33.7 MB


Runtime: 7 ms
Memory Usage: 34.9 MB



Heavenly Jewels


There is a flat island whose shape is a perfect square. On this island, there are three habitants whose names are IC, PC, and ACM. Every day, one jewel is dropped from the heaven. Just as the jewel touches the ground, IC, PC, and ACM leave their houses simultaneously, run with the same speed, and then a person who first touched the jewel can get the jewel. Hence, the person whose house is nearest to the location of the jewel is the winner of that day.nThey always have a quarrel with each other, and therefore their houses are located at distinct places. The locations of their houses are fixed. This jewel falls at a random point on the island, that is, all points on the island have even chance.nnWhen there are two or more persons whose houses are simultaneously nearest, the last person in the order ofnnIC, PC, ACM nnobtains the jewel. nnOur interest is to know the probability for IC to get the jewel under this situation.nnnInputnnThe input describes one problem instance per line. Each line contains the x- and y-coordinates of IC's home, those of PC's, and those of ACM's in this order. Note that the houses of IC, PC and ACM are located at distinct places. The end of the input is indicated by a line with six zeros.nnThe coordinates of the whole island are given by (0,10000) times (0,10000) and coordinates of houses are given in integer numbers between 1 and 9999, inclusive. It should be noted that the locations of the jewels are arbitrary places on the island and their coordinate values are not necessarily integers.nnnOutputnnFor each input line, your program should output its sequence number starting from 1, and the probability for the instance. The computed probability values should have errors less than 10^-5.nnThe sequence number and the probability should be printed on the same line. The two numbers should be separated by a space.nnnSample Inputnn2000 2000 8000 8000 9000 9500n2500 2500 7500 2500 2500 7500n0 0 0 0 0 0nnnSample Outputnn1 0.50000n2 0.25000 问答

Throw the Stones


Problem Descriptionn  Remember our childhood? A few naked children throw stones standing on the same position, the one throws farther win the game. Aha, of course, there are some naughty boys who care more about whether they can urinate father.n  You believe it or not, anyway, I believed. Nowadays, some of the children are smarter than we were, while others may be more naughty.n  A week ago, I saw several children throw stones. In fact, they are more clever than we were, since the game they played, apparently, is more complex than we did. Maybe you have different points of view, however, you’d better learn about the rules of the game before expressing your views. A group of children take turns to throw stones standing on the same position. After some child throw a stone, the children will draw a convex polyhedron with smallest volume together to enclose all the stones thrown by them. You may assume that the stone is so small as to be abstracted as a point in three-dimensional space. Naively, the children regard the space enclosed by the convex polyhedron as territory under their control. After a child throw his stone, the score he obtains equals the incremental of the volume of their territory. Unfortunately, the first three child’s score will always be zero. At last, the child with the highest score will win the game, and known as the "King".n  I think you have accepted my opinion already, for the rules of their throwing stones game are really complicated. But, you also don’t need to be frustrated for it. Now, in order to show you are smarter, maybe you can write a program to help the children point out their "King".n nnInputn  Input consists of a number of cases. The data of each case appears on a number of input lines, the first of which contains an integer N. The following N lines contain three number (xi, yi, zi) indicating coordinates of the stone thrown by the i-th child. nNote: 1 <= N <= 10^4, 1 <= i <= N, -10^4 <= xi , yi , zi <= 10^4.n nnOutputn  For each test case, you should output two lines. The first line is "Case #K:", K means the number of the test case. The second line is "i v", i means index of the "King" and v means the score of the "King". If there are more than one "King", output the one throws stone earlier than others.n  Please round the result to 2 digits after decimal point if necessary.n nnSample Inputn4n1 0 0n1 1 0n0 1 0n0 0 1n5n1 0 0n1 1 0n0 1 0n0 0 0n0 0 1n nnSample OutputnCase #1:n4 0.17nCase #2:n5 0.33 问答

Collect More Jewels


It is written in the Book of The Lady:nAfter the Creation, the cruel god Moloch rebellednagainst the authority of Marduk the Creator.nMoloch stole from Marduk the most powerful of allnthe artifacts of the gods, the Amulet of Yendor,nand he hid it in the dark cavities of Gehennom, thenUnder World, where he now lurks, and bides his time.nnYour goddess The Lady seeks to possess the Amulet, and with it to gain deserved ascendance over the other gods.nnYou, a newly trained Rambler, have been heralded from birth as the instrument of The Lady. You are destined to recover the Amulet for your deity, or die in the attempt. Your hour of destiny has come. For the sake of us all: Go bravely with The Lady!nnIf you have ever played the computer game NETHACK, you must be familiar with the quotes above. If you have never heard of it, do not worry. You will learn it (and love it) soon.nnIn this problem, you, the adventurer, are in a dangerous dungeon. You are informed that the dungeon is going to collapse. You must find the exit stairs within given time. However, you do not want to leave the dungeon empty handed. There are lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and some are more expensive. So you will try your best to maximize your collection, more importantly, leave the dungeon in time.nnInput DescriptionnnStandard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.nnThe first line of each test case contains four integers W (1 <= W <= 50), H (1 <= H <= 50), L (1 <= L <= 1,000,000) and M (1 <= M <= 10). The dungeon is a rectangle area W block wide and H block high. L is the time limit, by which you need to reach the exit. You can move to one of the adjacent blocks up, down, left and right in each time unit, as long as the target block is inside the dungeon and is not a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels will be collected once the adventurer is in that block. This does not cost extra time.nnThe next line contains M integers��which are the values of the jewels.nnThe next H lines will contain W characters each. They represent the dungeon map in the following notation:n> [*] marks a wall, into which you can not move;n> [.] marks an empty space, into which you can move;n> [@] marks the initial position of the adventurer;n> [<] marks the exit stairs;n> [A] - [J] marks the jewels.nnOutput DescriptionnnResults should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.nnIf the adventurer can make it to the exit stairs in the time limit, print the sentence "The best score is S.", where S is the maximum value of the jewels he can collect along the way; otherwise print the word "Impossible" on a single line.nnSample Inputnn3nn4 4 2 2n100 200n****n*@A*n*B<*n****nn4 4 1 2n100 200n****n*@A*n*B<*n****nn12 5 13 2n100 200n************n*B.........*n*.********.*n*@...A....<*n************nnSample OutputnnCase 1:nThe best score is 200.nnCase 2:nImpossiblennCase 3:nThe best score is 300. 问答

Collecting Stones


Trudy is playing a phone game called Collecting Stones. In this game, a robert is placed on the upper-left corner of the board, which consists of 8x8 grids. On each grid, there are less than 2000001 stones. The robert can move up, down, right, up-right and down-right, but can not move into a grid that has already been passed before. All the stones on the grids which were passed by the robert are collected and Trudy is want to know if it is possible to collect exactly M number of stones when the robert arrives at the lower-right corner.nnInput:nnThe first line of input contains a single integer T(T<=20), the number of testcases. In the first line of each testcase, there is a number M described above, and follows 8x8 numbers, which represents the number of stones on each grid. Note that M will not greater than the sum of all stones on the board.nnOutput:nnFor each testcase, output "Yes" if there exists a path for the robert to collect exactly M number of stones, output "No" otherwise.nnSample Input:n2n374n1 2 3 4 5 6 7 8n9 10 11 12 13 14 15 16n17 18 19 20 21 22 23 24n25 26 27 28 29 30 31 32n33 34 35 36 37 38 39 40n41 42 43 44 45 46 47 48n49 50 51 52 53 54 55 56n57 58 59 60 61 62 63 64n2032n1 2 3 4 5 6 7 8n9 10 11 12 13 14 15 16n17 18 19 20 21 22 23 24n25 26 27 28 29 30 31 32n33 34 35 36 37 38 39 40n41 42 43 44 45 46 47 48n49 50 51 52 53 54 55 56n57 58 59 60 61 62 63 64nSample Output:nYesnNonNote for the first sample: A possible path is 1, 10 18, 19, 20, 29, 38, 31, 40, 48, 56, 64. 问答