【好】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 结束练习
1.文件操作有哪些模式?请简述各模式的作用 r 只读模式 r+ 读写 rb w 只写模式 w+ 写读 wb x 只写模式 x+ 写读 xb a 追加模式 a+ 写读 ab 2.s = '**hello ...
- 深度学习方法:受限玻尔兹曼机RBM(四)对比散度contrastive divergence,CD
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 上篇讲到,如果用Gibbs Sa ...
- hdu 1426(DFS+坑爹的输入输出)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1269 迷宫城堡(Targin算法)
---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Java学习笔记(三)——静态导入,package-info,Fall-through
[前面的话] 算是真正的放松了好几天时间,没有看任何书,没有任何任务,今天是过完年后的第一天上班时间,我又开始了我的学习之路,感觉还没有老,怎么心态越来越平静了,进入工作状态,就好好努力工作,新的一年 ...
- 利用Node 搭配uglify-js压缩js文件,批量下载图片到本地
Node的便民技巧-- 压缩代码 下载图片 压缩代码 相信很多前端的同学都会在上线前压缩JS代码,现在的Gulp Webpack Grunt......都能轻松实现.但问题来了,这些都不会,难道就要面 ...
- 【cocos2d-js网络教程篇】cocos2d-js http网络请求
前言 刚入手cocos2d-js,看到网上的JS的http网络请求,大部分都是错的.原因在于,js-tests里面的网络请求实例没有给出加载完成事件.正确的加载完成事件如下: var xhr = cc ...
- 转型(java)(.net)
/** * 父类 */ class Animal { public void eat() { //输出 父类吃.... } } class Bird extends Animal { public v ...
- Java 中自定义时间格式
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(); String s ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...