当前位置:网站首页>The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
2022-08-02 02:09:00 【MOAN.】
Here is the little M who is preparing for the autumn move
Today I regained my confidence and challenged a simple algorithm problem (from Niu Ke, the link has been posted).You can think about it first
Counting the most letters in a string__Nioke.com(nowcoder.com)
After half an hour of coding, I found out why it is ok in vsCode, but it keeps getting an error no matter how I enter it!!!!Slowly fell into contemplation and began to get crazy !!!
Finally I fell asleep after constant thinking
The next day, I woke up struggling, turned on the computer, skillfully clicked on Niu Ke, and checked the analysis with one click
Unable to pass a single test case
With my unremitting efforts, I discovered the coding "secret" of online programming problems
Why my code in vscode is correct, but an error is reported when I enter the online editor
Premise: The code logic is correct
First of all, don't enter it in the form of a function, just write the logic code directly
Then how do I write test parameters
This is the most important secret I found:readline();
//Get the input string ``
In this way, the test data of the online editor can be directly obtained
like this
let str = readline();//Get the input string
If it is a fixed test data, you can only enter a fixed answer, so the test case cannot be passedlet str = 'aaccc' one of the reasons I'm getting an error is doing this
Secondly, remember to enter the answer
Solution ideas
Let's look at the topic again, that is, enter aab=> a , aaccc=>c , aabbcccdddddd=> d
We all know the truth. For me, the little white, the first thing that comes to mind is the subscript counting method
But obviously it won't work
Ideas:
1. Process the string, convert the string into an array to traverse each character (split() splits the string into an array)
2. Create a new map object to store each character key and the number of occurrences value (equivalent to a counter)
3. For loop traversal (for...in is also possible), and determine whether there is a current character in the key of the map, if it exists, value+1; if it does not exist, set one, and set the value to 0
4. Get the maximum value in the value of the map (through the loop, get the key while finding the maximum value)
In this place, I made a mistake in my thinking at first. I want to use Math.max() to directly get the maximum value of values, and it is in one step, but it seems more troublesome for me to get the corresponding key.Math.max(...map.values())
5. Output
Code
let str = readline();//Get the input stringconst map=new Map()//Counterlet arr = str.split('') //split string into arrayfor(let i = 0 ; i < arr.length;i++){let val =map.get(arr[i])if(val){map.set(arr[i],val+1)}else{map.set(arr[i],1)}}let max=0; //Maximum valuelet keymap.forEach((v,k) => {if(max
In fact, the code is written like this, but pay attention to some features of the online editor
let str = 'aabbbcccccc'function countCase(str){//counterlet map=new Map()let str2 = str.split('')for(let i = 0 ; i < str2.length;i++){let val =map.get(str2[i])if(val){map.set(str[i],val+1)}else{map.set(str[i],1)}}let max=0;let keymap.forEach((v,k) => {if(max
When will Xiao Xiao Xiao Bai become an algorithm Xiao Bai?
If there is something wrong, welcome guidance
Reprint: Welcome to reprint, but this statement must be retained without the author's consent;
边栏推荐
- typescript33-typescript高级概述
- [ORB_SLAM2] SetPose, UpdatePoseMatrices
- typescript33 - high-level overview of typescript
- LeetCode刷题日记: 33、搜索旋转排序数组
- 飞桨助力航天宏图PIE-Engine地球科学引擎构建
- The underlying data structure of Redis
- Handwriting a blogging platform ~ the first day
- Golang分布式应用之Redis
- AntPathMatcher使用
- 软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
猜你喜欢
Constructor of typescript35-class
A good book for newcomers to the workplace
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
Chengdu openGauss user group recruit!
Day116. Shangyitong: Details of appointment registration ※
typescript37-class的构造函数实例方法继承(extends)
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
Analysis of volatile principle
The characteristics and principle of typescript29 - enumeration type
AOF重写
随机推荐
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
volatile原理解析
2022-08-01 反思
¶Backtop 回到顶部 不生效
typescript36-class的构造函数实例方法
The underlying data structure of Redis
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
The characteristics and principle of typescript29 - enumeration type
Typescript31 - any type
Golang分布式应用之Redis
用位运算为你的程序加速
Named parameter implementation of JDBC PreparedStatement
MySQL optimization strategy
乱七八糟的网站
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
检查IP或端口是否被封
软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
手写博客平台~第二天
CodeTon Round 2 D. Magical Array