【好】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 ...
随机推荐
- y=y||'world'与y=y?y:'world'
1.y=y||’world’ function log(x,y){ y=y||’world’; console.log(x,y) } log(‘hello’)===>hello world lo ...
- git+jenkins在windows机器上新建一个slave节点【转载】
转至博客:上海-悠悠 前言 我们在跑自动化项目的时候,希望有单独的测试机能跑自动化项目,并且能集成到jenkins上构建任务.如果公司已经有jenkins环境了,那无需重新搭建. 只需在现有的平台基础 ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- memcached安装+绑定访问ip
安装: 1.由于memcached是基于libevent的,需要安装libevent,libevent-devel $yum -y install libevent libevent-devel 2. ...
- private是自己私有的,protected是可以让孩子知道的,public是公开的
三种访问权限 public:可以被任意实体访问,数据成员和函数成员可在成员函数,友元,继承类中直接使用.亦可以作为接口,供类的用户使用 protected:只允许子类及本类的成员函数访问,在基类中用法 ...
- nodejs里的express自动刷新高级篇【转载】
搬运自[简书:http://www.jianshu.com/p/2f923c8782c8]亲测可用哦! 最近在使用express框架及mongodb,由于前端和后端代码修改后都需要实现自动刷新功能,刚 ...
- CentOS7.5安装坚果云
1.下载坚果云rpm包,对于CentOS,选fedora那个版本的包 https://www.jianguoyun.com/s/downloads/linux 2.安装 坚果云安装依赖notify-p ...
- HDMI 电视 点对点 桌面超出屏幕
一直在用电视作显示器,但是没有注意点对点到问题,只是感觉字体发虚.直到今天装win10,桌面会超出屏幕,使用intel控制面板调整分辨率后正常,但是注销或重启会再次回复.百度无果,自己摸索,不仅解决了 ...
- 215. Kth Largest Element in an Array【Medium】【找到第 k 大的元素】
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- HDU 4888 Redraw Beautiful Drawings
网络流. $s$向每一个$r[i]$连边,容量为$r[i]$. 每一个$r[i]$向每一个$c[j]$连边,容量为$k$. 每一个$c[j]$向$t$连边容量为$c[j]$. 跑最大流,中间每一条边上 ...