赞
踩
Hello,各位小伙伴,大家好呀,放国庆节假了,老哥在这里首先祝福祖国,繁荣昌盛,然后祝各位小伙伴们假期玩的愉快,吃的开心;然后的话,在家也不能忘记学习哦,每天进步一点点,老哥玩了一天,回到家,然后最近在刷算法题,那么就浅浅记录一下吧
话不多说,让我们开始吧
1、刷题网站,https://www.hackerrank.com/
2、然后这个网站的题目是全英文的,有英文阅读困难的,可以用下某度翻译
3、我们来看到这个题目
There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.
老哥来解释一下,一个1加上另外一个1是一对袜子,一个2加上另外一个2是一对袜子
剩下3个不同颜色的袜子
解题思路: 我们需要得到每个颜色袜子的总数,然后把每个袜子的颜色 /2 ,会得到每个袜子的对数,然后相加,得到所有每个袜子成对的数量,最后返回总的袜子总数
package main
import "fmt"
func main() {
var arr = []int32{1, 2, 1, 2, 1, 3, 2}
sockPair := sockMerchant(7, arr)
fmt.Println(sockPair)
}
func sockMerchant(n int32, ar []int32) int32 {
// Write your code here
var count = make(map[int32]int32)
for _, value := range ar {
count[value]++
}
var result int32 = 0
for _, match := range count {
result += match / 2
}
return result
}
3A、第二题
The hiker first enters a valley 2 units deep. Then they climb out and up onto a mountain 2 units high. Finally, the hiker returns to sea level and ends the hike.
老哥一开始看到这个也是有点懵,不知道它想表达什么,后面大概理解了,就是一个徒步者,从地平面往山谷走两步下去,记录为2个D(down的简称),然后从山谷走上地平面,记录两个U
(up的简称)
解题思路:从地平面下去,经过山谷,爬上地平面,记1次
package main
import (
"fmt"
"strings"
)
func main() {
var steps int32 = 8
var path = "UDDDUDUU"
valleys := countingValleys(steps, path)
fmt.Println(valleys)
}
// hacker Rank不要在里面写中文注释!!!
func countingValleys(steps int32, path string) int32 {
// Write your code here
var sealevel = 0
var vally int32 = 0
if int(steps) != len(path) {
return 0
}
paths := strings.Split(path, "")
for i := 0; i < int(steps); i++ {
if strings.EqualFold(paths[i], "U") {
//sealevel ++ Same effect
sealevel = sealevel + 1
} else {
//sealevel -- Same effect
sealevel = sealevel - 1
}
if paths[i] == "U" && sealevel == 0 {
vally = vally + 1
}
}
return vally
}
5、第三题
There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2 .
这个题目,老哥一开始没看懂,后面大概理解了,就是每一次跳,如果可以跳一步,就跳一步,如果可以跳两步,就跳两步,不要踩到雷区就行
解题思路: 我们先拿到走一步的总长,和走两步的总长,减去1,就可以得到跳的步数,然后比较两种方案,谁走的步数少,返回结果
package main
import "fmt"
func main() {
var arr = []int32{0, 0, 0, 1, 0, 0}
min := jumpingOnClouds(arr)
fmt.Println(min)
}
func jumpingOnClouds(c []int32) int32 {
// Write your code here
var jumpByOne int32 = 0
var jumpByTwo int32 = 0
for i := 0; i < len(c); i++ {
if c[i] == 0 {
jumpByOne++
}
}
for i := 0; i < len(c); i += 2 {
if c[i] != 0 {
i -= 1
if c[i] == 0 {
jumpByTwo++
}
} else {
if i+2 == len(c) {
i -= 1
}
jumpByTwo++
}
}
//jumpByOne jumpByTwo is total length,so we just need steps of jump
return min(jumpByOne-1, jumpByTwo-1)
}
func min(a, b int32) int32 {
if a > b {
return b
}
return a
}
6、第四题
There is a string s , of lowercase English letters that is repeated infinitely many times. Given an integer n, , find and print the number of letter a's in the first n letters of the infinite string.
这个题比较简单,把字符串第一个字符出现的次数找出来,10代表这个数,要重复拼接的长度为10,也就是 abaabaabaa
解题思路:利用求余和取模的思想,把一个字符串出现的首个字符的次数求出来,然后乘以复制的,再加上尾部的字符,返回总个数即可
package main
import (
"fmt"
)
func main() {
var str = "aba"
var n int64 = 10
repeated := repeatedString(str, n)
fmt.Println(repeated)
}
func repeatedString(s string, n int64) int64 {
// Write your code here
var copyMul = n / int64(len(s))
var copyMod = n % int64(len(s))
var count int64 = 0
var modcount int64 = 0
for i := 0; i < len(s); i++ {
if string(s[i]) == "a" {
count++
}
}
for i := 0; i < int(copyMod); i++ {
if string(s[i]) == "a" {
modcount++
}
}
return count*copyMul + modcount
}
7、最后,各位小伙伴,麻烦给老哥一个点赞、关注、收藏三连好吗,你的支持是老哥更新最大的动力,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。