Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters

CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters

編輯:關於Android編程

Eliminate all ‘b’ and ‘ac’ in an array of characters, you have to replace them in-place, and you are only allowed to iterate over the char array once.

Examples:
abc -> ac
ac->''
rbact->rt

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The key point is in-place and iteration once.


int eliminate( char* p)
{
  int deleted = 0;
  if (! p )
    return deleted;
  while (*p){
    if (*p == 'b')
	  deleted++;
	else if ( ( *p == 'a' ) && ( *(p+1) == 'c')){
	  deleted += 2;
	  p++;
	} 
	else if ( deleted > 0 )
	  *(p-deleted) = *p;
	p++;
  }
  *(p-deleted) = '\0';
  return deleted;
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved