C++ Leetcode实现从英文中重建数字

目录
  • 题目
  • 分析
  • 代码

题目

分析

首先我们先分析每个字母的组成,然后发现一些字符只在一个单词中出现,我们先去统计一下这些单词个数。

z,w,u,x,g都只出现在一个数字中,也就是0,2,4,6,8,我们用哈希表统计一下s字符串中各个字符的数量,就可以知道0,2,4,6,8的数量,然后我们注意一下只在两个数字中出现的字符。

  • h 只在 3,8 中出现。由于我们已经知道了 8 出现的次数,因此可以计算出 3 出现的次数。
  • f 只在 4,5 中出现。由于我们已经知道了 4 出现的次数,因此可以计算出 5 出现的次数。
  • s 只在 6,7 中出现。由于我们已经知道了 6 出现的次数,因此可以计算出 7 出现的次数。

此时,只剩下1和9还不知道,但是字符含有o的其他数字我们都已经知道了,那么剩下的数量就是1的数量。

然后此时含有i的就只有9了,统计一下9的数量即可。

统计完次数,按升序排列即可。

代码

C++

我的代码

class Solution {
public:
    string originalDigits(string s) {
        unordered_map<char, int> m;
        string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        string res;

        for(char ch : s) m[ch]++;

        // 0
        if(m['z'] > 0)
        {
            for(int i=0 ; i<m['z'] ; i++) res += '0';
            int x = m['z'];
            m['z'] -= x;
            m['e'] -= x;
            m['r'] -= x;
            m['o'] -= x;
        }

        // 2
        if(m['w'] > 0)
        {
            int x = m['w'];
            for(int i=0 ; i<x ; i++) res += '2';
            m['t'] -= x;
            m['w'] -= x;
            m['o'] -= x;
        }

        // 4
        if(m['u'] > 0)
        {
            int x = m['u'];
            for(int i=0 ; i<x ; i++) res += '4';
            m['f'] -= x;
            m['o'] -= x;
            m['u'] -= x;
            m['r'] -= x;
        }

        // 5
        if(m['f'] > 0)
        {
            int x = m['f'];
            for(int i=0 ; i<x ; i++) res += '5';
            m['f'] -= x;
            m['i'] -= x;
            m['v'] -= x;
            m['e'] -= x;
        }

        // 6
        if(m['x'] > 0)
        {
            int x = m['x'];
            for(int i=0 ; i<x ; i++) res += '6';
            m['s'] -= x;
            m['i'] -= x;
            m['x'] -= x;
        }

        // 7
        if(m['s'] > 0)
        {
            int x = m['s'];
            for(int i=0 ; i<x ; i++) res += '7';
            m['s'] -= x;
            m['e'] -= x;
            m['v'] -= x;
            m['e'] -= x;
            m['n'] -= x;
        }

        // 8
        if(m['g'] > 0)
        {
            int x = m['g'];
            for(int i=0 ; i<x ; i++) res += '8';
            m['e'] -= x;
            m['i'] -= x;
            m['g'] -= x;
            m['h'] -= x;
            m['t'] -= x;
        }

        // 1
        if(m['o'] > 0)
        {
            int x = m['o'];
            for(int i=0 ; i<x ; i++) res += '1';
            m['o'] -= x;
            m['n'] -= x;
            m['e'] -= x;
        }

        // 3
        if(m['t'] > 0)
        {
            int x = m['t'];
            for(int i=0 ; i<x ; i++) res += '3';
            m['t'] -= x;
            m['h'] -= x;
            m['r'] -= x;
            m['e'] -= x;
            m['e'] -= x;
        }

        // 9
        if(m['i'] > 0)
        {
            int x = m['i'];
            for(int i=0 ; i<x ; i++) res += '9';
            m['n'] -= x;
            m['i'] -= x;
            m['n'] -= x;
            m['e'] -= x;
        }

        sort(res.begin(), res.end());

        return res;
    }
};

C++

官方题解

class Solution {
public:
    string originalDigits(string s) {
        unordered_map<char, int> c;
        for (char ch: s) {
            ++c[ch];
        }

        vector<int> cnt(10);
        cnt[0] = c['z'];
        cnt[2] = c['w'];
        cnt[4] = c['u'];
        cnt[6] = c['x'];
        cnt[8] = c['g'];

        cnt[3] = c['h'] - cnt[8];
        cnt[5] = c['f'] - cnt[4];
        cnt[7] = c['s'] - cnt[6];

        cnt[1] = c['o'] - cnt[0] - cnt[2] - cnt[4];

        cnt[9] = c['i'] - cnt[5] - cnt[6] - cnt[8];

        string ans;
        for (int i = 0; i < 10; ++i) {
            for (int j = 0; j < cnt[i]; ++j) {
                ans += char(i + '0');
            }
        }
        return ans;
    }
};

Java

class Solution {
    public String originalDigits(String s) {
        Map<Character, Integer> c = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            c.put(ch, c.getOrDefault(ch, 0) + 1);
        }

        int[] cnt = new int[10];
        cnt[0] = c.getOrDefault('z', 0);
        cnt[2] = c.getOrDefault('w', 0);
        cnt[4] = c.getOrDefault('u', 0);
        cnt[6] = c.getOrDefault('x', 0);
        cnt[8] = c.getOrDefault('g', 0);

        cnt[3] = c.getOrDefault('h', 0) - cnt[8];
        cnt[5] = c.getOrDefault('f', 0) - cnt[4];
        cnt[7] = c.getOrDefault('s', 0) - cnt[6];

        cnt[1] = c.getOrDefault('o', 0) - cnt[0] - cnt[2] - cnt[4];

        cnt[9] = c.getOrDefault('i', 0) - cnt[5] - cnt[6] - cnt[8];

        StringBuffer ans = new StringBuffer();
        for (int i = 0; i < 10; ++i) {
            for (int j = 0; j < cnt[i]; ++j) {
                ans.append((char) (i + '0'));
            }
        }
        return ans.toString();
    }
} 

以上就是C++ Leetcode实现从英文中重建数字的详细内容,更多关于C++ Leetcode 英文中重建数字的资料请关注我们其它相关文章!

(0)

相关推荐

  • C++实现LeetCode(202.快乐数)

    [LeetCode] 202.Happy Number 快乐数 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits,

  • C++实现LeetCode(209.最短子数组之和)

    [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和 Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example:  Input: s = 7, num

  • C++实现LeetCode(211.添加和查找单词-数据结构设计)

    [LeetCode] 211.Add and Search Word - Data structure design 添加和查找单词-数据结构设计 Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string c

  • C++实现LeetCode数组练习题

    目录 1.存在重复元素 2.最大子序和 3.两数之和 4.合并两个有序数组 5.两个数组的交集II 6.买卖股票的最佳时机 7.杨辉三角 8.重塑矩阵 9.有效的数独 10.矩阵置零 总结 1.存在重复元素 排序数组,之后遍历是否有重复的元素 public boolean containsDuplicate(int[] nums) { Arrays.sort(nums); for(int i=1;i<nums.length;i++){ if(nums[i-1]==nums[i]){ return

  • C++ Leetcode实现从英文中重建数字

    目录 题目 分析 代码 题目 分析 首先我们先分析每个字母的组成,然后发现一些字符只在一个单词中出现,我们先去统计一下这些单词个数. z,w,u,x,g都只出现在一个数字中,也就是0,2,4,6,8,我们用哈希表统计一下s字符串中各个字符的数量,就可以知道0,2,4,6,8的数量,然后我们注意一下只在两个数字中出现的字符. h 只在 3,8 中出现.由于我们已经知道了 8 出现的次数,因此可以计算出 3 出现的次数. f 只在 4,5 中出现.由于我们已经知道了 4 出现的次数,因此可以计算出

  • Go Java算法之从英文中重建数字示例详解

    目录 从英文中重建数字 Java实现 Go实现 从英文中重建数字 给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9).按 升序 返回原始的数字. 示例 1: 输入:s = "owoztneoer" 输出:"012" 示例 2: 输入:s = "fviefuro" 输出:"45" 提示: 1 <= s.length <= 105 s[i] 为 ["e","g&

  • Java简单统计字符串中汉字,英文字母及数字数量的方法

    本文实例讲述了Java简单统计字符串中汉字,英文字母及数字数量的方法.分享给大家供大家参考,具体如下: package org.zhy.demo.algorithm; /** * 有一个字符串,其中包含中文字符.英文字符和数字字符,请统计和打印出各个字符的个数 * * @author Administrator * */ public class Str { public static void main(String[] args) { String str = "adasfAAADFD阿萨德

  • C++实现LeetCode(26.有序数组中去除重复项)

    [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

  • C++实现LeetCode(94.二叉树的中序遍历)

    [LeetCode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历 Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively

  • C++实现LeetCode(80.有序数组中去除重复项之二)

    [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二 Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you mus

  • js中Number数字数值运算后值不对的解决方法

    问题: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两个只有一位小数的数字相乘,怎么可能多出这么小数点出来. 我Google了一下,发现原来这是JavaScript浮点运算的一个bug. 比如:7*0.8 JavaScript算出来就是:5.6000000000000005 解决方法:网上找到了一些解决办法,就是重新写了一些浮点运算的函数. 下面就把这

  • java从字符串中提取数字的简单实例

    随便给你一个含有数字的字符串,比如: String s="eert343dfg56756dtry66fggg89dfgf"; 那我们如何把其中的数字提取出来呢?大致有以下几种方法,正则表达式,集合类,还有就是String类提供的方法. 1 String类提供的方法: package 测试练习; import Java.util.*; public class get_StringNum { /** *2016.10.25 */ public static void main(Strin

  • java基于正则提取字符串中的数字功能【如提取短信中的验证码】

    本文实例讲述了java基于正则提取字符串中的数字功能.分享给大家供大家参考,具体如下: 使用Java正则可以很方便的从字符串中提取符合条件的内容. 1.提取字符串中所有的手机号: private void getPhoneNum(String smsBody) { Pattern pattern = Pattern.compile("(13|14|15|18)\\d{9}"); Matcher matcher = pattern.matcher(smsBody); while (mat

  • php实现过滤字符串中的中文和数字实例

    本文实例讲述了php实现过滤字符串中的中文和数字.分享给大家供大家参考.具体实现方法如下: function getChinese($string,$encode="GBK") { switch($encode){ case "GBK" :$codelength=2;break; case "GB2312" :$codelength=3;break; case "UTF-8" :$codelength=3;break; cas

随机推荐