【好】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 ...
随机推荐
- linux下查看机器配置
查看cpu信息:lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): ...
- for in 对象时,属性为非负整数的情况
在我做一个需求的时候 for in 一个对象,对象的属性都是数字 但是我想给这个对象加一个默认的属性跟值 原对象是{5446:"广州市"}.....类似于下去 然后我想给我页面展示 ...
- OpenStack 存储服务 Cinder介绍和控制节点部署 (十三)
Cinder介绍 OpenStack块存储服务(cinder)为虚拟机添加持久的存储,块存储提供一个基础设施为了管理卷,以及和OpenStack计算服务交互,为实例提供卷.此服务也会激活管理卷的快照和 ...
- 【转载】LinearLayout 源码分析
原文地址:https://github.com/razerdp/AndroidSourceAnalysis/blob/master/LinearLayout/android_widget_Linear ...
- Java中的冒泡排序(减少比较次数)
package yzhou.sort; import java.util.Arrays; public class BubbleSort { public static void main(Strin ...
- 异步加载 Echarts图的数据
<script src="~/Scripts/NewEcharts/echarts.js"></script> <script type=" ...
- phpstorm中Xdebug的使用
目 录 1.Xdebug简介 2.Xdebug的安装.操作 2.1环境搭建 2.2配置php.ini 2.3配置PhpStorm 2.4配置PHP Debug 2.5进行调试 1.Xdebug简介 ...
- CodeForces 779A Pupils Redistribution
简单题. 因为需要连边的人的个数一样,又要保证和一样,所以必须每个数字的个数都是一样的. #include<map> #include<set> #include<cti ...
- HZAU 1205 Sequence Number(双指针)
题目链接:http://acm.hzau.edu.cn/problem.php?id=1205 [题意]给你一串数,要求你找到两个数a[i],a[j],使得a[i]<=a[j]且j>=i且 ...
- tarfile/zipfile/shutil
当我们选择使用Python来进行Linux系统管理,那么就免不了会在Python代码中对压缩包进行处理,包括创建压缩包.解压.获取压缩包中的文件列表等 tarfile Python的tarfile标准 ...