当前位置:网站首页>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)
![[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the imageDirect upload (img-dO9qTJcx-1658891506240)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b5f13cb4d0724f008cc21b0c4f505e40~tplv-k3u1fbpfcp-watermark.image?)]](/img/94/cdb89cebec3f5d7d35c44b0e8bb51a.png)
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 !!!
![[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the imageDirect upload (img-IpBQA7LH-1658891506243)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3372d9277ba746a1b01e4dba2b07fca3~tplv-k3u1fbpfcp-watermark.image?)]](/img/6b/02180259e0abbd0f50ad1d96092e45.png)
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
![[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the imageDirect upload (img-VhJml2cM-1658891506244)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8d3172e1a2894c0786d37f8b1c60c9c4~tplv-k3u1fbpfcp-watermark.image?)]](/img/3b/14f7a6d5b4fd6a7c1a82784aa0b46e.png)
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 thisSecondly, remember to enter the answer
Solution ideas
![[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the imageDirect upload (img-sZxBO6MV-1658891506245)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0c3b71ca41ee43d29c15fa022e36d71b~tplv-k3u1fbpfcp-watermark.image?)]](/img/2f/c59b87882b9c3c0a10e98c260c95a4.png)
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(maxIn 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(maxWhen 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;
边栏推荐
- 密码学的基础:X.690和对应的BER CER DER编码
- A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
- 2022-08-01 反思
- After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
- 【LeetCode每日一题】——654.最大二叉树
- MySQL8 下载、启动、配置、验证
- oracle查询扫描全表和走索引
- Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
- Day116.尚医通:预约挂号详情 ※
- Chengdu openGauss user group recruit!
猜你喜欢

使用百度EasyDL实现厂区工人抽烟行为识别

Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem

垃圾回收器CMS和G1

3. Bean scope and life cycle

云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣

Constructor of typescript35-class

"NetEase Internship" Weekly Diary (3)

密码学的基础:X.690和对应的BER CER DER编码

"NetEase Internship" Weekly Diary (1)

【轮式里程计】
随机推荐
CodeTon Round 2 D. Magical Array 规律
Force buckle, 752-open turntable lock
PHP uses PHPRedis and Predis
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
秒懂大模型 | 3步搞定AI写摘要
求大神解答,这种 sql 应该怎么写?
力扣、752-打开转盘锁
优炫数据库导库导错了能恢复吗?
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
AntPathMatcher uses
HSDC is related to Independent Spanning Tree
LeetCode刷题日记:53、最大子数组和
typeof in typescript32-ts
Garbage Collector CMS and G1
oracle query scan full table and walk index
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
手写一个博客平台~第一天
【LeetCode Daily Question】——704. Binary Search