惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
D
DataBreaches.Net
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
S
Secure Thoughts
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
腾讯CDC
GbyAI
GbyAI
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI

LeetCode

二分查找 mid 什么时候要加 1 - V2EX 大家知道哔站或者 Youtube 上有木有出 leetcode 刷题讲解视频的博主? - V2EX leetcode 到达 1000 题了🎉 - V2EX 为什么打开 leetcode 中文网要验证手机号啊,能不验证吗 - V2EX 为什么要刷算法? - V2EX 靓仔们,刷的第一个道 leetcode 题是什么哇 - V2EX Leecode 组队刷题 - V2EX 2018 年之前大家怎么准备的面试? - V2EX 142. 环形链表 II 我这么写为什么有个 case 无法通过 - V2EX 各位都是怎么刷题的 - V2EX leetcode 第 402 题,移除 K 位数字 的疑问 - V2EX 你力扣做几题了?做题数还能衡量水平吗?我做了很多题还是很菜 - V2EX leetcode-cn 年费会员 20%off - V2EX Leetcode 刷题记录 - V2EX LeetCode 有史以来最简单的题目 - V2EX 一直很好奇 leetcode 是怎么判断随机算法的正误的 - V2EX 你们会在意执行用时和内存消耗吗? - V2EX leetcode 502badgateway 了? - V2EX 开心,参加 Weekly Contest 270 不到 1 小时解决了 3 题, Q3 最后几十秒提交一次性通过 - V2EX 刷题,组队 - V2EX 早安刷题 - V2EX leetcode 33 解法求解释 - V2EX 大家刷题,是去中文的 leetcode-cn.com,还是去 leetcode.com?貌似还有一个山寨的 lintcode - V2EX 刷同一类题型真是很不错的方法 - V2EX 3 个月 了 刷了 51 道题 平均每道题 3 种左右的方法 区间合并 断断续续想了 2 天 执行时间竟然 100% 两次提交 代码没有变动 竟然时间还不一样 一起来刷题 - V2EX 30 天 LeetCode 从 100 题刷到 200 题,再次发帖留念 - V2EX 被今天的 LeetCode 打卡题搞吐了,思路和代码都和题解基本一样,但就是一直超时不过 - V2EX LeetCode 中使用 StringBuilder 连接字符串为什么会比用+号连接快? 刷题很痛苦 需要自己找方法刺激过程 别放弃 - V2EX leetcode 有排名了,每天起来看自己排名上涨也是一种乐趣 认认真真地刷题 提高代码能力和思维能力 - V2EX 刷了 10 题 leetcode,感觉身体被掏空 - V2EX 大家觉得 backtracking 难吗? - V2EX 国内本科学生,想问问大佬们 Leetcode 推荐美国站还是国内站 - V2EX 华为 OD 机试题:给定两个数 0<S, N<100000, S 为数列总和, N 为数列长度,求连续正整数列 LeetCode 2020 感恩节促销开始 - V2EX 求分享 416 的衍生问题的解题思路 LeetCode 有关 树 的题到底怎么套输入 - V2EX 有没有正在刷 LeetCode 的小伙伴们? - V2EX 九章算法 | 字节跳动面试题:用 Rand7()实现 Rand10() - V2EX 长文慎点! Kaggle 比赛的 top2%和金牌到底差距有多大? - V2EX 微软面试真题+面试官改编 leetcode 思路(哈希篇-加更) - V2EX 微软面试真题+面试官改编 leetcode 思路(链表篇) - V2EX
leetcode 1758 - V2EX
yujianwjj · 2021-04-08 · via LeetCode

https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/

题目比较简单,只有 010101... 或者 101010... 两个方案 最开始我的解法是:

func minOperations(s string) int {
    count1 := 0
    // 010101...
    for i := 0; i < len(s); i++ {
        if int(s[i] - '0') != i % 2 {
            count1++
        }
    }
    count2 := 0
    // 10101010...
    for i := 0; i < len(s); i++ {
        if int(s[i] - '0') != (i+1) % 2 {
            count2++
        }
    }
    return min(count1, count2)
}

后来发现更简单一点的

func minOperations(s string) int {
    count1 := 0
    // 010101...
    for i := 0; i < len(s); i++ {
        if int(s[i] - '0') != i % 2 {
            count1++
        }
    }
    
    return min(count1, len(s)-count1)
}

我的疑问是如何用数学证明这两种方案加起来刚好是 s 的长度。