编程杂烩

public int sumArray(int[] nums, int start, int end) {
  int i=0;
  int sum=0;
  for(i=start;i<=end;i++){
    sum+=nums[i];
  }
  return sum;
}

public boolean canBalance(int[] nums) {
  int i=0;
  int firstHalfSum;
  int secondHalfSum;
  while(i<nums.length){
    firstHalfSum=sumArray(nums, 0 , i);
    secondHalfSum=sumArray(nums , i+1, nums.length-1);
    if(firstHalfSum==secondHalfSum) return true;
    
    i++;
  }
  
  return false;
}

 

public Map<String, Integer> word0(String[] strings) {
  int i=0;
  Map<String, Integer> wordMap = new HashMap<String, Integer>();
  while(i<strings.length){
    if( ! wordMap.containsKey(strings[i]) ) wordMap.put(strings[i], 0);
    
    i++;
    
  }
  
  return wordMap;
}

 

public String[] allSwap(String[] strings) {
  Map<String, Integer> keyPosMap = new HashMap<String, Integer>();
  
  int i=0;
  
  String tempString;
  int tempPos=0;
  
  while(i < strings.length){
    
    String key=Character.toString(strings[i].charAt(0));
    if(keyPosMap.containsKey(key)){  
      tempString=strings[i];
      tempPos=keyPosMap.get(key);
      strings[i]=strings[tempPos];
      strings[tempPos]=tempString;
      keyPosMap.remove(key);
      
    }
    else{
      keyPosMap.put(key,new Integer(i));
    }
    
    i++;
  }
  
  return strings;
  
}

 

public int countYZ(String str) {
  int i=0;
  String x="x";
  String y="y";
  int count=0;
  
  while(i<str.length()){
    if((str.substring(i,i+1).compareToIgnoreCase("y") ==0) 
      || (str.substring(i,i+1).compareToIgnoreCase("z") ==0) ) {
      if(((i+1) >= str.length() )|| (  Character.isLetter(str.charAt(i+1)) ==false  )){
        count++;
      }
      
    
    }
     i++; 
  }
  return count;
}

 

public boolean gHappy(String str) {
  
  int i=0;
  boolean hasPrevG=false;
  boolean hasNextG=false;
  
  while(i<str.length()){
    if(str.substring(i,i+1).compareTo("g")==0){
      if((i+1)<str.length()){
        if(str.substring(i+1,i+2).compareTo("g")==0){
          hasNextG=true;
        }
      }
      if((i>0) && (str.substring(i-1,i).compareTo("g")==0)){
        hasPrevG=true;
      }
      if((!hasPrevG)&&(!hasNextG)){
        return false;
      }
      else{
        hasPrevG=false;
        hasNextG=false;
       }
      }
      i++;
    }
    
  return true;
  
}

 

public String notReplace(String str) {
  int i=0;
  int posCopy=0;
  StringBuilder sb = new StringBuilder(str.length()+10);
  while(i<str.length()){
    if((i+2)<=(str.length())){
      if(str.substring(i,i+2).compareTo("is")==0){
        if(
          ( (i==0) || (Character.isLetter(str.charAt(i-1))==false) )
          &&
          ( ( (i+2)==str.length() ) ||( Character.isLetter(str.charAt(i+2))==false) )  
          ){
            
            sb.append(str.substring(posCopy, i+2));
            sb.append(" not");
            posCopy=i+2;
          }
        
      }
    }
    i++;  
  }
  
  if(posCopy < (str.length() -1 )){
    sb.append(str.substring(posCopy, str.length()));
  }
  
  return sb.toString();
}

 

public String mirrorEnds(String string) {
  int istart=0;
  int istop=string.length()-1;
  
  while(istart<string.length()){
    if(string.charAt(istart) != string.charAt(istop)){
      break;
    } 
    istart++;
    istop--;
    
  }
  return string.substring(0,istart);
  
}

 

posted @ 2017-02-15 22:37  友哥  阅读(143)  评论(0编辑  收藏  举报