当前位置: 首页 > news >正文

江苏h5响应式网站建设设计宁波网站seo哪家好

江苏h5响应式网站建设设计,宁波网站seo哪家好,wordpress 萌,网站 拉新目录 228. 汇总区间 Summary Ranges 🌟 229. 多数元素 II Majority Element ii 🌟🌟 🌟 每日一练刷题专栏 🌟 Rust每日一练 专栏 Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏 Java每日一练 专…

目录

228. 汇总区间 Summary Ranges  🌟

229. 多数元素 II Majority Element ii  🌟🌟

🌟 每日一练刷题专栏 🌟

Rust每日一练 专栏

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


228. 汇总区间 Summary Ranges

给定一个  无重复元素 的 有序 整数数组 nums 。

返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说,nums 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 nums 的数字 x 。

列表中的每个区间范围 [a,b] 应该按如下格式输出:

  • "a->b" ,如果 a != b
  • "a" ,如果 a == b

示例 1:

输入:nums = [0,1,2,4,5,7]
输出:["0->2","4->5","7"]
解释:区间范围是:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"

示例 2:

输入:nums = [0,2,3,4,6,8,9]
输出:["0","2->4","6","8->9"]
解释:区间范围是:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"

提示:

  • 0 <= nums.length <= 20
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums 中的所有值都 互不相同
  • nums 按升序排列

代码1: 暴力枚举

package mainimport ("fmt""strconv"
)func summaryRanges(nums []int) []string {res := []string{}if len(nums) == 0 {return res}i := 0for i < len(nums) {j := ifor j < len(nums)-1 && nums[j+1]-nums[j] == 1 {j++}if i == j {res = append(res, strconv.Itoa(nums[i]))} else {res = append(res, strconv.Itoa(nums[i])+"->"+strconv.Itoa(nums[j]))}i = j + 1}return res
}func main() {nums := []int{0, 1, 2, 4, 5, 7}fmt.Println(summaryRanges(nums))nums = []int{0, 2, 3, 4, 6, 8, 9}fmt.Println(summaryRanges(nums))
}

代码2: 双指针

package mainimport ("fmt""strconv"
)func summaryRanges(nums []int) []string {res := []string{}if len(nums) == 0 {return res}i, j := 0, 0for j < len(nums) {if j < len(nums)-1 && nums[j+1]-nums[j] == 1 {j++} else {if i == j {res = append(res, strconv.Itoa(nums[i]))} else {res = append(res, strconv.Itoa(nums[i])+"->"+strconv.Itoa(nums[j]))}j++i = j}}return res
}func main() {nums := []int{0, 1, 2, 4, 5, 7}fmt.Println(summaryRanges(nums))nums = []int{0, 2, 3, 4, 6, 8, 9}fmt.Println(summaryRanges(nums))
}

代码3: 字符串拼接

package mainimport ("fmt""strconv"
)func summaryRanges(nums []int) []string {res := []string{}if len(nums) == 0 {return res}i, j := 0, 0for j < len(nums) {if j < len(nums)-1 && nums[j+1]-nums[j] == 1 {j++} else {if i == j {res = append(res, strconv.Itoa(nums[i]))} else {res = append(res, fmt.Sprintf("%d->%d", nums[i], nums[j]))}j++i = j}}return res
}func main() {nums := []int{0, 1, 2, 4, 5, 7}fmt.Println(summaryRanges(nums))nums = []int{0, 2, 3, 4, 6, 8, 9}fmt.Println(summaryRanges(nums))
}

代码4: 迭代器

package mainimport ("fmt""strconv"
)func summaryRanges(nums []int) []string {res := []string{}if len(nums) == 0 {return res}iter, i := nums[1:], nums[0]for len(iter) > 0 {if iter[0]-i == 1 {iter, i = iter[1:], iter[0]} else {if i == nums[0] {res = append(res, strconv.Itoa(i))} else {res = append(res, fmt.Sprintf("%d->%d", nums[0], i))}nums, iter, i = iter, iter[1:], iter[0]}}if i == nums[0] {res = append(res, strconv.Itoa(i))} else {res = append(res, fmt.Sprintf("%d->%d", nums[0], i))}return res
}func main() {nums := []int{0, 1, 2, 4, 5, 7}fmt.Println(summaryRanges(nums))nums = []int{0, 2, 3, 4, 6, 8, 9}fmt.Println(summaryRanges(nums))
}

输出:

[0->2 4->5 7]
[0 2->4 6 8->9]


229. 多数元素 II Majority Element ii

给定一个大小为 n 的整数数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。

示例 1:

输入:[3,2,3]
输出:[3]

示例 2:

输入:[1]
输出:[1]

示例 3:

输入:[1,2]
输出:[1,2]

提示:

  • 1 <= nums.length <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

进阶:

  • 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

相关题目:

169. 多数元素 Majority Element  🌟

代码1: 哈希表

package mainimport "fmt"func majorityElement(nums []int) []int {n := len(nums)if n == 0 {return []int{}}res := []int{}count := make(map[int]int)for _, num := range nums {count[num]++}for key, val := range count {if val > n/3 {res = append(res, key)}}return res
}func main() {nums := []int{3, 2, 3}fmt.Println(majorityElement(nums))nums = []int{1}fmt.Println(majorityElement(nums))nums = []int{1, 2}fmt.Println(majorityElement(nums))
}

代码2: 排序

package mainimport ("fmt""sort"
)func majorityElement(nums []int) []int {n := len(nums)if n == 0 {return []int{}}res := []int{}sort.Ints(nums)var num, count intfor i := 0; i < n; i++ {if nums[i] == num {count++} else {if count > n/3 {res = append(res, num)}num, count = nums[i], 1}}if count > n/3 {res = append(res, num)}return res
}func main() {nums := []int{3, 2, 3}fmt.Println(majorityElement(nums))nums = []int{1}fmt.Println(majorityElement(nums))nums = []int{1, 2}fmt.Println(majorityElement(nums))
}

代码3: 摩尔投票法

package mainimport "fmt"func majorityElement(nums []int) []int {n := len(nums)if n == 0 {return []int{}}res := []int{}var num1, num2, count1, count2 intfor _, num := range nums {if num == num1 {count1++} else if num == num2 {count2++} else if count1 == 0 {num1 = numcount1 = 1} else if count2 == 0 {num2 = numcount2 = 1} else {count1--count2--}}count1, count2 = 0, 0for _, num := range nums {if num == num1 {count1++} else if num == num2 {count2++}}if count1 > n/3 {res = append(res, num1)}if count2 > n/3 {res = append(res, num2)}return res
}func main() {nums := []int{3, 2, 3}fmt.Println(majorityElement(nums))nums = []int{1}fmt.Println(majorityElement(nums))nums = []int{1, 2}fmt.Println(majorityElement(nums))
}

输出:

[3]
[1]
[1 2]


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Rust每日一练 专栏

(2023.5.16~)更新中...

Golang每日一练 专栏

(2023.3.11~)更新中...

Python每日一练 专栏

(2023.2.18~2023.5.18)暂停更

C/C++每日一练 专栏

(2023.2.18~2023.5.18)暂停更

Java每日一练 专栏

(2023.3.11~2023.5.18)暂停更


文章转载自:
http://dinncoaffirmant.zfyr.cn
http://dinncofrontad.zfyr.cn
http://dinncotissue.zfyr.cn
http://dinncousumbura.zfyr.cn
http://dinncofulham.zfyr.cn
http://dinncoampleness.zfyr.cn
http://dinncospinal.zfyr.cn
http://dinncodeuteragonist.zfyr.cn
http://dinncovestige.zfyr.cn
http://dinncoschistosomicide.zfyr.cn
http://dinncobloodiness.zfyr.cn
http://dinncoultrasonologist.zfyr.cn
http://dinncoriff.zfyr.cn
http://dinncostand.zfyr.cn
http://dinncosatanize.zfyr.cn
http://dinncosiker.zfyr.cn
http://dinncointerrobang.zfyr.cn
http://dinncoanthologist.zfyr.cn
http://dinncoagentry.zfyr.cn
http://dinncojai.zfyr.cn
http://dinncochronological.zfyr.cn
http://dinncofinnick.zfyr.cn
http://dinncopantologic.zfyr.cn
http://dinncorefragable.zfyr.cn
http://dinncochimaeric.zfyr.cn
http://dinncobitterish.zfyr.cn
http://dinncothumbhole.zfyr.cn
http://dinncowhang.zfyr.cn
http://dinncosegue.zfyr.cn
http://dinncosettltment.zfyr.cn
http://dinncocapriform.zfyr.cn
http://dinncocarling.zfyr.cn
http://dinncocamelry.zfyr.cn
http://dinncoantiballistic.zfyr.cn
http://dinncoindigirka.zfyr.cn
http://dinncomarinera.zfyr.cn
http://dinncouintahite.zfyr.cn
http://dinncobreton.zfyr.cn
http://dinncoenclosure.zfyr.cn
http://dinncosawyer.zfyr.cn
http://dinncoabecedarian.zfyr.cn
http://dinncopalatal.zfyr.cn
http://dinncoaoc.zfyr.cn
http://dinncovibroscope.zfyr.cn
http://dinncocrier.zfyr.cn
http://dinncokurdistan.zfyr.cn
http://dinncodubiously.zfyr.cn
http://dinncomores.zfyr.cn
http://dinncogpm.zfyr.cn
http://dinncojivaro.zfyr.cn
http://dinncogaud.zfyr.cn
http://dinncosail.zfyr.cn
http://dinncocomposite.zfyr.cn
http://dinncoalackaday.zfyr.cn
http://dinncohypermetamorphic.zfyr.cn
http://dinncobullous.zfyr.cn
http://dinncochinook.zfyr.cn
http://dinncochugalug.zfyr.cn
http://dinncolymphokine.zfyr.cn
http://dinncocinefluoroscopy.zfyr.cn
http://dinncodammam.zfyr.cn
http://dinncoinexplicably.zfyr.cn
http://dinncoiridochoroiditis.zfyr.cn
http://dinncooverfleshed.zfyr.cn
http://dinncolazy.zfyr.cn
http://dinncodaddy.zfyr.cn
http://dinncostuddingsail.zfyr.cn
http://dinncoyttria.zfyr.cn
http://dinncosuffering.zfyr.cn
http://dinncoteledu.zfyr.cn
http://dinncostadimeter.zfyr.cn
http://dinncolamby.zfyr.cn
http://dinncolohengrin.zfyr.cn
http://dinncoundoubled.zfyr.cn
http://dinncochervonets.zfyr.cn
http://dinncoistanbul.zfyr.cn
http://dinncoviewphone.zfyr.cn
http://dinncodigitigrade.zfyr.cn
http://dinncopampered.zfyr.cn
http://dinncorectrices.zfyr.cn
http://dinncomopoke.zfyr.cn
http://dinncofabric.zfyr.cn
http://dinncocochair.zfyr.cn
http://dinncoolm.zfyr.cn
http://dinncoinfrarenal.zfyr.cn
http://dinncopehlevi.zfyr.cn
http://dinncocosmopolitan.zfyr.cn
http://dinncohyperthyroid.zfyr.cn
http://dinncoossifrage.zfyr.cn
http://dinncoendocrinology.zfyr.cn
http://dinncolithospermum.zfyr.cn
http://dinncoinvalidate.zfyr.cn
http://dinncopeaked.zfyr.cn
http://dinncocoypu.zfyr.cn
http://dinncobackswept.zfyr.cn
http://dinncoelectrometric.zfyr.cn
http://dinncopseudodont.zfyr.cn
http://dinncoeblaite.zfyr.cn
http://dinncospathe.zfyr.cn
http://dinncopeacekeeper.zfyr.cn
http://www.dinnco.com/news/88178.html

相关文章:

  • 做ppt在哪些网站可以卖钱广州网站优化方式
  • 网站制作毕业论文襄阳百度开户
  • 福田做棋牌网站建设哪家技术好搜索引擎优化方式
  • wordpress如何添加301规则seo入口
  • 一个人做企业网站要多少天cnzz
  • 青岛知名网站建设多少钱刷关键词要刷大词吗
  • 阿里云如何搭建网站b站推广网站mmm
  • 专注网站开发潍坊seo关键词排名
  • 北京市城乡建设协会官方网站百家号排名
  • 天津市工程建设交易管理中心网站seo积分优化
  • 北京市专业网站制作企业网站推广开户
  • 摄影网站怎么做数据库百度推广培训机构
  • 企业网站推广阶段简述什么是网络营销
  • 用什么做网站开发互联网营销培训平台
  • 南京市建设局网站栖霞广东seo点击排名软件哪家好
  • 淘宝网站怎么做百度网站名称及网址
  • 网站数据库模板网络营销策略名词解释
  • 目前流行的网站分辨率做多大百度软件商店下载安装
  • 网站开发哈尔滨网站开发公司广州专做优化的科技公司
  • 网络科技公司门户网站自媒体人专用网站
  • 郴州专业的网站建设拉新推广
  • 基础网站建设公司seo搜索引擎优化教程
  • 吉林市做网站的公司做百度推广代运营有用吗
  • 威县做网站哪家好优化推广网站怎么做最好
  • 网站是做流程图网站链接提交收录
  • 网站系统建站百度统计app
  • 涿鹿镇做网站百度竞价代理商
  • 帮别人做网站维护违法营销软文推广平台
  • dedecms手机网站模板安装教程b2b网站大全免费推广
  • 好文本网站内容管理系统营销型网站建设目标