【好】strong-password-checker,我自己做出来的:)
我自己做出来的,分了几种情况来考虑。(再后面有加了注释的版本)
https://leetcode.com/problems/strong-password-checker/
// 加油!
public class Solution {
public int strongPasswordChecker(String s) {
int sLen = s.length();
if (sLen < 4) {
return 6 - sLen;
}
int lnum = 1;
int unum = 1;
int dnum = 1;
int rcount = 0;
int ricount = 0;
int rdcount = 0;
int sameseq = 0;
for (int i=0; i<sLen; i++) {
char ch = s.charAt(i);
if (ch>='a' && ch<='z') {
lnum = 0;
}
if (ch>='A' && ch<='Z') {
unum = 0;
}
if (ch>='0' && ch<='9') {
dnum = 0;
}
// fix bug
if (i == 0) {
sameseq = 1;
}
else if (ch != s.charAt(i-1)) {
if (sameseq >= 3) {
// 这个很重要
while (sLen + ricount < 6 && sameseq >= 3) {
ricount++;
sameseq -= 2;
}
while (sLen - rdcount > 20 && sameseq >= 3) {
rdcount++;
sameseq --;
}
rcount += sameseq / 3;
}
sameseq = 1;
}
else {
sameseq++;
}
}
// fixbug
if (sameseq >= 3) {
// 这个很重要
while (sLen + ricount < 6 && sameseq >= 3) {
ricount++;
sameseq -= 2;
}
while (sLen - rdcount > 20 && sameseq >= 3) {
rdcount++;
sameseq --;
}
rcount += sameseq / 3;
}
//System.out.printf("rcount: %d, ricount: %d, rdcount: %d, lnum: %d, unum: %d, dnum: %d\n",
// rcount, ricount, rdcount, lnum, unum, dnum);
int update = lnum + unum + dnum;
int must = ricount + rcount;
if (sLen + ricount < 6) {
must += 6 - sLen - ricount;
}
if (sLen < 20) {
return must > update ? must : update;
}
// 跟上面的不一样,因为删除字符是无法增加新的类型的
if (sLen - rdcount > 20) {
rdcount += sLen - rdcount - 20;
}
return rcount >= update ? rcount + rdcount : update + rdcount;
}
}
以下是加了注释的版本:
public class Solution {
public int strongPasswordChecker(String s) {
int sLen = s.length();
if (sLen < 4) {
return 6 - sLen;
}
int lnum = 1; // need lower
int unum = 1; // need upper
int dnum = 1; // need digit
int rcount = 0; // count need to replace repeated seq
int ricount = 0; // count need to add in repeated seq
int rdcount = 0; // count need to remove from repeated seq
int sameseq = 0; // count of chars in repeated seq
for (int i=0; i<sLen; i++) {
char ch = s.charAt(i);
if (ch>='a' && ch<='z') {
lnum = 0;
}
if (ch>='A' && ch<='Z') {
unum = 0;
}
if (ch>='0' && ch<='9') {
dnum = 0;
}
// check repeated seq
if (i == 0) {
sameseq = 1;
}
else if (ch != s.charAt(i-1)) {
if (sameseq >= 3) {
// if shorter length, add char into repeated seq
while (sLen + ricount < 6 && sameseq >= 3) {
ricount++;
sameseq -= 2;
}
// if longer length, remove char from repeated seq
while (sLen - rdcount > 20 && sameseq >= 3) {
rdcount++;
sameseq --;
}
// if length matches, replace char in repeated seq
rcount += sameseq / 3;
}
sameseq = 1;
}
else {
sameseq++;
}
}
// need check repeated seq after loop
if (sameseq >= 3) {
// as previous process
while (sLen + ricount < 6 && sameseq >= 3) {
ricount++;
sameseq -= 2;
}
while (sLen - rdcount > 20 && sameseq >= 3) {
rdcount++;
sameseq --;
}
rcount += sameseq / 3;
}
int update = lnum + unum + dnum;
int must = ricount + rcount;
if (sLen + ricount < 6) {
must += 6 - sLen - ricount;
}
if (sLen < 20) {
return must > update ? must : update;
}
// if longer length, use below process
if (sLen - rdcount > 20) {
rdcount += sLen - rdcount - 20;
}
return rcount >= update ? rcount + rdcount : update + rdcount;
}
}
准备发表在Discuss版:
https://discuss.leetcode.com/category/549/strong-password-checker
【好】strong-password-checker,我自己做出来的:)的更多相关文章
- [LeetCode] Strong Password Checker 密码强度检查器
A password is considered strong if below conditions are all met: It has at least 6 characters and at ...
- Leetcode: Strong Password Checker
A password is considered strong if below conditions are all met: It has at least 6 characters and at ...
- [Swift]LeetCode420. 强密码检验器 | Strong Password Checker
A password is considered strong if below conditions are all met: It has at least 6 characters and at ...
- Hard模式题目
先过一下Hard模式的题目吧. # Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Ha ...
- 练练脑,继续过Hard题目
http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧. # Title Editorial Acceptance Diffi ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 继续过Hard题目
接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧. # Title Editorial Acceptance ...
随机推荐
- python 函数内置方法short_desc
1. 给函数设置一个文本 def action_checked(self, request): pass action_checked.short_desc = "签到" # sh ...
- python Mixin 是个啥?
内容待添加... 参考文章: [1][python] Mixin 扫盲班
- hdu5823
官方题解:直接状压dp就行了,f[S]表示点集S的色数,枚举子集转移(子集是独立集).这样是3^n的. 这样就可以过了……(独立集就是点互相没有连边) 学到了一个穷举子集的简便写法 for (int ...
- Openstack 清除openstack网络与路由 (十七)
一)清除openstack网络与路由 “清除openstack网络与路由”和”添加openstack网络与路由”的操作步骤相反. 添加网络或路由时是先建 搭建网络>搭建子网>建立端口, 而 ...
- OpenStack 计算服务 Nova介绍和控制节点部署 (八)
一)nova在keystone上服务注册 1.1创建nova务实体 [root@controller ~]# source admin-openrc [root@controller ~]# open ...
- Hibernate + Oracle 创建自增序列ID
1.创建自增序列 2.对ID创建触发器 3.Userinfo.hbm.xml使得<generator class="increment"> 序列: MAXVALUE I ...
- Flask实战第50天:cms添加轮播图的模态对话框制作
编辑cms_banners.html, 在{% block main_content%}中加上表给内容如下 {% block main_content %} <table class=" ...
- BZOJ 3289 Mato的文件管理(莫队+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题目大意] 求静态区间逆序对. [题解] 我们对查询进行莫队操作,对于区间的删 ...
- 实验三 敏捷开发与XP实践实验报告
实验三 敏捷开发与XP实践实验报告 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vi ...
- [NOI2017]整数
[NOI2017]整数 题目大意: \(n(n\le10^6)\)次操作维护一个长度为\(30n\)的二进制整数\(x\),支持以下两种操作: 将这个整数加上\(a\cdot2^b(|a|\le10^ ...