# Remove Duplicates from sorted Array II

## 题目描述

What if duplicates are allowed at most twice?For example,
Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].

## 代码实现

 int removeDuplicates(int A[], int n) { if(n<=2) return n; int count=2; for(int i=2;i<n;i++) { if(A[i]!=A[count-2]) A[count++]=A[i]; } return count; }

# Sqrx

## 代码实现

 int sqrt(int x) { long r=x; while(r*r>x) r=(r+x/r)/2; return r; }

# Length of last word

## 题目描述

Given a string s consists of upper/lower-case alphabets and empty space characters’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as a character sequence consists of non-space characters only.For example,
Given s =”Hello World”,
return5.

## 题目解析

s[i]‘ ’‘ ’
s[i+1‘\0’某个字符

• 不断对计数加1，直到遇到第1个空格。
• 然后判断此时count是否为0，若为0，说明空格是最后一个字符，所以循环不跳出，继续进行；若count不为零，说明第一个单词已经结束，直接break跳出，返回count。

## 代码实现1

int lengthOfLastWord(const char *s) {       if (s == nullptr)              return 0;       int count = 0;       int len = strlen(s);       for (int i = 0; i < len; i++)       {              if (s[i] != ' ')                     ++count;              else if (s[i] == ' '&&s[i + 1] != '\0')                     count = 0;              else                     break;       }       return count;}

## 代码实现2

 int lengthOfLastWord(const char *s) { if (s == nullptr) return 0; int count = 0; int len = strlen(s); for (int i = len - 1; i >= 0; i--) { if (s[i] != ' ') ++count; else if(count) break; } return count; }