验证回文字符串 Ⅱ 当左指针和右指针所指向的值不想等时,删除一个字符有两种情况,从左删或者从右删。此时,左指针的左边和右指针的右边已经是对称的了,只用判断s[i+1]-s[j] 或者 s[i]-s[j-1]的对称性
bool validPalindrome ( char * s) {
for ( int i = 0 , j = strlen ( s) - 1 ; i < j; ++ i, -- j)
if ( s[ i] != s[ j] )
return ( judge ( s, i+ 1 , j) || judge ( s, i, j- 1 ) ) ;
return true;
}
int judge ( char * str, int i, int j) {
while ( i < j) if ( str[ i++ ] != str[ j-- ] ) return false;
return true;
}
反转字符串中的元音字母 拿到这题,我在想,what?元音字母是什么来着,hhhhhhh 在i<j的情况,i负责从左到右找元音字母,j负责从右到左找元音字母,然后交换即可
int vowel ( char c)
{
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
return 1 ;
else
return 0 ;
}
char * reverseVowels ( char * s) {
int i = 0 , j = strlen ( s) - 1 ;
char t;
while ( i < j) {
while ( i < j && ! vowel ( s[ i] ) )
i++ ;
while ( i < j && ! vowel ( s[ j] ) )
j-- ;
t = s[ i] ;
s[ i] = s[ j] ;
s[ j] = t;
i++ ;
j-- ;
}
return s;
}
平方数之和 需要注意的就是j的取值即可
bool judgeSquareSum ( int c) {
long i= 0 , j= sqrt ( c) , num;
while ( i<= j) {
num= i* i+ j* j;
if ( num== c)
return true;
else if ( num> c) {
j-- ;
} else
i++ ;
}
return false;
}
两数之和 II - 输入有序数组 注意已知条件,数组已升序排列了,又是找两数,很容易想到双指针哈
int * twoSum ( int * numbers, int numbersSize, int target, int * returnSize) {
int * result= ( int * ) malloc ( sizeof ( int ) * 2 ) ;
int i= 0 , j= numbersSize- 1 , sum;
while ( i< j) {
sum= numbers[ i] + numbers[ j] ;
if ( sum== target) {
result[ 0 ] = i+ 1 ;
result[ 1 ] = j+ 1 ;
* returnSize= 2 ;
return result;
}
else if ( sum> target)
j-- ;
else
i++ ;
}
return NULL ;
}
合并两个有序数组 注释掉的部分,正序插入的,是我第一遍想到的,需要想的情况较多喃,看到不舒服。 第二种是倒序的情况,清晰明了些,因为两个数组本来就是有序的嘛
void merge ( int * nums1, int nums1Size, int m, int * nums2, int nums2Size, int n) {
int index1 = m - 1 , index2 = n - 1 ;
int indexMerge = m + n - 1 ;
while ( index1 >= 0 || index2 >= 0 ) {
if ( index1 < 0 ) {
nums1[ indexMerge-- ] = nums2[ index2-- ] ;
} else if ( index2 < 0 ) {
nums1[ indexMerge-- ] = nums1[ index1-- ] ;
} else if ( nums1[ index1] > nums2[ index2] ) {
nums1[ indexMerge-- ] = nums1[ index1-- ] ;
} else {
nums1[ indexMerge-- ] = nums2[ index2-- ] ;
}
}
}
环形链表 这道题非常典型,不用多说什么
bool hasCycle ( struct ListNode * head) {
if ( head== NULL )
return false;
struct ListNode * slow= head, * fast= head-> next;
while ( fast!= NULL && slow!= NULL && fast-> next!= NULL )
{
if ( fast-> val== slow-> val)
return true;
slow= slow-> next;
fast= fast-> next-> next;
}
return false;
}
通过删除字母匹配到字典里最长单词 其实就是考查字符串数组中,是否有s的子序列,如果有,则继续判断是不是最长,字典排序最小的。否则返回空。 顺带再注意一下二级指针的使用,有点遗忘了
bool IsSubtr ( char * s1, char * s2) {
int length1= strlen ( s1) , length2= strlen ( s2) , i= 0 , j= 0 ;
while ( i< length1&& j< length2) {
if ( s1[ i] == s2[ j] )
j++ ;
i++ ;
}
return j== length2;
}
char * findLongestWord ( char * s, char * * d, int dSize) {
int i, maxLength= 0 ;
char * p, * longWord= "" ;
for ( i= 0 ; i< dSize; i++ ) {
maxLength= strlen ( longWord) ;
p= d[ i] ;
if ( ( strlen ( p) == maxLength&& strcmp ( p, longWord) > 0 ) || strlen ( p) < maxLength)
continue ;
if ( IsSubtr ( s, p) ) {
longWord= p;
}
}
return longWord;
}