zoukankan      html  css  js  c++  java
  • 984. 不含 AAA 或 BBB 的字符串


    emmm 很遗憾超时间了

    public String strWithout3a3b(int A, int B) {
    		StringBuffer str = new StringBuffer("");
    		int i = A + B;
    		while (true) {
    			if (str.length() == i)
    				break;
    			else {
    				if (str.length() >= 2) {
    					if (str.charAt(str.length() - 1) == str.charAt(str.length() - 2)) {
    						if (str.charAt(str.length() - 1) == 'a') {
    							str .append("b") ;
    							B--;
    						} 
    						else if(str.charAt(str.length() - 1) == 'b'){
    							str.append("a");
    							A--;
    						}
    					}
    				}
    				if (A > B) {
    					str.append("a");
    					A--;
    				} 
    				else {
    					str.append("b");
    					B--;
    				}
    			}
    		}
    		return str.toString();
    	}
    

    优化后

    public String strWithout3a3b(int A, int B) {
    		StringBuffer str = new StringBuffer("");
    		int i = A + B;
    		while (i>0) {
    			if (str.length() == i)
    				break;
    			if (A >= B) {
    					str.append("a");
    					A--;
    				} 
    			else {
    					str.append("b");
    					B--;
    				}
    			if (str.length() == i)
    				break;
    			if (str.length() >= 2 &&str.charAt(str.length() - 1) == str.charAt(str.length() - 2)) {
    					if (str.charAt(str.length() - 1) == 'a') {
    							str .append("b") ;
    							B--;
    						} 
    					else{
    							str.append("a");
    							A--;
    						}
    					}
    		}
    		return str.toString();
    	}
    

    再次修改
    分成两步 一步是判断是否已经是两个连接同样的字母在一起了 ,另一步则是添加

    public String strWithout3a3b(int A, int B) {
    		StringBuffer str = new StringBuffer("");
    		while (A + B > 0) {
     
    			if (str.length() >= 2 && str.charAt(str.length() - 1) == str.charAt(str.length() - 2)) {
    				if (str.charAt(str.length() - 1) == 'a') {
    					str.append("b");
    					B--;
    				} else {
    					str.append("a");
    					A--;
    				}
    			} else {
    				if (A >= B) {
    					str.append("a");
    					A--;
    				} else {
    					str.append("b");
    					B--;
    				}
    			}
    		}
    		return str.toString();
    	}
    }
    

    第二种不会超时

     
    	public String strWithout3a3b(int A, int B) {
    		StringBuffer str = new StringBuffer("");
    		int i = A + B;
    		while (A>0||B>0) {
    			boolean bol = false; 
    			if (str.length() == i)
    				break;
    			else {
    				if (str.length() >= 2 && str.charAt(str.length() - 1) == str.charAt(str.length() - 2)) {
    						if (str.charAt(str.length() - 1) == 'b') {
    							bol = true;
    						} 
    					}
    				else {
    					if(A >= B)
    					bol = true;
    					}
    				
    				if (bol) {
    					str.append("a");
    					A--;
    				} 
    				else {
    					str.append("b");
    					B--;
    				}
    			}
    		}
    		return str.toString();
    	}
    
  • 相关阅读:
    【转】K/3 KIS BOS 插件入门
    [转]SQL Server 存储过程中使用 in 动态变量
    [转]Delphi 12种大小写转换的方法
    cxGrid FilterRow 添加左模糊查询,实现 %ABC%
    cxGrid 锁定一行,让该行数据不能编辑
    K/3工业单据K3BillTransfer的属性及使用方法
    VB6上创建金蝶K/3或KIS旗舰版插件
    MySQL UTF8 中文乱码处理
    两种方法自动部署代码:webhooks钩子自动部署代码方法一 及定时任务自动部署二 简介
    Linux查看进程的4种方法
  • 原文地址:https://www.cnblogs.com/cznczai/p/11150473.html
Copyright © 2011-2022 走看看