当前位置:网站首页>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;
边栏推荐
- typescript30 - any type
- [ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
- 【LeetCode每日一题】——704.二分查找
- "NetEase Internship" Weekly Diary (3)
- 【 wheeled odometer 】
- 2022-07-30 mysql8执行慢SQL-Q17分析
- [ORB_SLAM2] SetPose, UpdatePoseMatrices
- Simple example of libcurl accessing url saved as file
- LeetCode brushing diary: 33. Search and rotate sorted array
- 用位运算为你的程序加速
猜你喜欢

【轮式里程计】
软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3

Personal blog system project test

3.Bean的作用域与生命周期

Fly propeller power space future PIE - Engine Engine build earth science

Constructor of typescript35-class

Chengdu openGauss user group recruit!

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

oracle query scan full table and walk index

2022-08-01 mysql/stoonedb慢SQL-Q18分析
随机推荐
密码学的基础:X.690和对应的BER CER DER编码
Redis 持久化 - RDB 与 AOF
『网易实习』周记(二)
typescript29-枚举类型的特点和原理
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
oracle查询扫描全表和走索引
Redis 订阅与 Redis Stream
2022-08-01 反思
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
6-24 exploit-vnc password cracking
Use baidu EasyDL implement factory workers smoking behavior recognition
HSDC和独立生成树相关
Constructor instance method inheritance of typescript37-class (extends)
Hiring a WordPress Developer: 4 Practical Ways
Day116. Shangyitong: Details of appointment registration ※
Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批
项目后台技术Express
Chengdu openGauss user group recruit!