zingyuk的Java小练习--demo01

2018.12.07/00:41/今天是第一次注册使用HackerRank

以下是题目描述:约翰在一家服装店工作。他有一大堆需要靠 颜色来配对才能销售的袜子。给出一个整型数组
代表每只袜子的颜色,计算通过相同的颜色配对可以配对出几双袜子。
John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
举个例子,这里有7只袜子分别对应颜色是【1,2,1,2,1,3,2】,在这个数组里面正好有一双颜色为1的袜子和一双颜色为2 的袜子,剩下了3只无法配对的,所以这里可以配对出2双袜子。
For example, there are n=7 socks with colors ar=[1,2,1,2,1,3,2] . There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is .

Function Description
功能描述
Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.
*在下面的编辑器中完成袜子商人这个方法。它必须返回一个表示匹配出袜子对数的整型变量
sockMerchant has the following parameter(s)
袜子商人方法拥有以下参数
n: the number of socks in the pile
ar: the colors of each sock
*

n:堆中袜子的数量(只)
ar:每只袜子的颜色()

Input Format

The first line contains an integer n , the number of socks represented in ar.
The second line contains space-separated integers describing the colors ar[i] of the socks in the pile.

Constraints
1<=n<=100
1<=ar[i]<=100 where 0<=i<n
Output Format

Print the total number of matching pairs of socks that John can sell.

Sample Input
输入样式
9
10 20 20 10 10 30 50 10 20

Sample Output
输出样式
3

Explanation//简单来说就是数组内相同元素两两凑一对
在这里插入图片描述
John can match three pairs of socks.

以下为原题目:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the sockMerchant function below.
    static int sockMerchant(int n, int[] ar) {
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] ar = new int[n];

        String[] arItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arItem = Integer.parseInt(arItems[i]);
            ar[i] = arItem;
        }

        int result = sockMerchant(n, ar);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

答案解析:
To solve this challenge, we go through each color i and count its frequency, f(i) Once we’ve calculated all the frequencies, we calculate the number of pairs of each kind of sock as f(i)/2 (using integer division). Finally, we print the total sum of all pairs of socks.

这里是答案:

import java.util.*;

class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        HashMap<Integer, Integer> colors = new HashMap<Integer, Integer>();
        
        while(n-- > 0) {
            int c = scan.nextInt();
            Integer frequency = colors.get(c);
            
            // If new color, add to map
            if(frequency == null) {
                colors.put(c, 1);
            }
            else { // Increment frequency of existing color
                colors.put(c, frequency + 1);
            }
        }
        scan.close();
        
        // Count and print the number of pairs
        int pairs = 0;
        for(Integer frequency : colors.values()) {
            pairs += frequency >> 1;
        }
        System.out.println(pairs);
    }
}

对于这个答案其实我是看不太懂的,毕竟刚学Java2个月。

  1. while(n-- > 0)
    这个代码块从来没见过,百度了一下稍微懂了一点,但是还是有待理解。
  2. 之前还看到了有人用hashmap来做但是好像不可以这样操作。

以上内容均是摘抄自HackerRank。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值