28. Implement strStr()
Description:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:
两层循环,匹配上后进入下一个循环,循环结束后,分别判断第一个、第二个是否结束。第一个结束,返回-1,第二个结束返回对应的index。如果都不符合则跳至匹配点的下一个位置,继续。
代码:
int strStr(char* haystack, char* needle) {
int index = -1;
char *last;
char *haystack_temp = haystack;
char * needle_temp = needle;
if(NULL == haystack || NULL == needle){
return -1;
}
if(0 == strlen(needle)){
return 0;
}
while(*haystack && *needle){
if(*haystack == *needle){
last = haystack;
haystack++;
needle++;
while(*haystack && *needle){
if(*haystack == *needle){
haystack++;
needle++;
continue;
}
break;
}
//printf("%c %c\n", *haystack, *needle);
if(0 == *needle){
index = last - haystack_temp;
return index;
}
if(0 == *haystack){
return index;
}
haystack = last + 1;
needle = needle_temp;
}else{
haystack++;
}
}
return index;
}
125. Valid Palindrome
Description:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
思路:
跳过不符合要求的字符,并且对大写进行转换,然后首尾对比。
代码:
bool is_alphanumeric(char c){
return ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9'));
}
bool isPalindrome(char* s) {
if(NULL == s){
return true;
}
int len = strlen(s);
char *start, *end;
char c_start, c_end;
if(0 == len){
return true;
}
start = s;
end = s+len-1;
while(start < end){
if(false == is_alphanumeric(*start)){
start++;
continue;
}
if(false == is_alphanumeric(*end)){
end--;
continue;
}
c_start = *start;
c_end = *end;
if(c_start>='A' && c_start<='Z'){
c_start += 'a' - 'A';
}
if(c_end>='A' && c_end<='Z'){
c_end += 'a' - 'A';
}
if(c_start == c_end){
start++;
end--;
}else{
return false;
}
}
return true;
}
665. Non-decreasing Array
Description:
Given an array with n
integers, your task is to check if it could become non-decreasing by modifying at most 1
element.
We define an array is non-decreasing if array[i] <= array[i + 1]
holds for every i
(1 <= i < n).
思路:
找到非递增的点,然后试图将该点或者后面的点去掉,查看队列是否依旧是递增,要求是一个点,也就是执行一次替换。
代码:
bool check_possibility(int *nums, int numsSize, int i)
{
int j;
if(i == numsSize-1){
numsSize--;
}
for(j = 0; j < numsSize-1; j++){
if(j+1 == i){
if(nums[j] > nums[j+2]){
return false;
}
continue;
}
if(j == i){
continue;
}
if(nums[j] > nums[j+1]){
return false;
}
}
return true;
}
bool checkPossibility(int* nums, int numsSize) {
if(0 == numsSize || NULL == nums){
return true;
}
int i, flag = 0;
for(i = 0; i < numsSize-1; i++){
if((nums[i] - nums[i+1]) <= 0){
continue;
}
if(flag == 0){
printf("%d %d %d\n", check_possibility(nums, numsSize, i), check_possibility(nums, numsSize, i+1), i);
if(!check_possibility(nums, numsSize, i) && !check_possibility(nums, numsSize, i+1)){
return false;
}
flag = 1;
}else{
return false;
}
}
return true;
}
479. Largest Palindrome Product
Description:
Find the largest palindrome made from the product of two n-digit numbers.
Since the result could be very large, you should return the largest palindrome mod 1337.
思路:
穷举。
最佳思路:
没搞明白。
代码:
int reverse(int x)
{
int rtn = 0;
while(x)
{
rtn = rtn*10 + x%10;
x /= 10;
}
return rtn;
}
int largestPalindrome(int n) {
if(n == 1)
return 9;
int max = pow(10,n), rtn = 0;
for(int x = 1; x < max; x++)
{
long L = max - x;
long R = reverse(L);
int dif = pow(x,2) - 4*R;
if(dif < 0)
continue;
else
{
if(sqrt(dif) == (int)sqrt(dif))
{
rtn = (L*max + R)%1337;
break;
}
}
}
return rtn;
}
222. Count Complete Tree Nodes
Description:
Given a complete binary tree, count the number of nodes.
思路:
直接遍历,结果超时,然后就是查了下定义,判断左子树和右子树是否相同,相同则返回计算值,否则继续遍历。
最佳思路:
完全数有个特点,即左子树的左深度(即最大深度)与右子树的深度相差不会超过1,利用这个特点,先计算出左右子树的左深度。然后如果相等,则说明左子树是满的,继续遍历右子树。大小就是2^ln-1+1+右子树的大小。如果不相等,则说明右子树是满的,左子树不能确定,大小就是2^rn-1+1+左子树的大小。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/*
int countNodes(struct TreeNode* root) {
int hLeft = 0, hRight = 0;
struct TreeNode *pLeft = root, *pRight = root;
while (pLeft) {
++hLeft;
pLeft = pLeft->left;
}
while (pRight) {
++hRight;
pRight = pRight->right;
}
if (hLeft == hRight) return pow(2, hLeft) - 1;
return countNodes(root->left) + countNodes(root->right) + 1;
}
*/
typedef struct TreeNode tn;
int height(tn* root){
int h=0;
while(root){
root=root->left;
h++;
}
return h;
}
int countNodes(struct TreeNode* root) {
if(root==NULL)
return 0;
int lh=height(root->left), rh=height(root->right);
if(lh==rh){
return 1+(1<<lh)-1+countNodes(root->right);
}
else
return 1+(1<<rh)-1+countNodes(root->left);
}
189. Rotate Array
Description:
Given an array, rotate the array to the right by k steps, where k is non-negative.
思路:
拷贝然后移动至对应地方,注意k大于numsSize的情况,需要求余。
代码:
void rotate(int* nums, int numsSize, int k) {
if(0 == numsSize || NULL == nums || 0 == k){
return;
}
int i;
int *p = NULL;
if(k > numsSize){
k %= numsSize;
}
p = malloc(sizeof(int) * k);
if(p){
memcpy(p, nums+numsSize-k, sizeof(int) * k);
for(i = numsSize-k-1; i >= 0; i--){
nums[i+k] = nums[i];
}
memcpy(nums, p, sizeof(int) * k);
free(p);
}
}
859. Buddy Strings
Description:
Given two strings A
and B
of lowercase letters, return true
if and only if we can swap two letters in A
so that the result equals B
.
思路:
先去掉长度小于2的情况,直接返回false。
接下来分两种情况讨论,一种是两个串完全相同,排序,找到包含有相同字母的返回true否则返回false。另一种情况是不相同,则找到第一个不相同的入队,然后下一个不相同的对比。
代码:
int char_cmp_func(void const *p1, void const *p2){
return (*((char *)p1) - *((char *)p2));
}
bool buddyStrings(char* A, char* B) {
if(NULL == A || B == NULL){
return false;
}
if(*(A+1) == *(B+1) && 0 == *(A+1)){
return false;
}
char s_swap[2] = {0, 0};
int flag = 1;
int is_different = 0;
char *p_a = A;
while(1){
if(*A == 0){
if(*B == 0){
break;
}else{
return false;
}
}
if(*A == *B){
A++;
B++;
continue;
}else{
is_different = 1;
if(flag){
if(0 == s_swap[0]){
s_swap[0] = *A;
s_swap[1] = *B;
}else{
if(*A == s_swap[1] && *B == s_swap[0]){
flag = 0;
}else{
return false;
}
}
}else{
return false;
}
A++;
B++;
}
}
if(0 == is_different){
qsort(p_a, strlen(p_a), sizeof(char), char_cmp_func);
printf("%c %c %c %c\n", p_a[0], p_a[1], p_a[2], p_a[3]);
while(*p_a){
if(*(p_a+1) != 0 && *p_a == *(p_a+1)){
return true;
}
p_a++;
}
return false;
}
if(flag){
return false;
}
return true;
}