当前位置:网站首页>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)
}
边栏推荐
- Change the background color of a pop-up dialog
- Database postragesql client authentication
- Introduction to the gtid mode of MySQL master-slave replication
- Do you know the eight signs of a team becoming agile?
- Basic operations of database and table ----- delete index
- Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Discrete mathematics: reasoning rules
- 【CTF】AWDP总结(Web)
- One plus six brushes into Kali nethunter
- Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
猜你喜欢
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
What sparks can applet container technology collide with IOT
How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
Take you ten days to easily complete the go micro service series (IX. link tracking)
Express routing, express middleware, using express write interface
[pure tone hearing test] pure tone hearing test system based on MATLAB
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)
Yyds dry inventory swagger positioning problem ⽅ formula
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
随机推荐
Include rake tasks in Gems - including rake tasks in gems
MySQL REGEXP:正则表达式查询
Heartless sword English translation of Xi Murong's youth without complaint
phpstrom设置函数注释说明
【微处理器】基于FPGA的微处理器VHDL开发
Do you know the eight signs of a team becoming agile?
微信小程序:独立后台带分销功能月老办事处交友盲盒
Call Huawei order service to verify the purchase token interface and return connection reset
Check if this is null - checking if this is null
Research Report on the overall scale, major producers, major regions, products and application segmentation of agricultural automatic steering system in the global market in 2022
实战模拟│JWT 登录认证
Change the background color of a pop-up dialog
Database postragesq peer authentication
[microprocessor] VHDL development of microprocessor based on FPGA
es使用collapseBuilder去重和只返回某个字段
【纯音听力测试】基于MATLAB的纯音听力测试系统
19. Delete the penultimate node of the linked list
【LeetCode】88. Merge two ordered arrays
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
增量备份 ?db full