当前位置:网站首页>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;
边栏推荐
- typescript33 - high-level overview of typescript
- oracle query scan full table and walk index
- LeetCode brushing diary: 53, the largest sub-array and
- Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批
- typescript32-ts中的typeof
- Force buckle, 752-open turntable lock
- typescript37-class的构造函数实例方法继承(extends)
- AntPathMatcher uses
- hash table
- LeetCode刷题日记:LCP 03.机器人大冒险
猜你喜欢

Huawei's 5-year female test engineer resigns: what a painful realization...

LeetCode Brushing Diary: 74. Searching 2D Matrix

【Brush the title】Family robbery

typescript31-any类型

"NetEase Internship" Weekly Diary (3)

飞桨助力航天宏图PIE-Engine地球科学引擎构建

A good book for newcomers to the workplace

2022河南青训联赛第(三)场
![[LeetCode Daily Question]——654. The largest binary tree](/img/05/0af1c6dc0085e253c0758c8da63e52.png)
[LeetCode Daily Question]——654. The largest binary tree

LeetCode刷题日记:74. 搜索二维矩阵
随机推荐
YGG Guild Development Plan Season 1 Summary
密码学的基础:X.690和对应的BER CER DER编码
CodeTon Round 2 D. Magical Array
垃圾回收器CMS和G1
Win Go development kit installation configuration, GoLand configuration
3.Bean的作用域与生命周期
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
Hash collisions and consistent hashing
libcurl访问url保存为文件的简单示例
使用百度EasyDL实现厂区工人抽烟行为识别
哈希冲突和一致性哈希
【LeetCode每日一题】——654.最大二叉树
Force buckle, 752-open turntable lock
AOF重写
[LeetCode Daily Question]——654. The largest binary tree
LeetCode Brushing Diary: 74. Searching 2D Matrix
C language inserted into the characters of simple exercises
优炫数据库导库导错了能恢复吗?
2022河南青训联赛第(三)场