当前位置:网站首页>Huawei machine test question: longest continuous subsequence
Huawei machine test question: longest continuous subsequence
2022-07-05 01:33:00 【JOEL-T99】
Scala Realization
Title Description :
- Yes N A sequence of positive integers , Given an integer sum
- Find the longest continuous subsequence so that their sum equals sum
- Returns the length of the secondary subsequence , If there is no sequence that meets the requirements return -1
remarks :
- The input sequence consists only of numbers and English commas , Numbers are separated by English commas
- Sequence length 1<=N<=200, The input sequence does not consider exceptions
- The title ensures that the input sequence meets the requirements
Example
Input :
- 1,2,3,4,2
- 6
Output :
- 3
analysis :
- 1,2,3 and 4,2 Both sequences can meet the requirements , So the longest continuous sequence is 1,2,3 So the result is 3
Input :
- 1,2,3,4,2
- 20
Output :
- -1
explain :
- There are no subsequences that meet the requirements , return -1
Code :
def main(args: Array[String]): Unit = {
// Get input
val elems = StdIn.readLine().split(",").map(_.toInt).toList
val sum = StdIn.readLine().toInt
// Declare variables
var tmp = 0
var maxLen = -1
// Calculation maxLen
for (i <- elems.indices) {
tmp += elems(i)
// Continuously increase the following number , If it is greater than sum Is out of , If it is equal to sum Reset maxLen
breakable {
for (j <- i + 1 until elems.length) {
tmp += elems(j)
// Out of the loop
if (tmp > sum) break
// Reset maxLen
if (tmp == sum) {
maxLen = Math.max(maxLen, j - i + 2)
break
}
// If you can't find it, set it to -1
maxLen = -1
}
}
// Reset tmp
tmp = 0
}
// Output results
println(maxLen)
}
边栏推荐
- Redis(1)之Redis简介
- Include rake tasks in Gems - including rake tasks in gems
- Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
- Database postragesq PAM authentication
- Five ways to query MySQL field comments!
- Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
- Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
- If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
- 视频网站手绘
- 【LeetCode】88. Merge two ordered arrays
猜你喜欢
![[swagger]-swagger learning](/img/60/1dbe074b3c66687867192b0817b553.jpg)
[swagger]-swagger learning

Call Huawei order service to verify the purchase token interface and return connection reset

Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!

Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income

Actual combat simulation │ JWT login authentication

增量备份 ?db full

To sort out messy header files, I use include what you use

JS implementation determines whether the point is within the polygon range

Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization

实战模拟│JWT 登录认证
随机推荐
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[OpenGL learning notes 8] texture
Postman automatically fills headers
phpstrom设置函数注释说明
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
Change the background color of a pop-up dialog
19. Delete the penultimate node of the linked list
微信小程序:全新独立后台月老办事处一元交友盲盒
Hand drawn video website
Redis(1)之Redis简介
【CTF】AWDP总结(Web)
Check if this is null - checking if this is null
Main window in QT application
Kibana installation and configuration
"2022" is a must know web security interview question for job hopping
How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery