面试题算法

快速排序

int key(vector<int> &a, int low, int high)
{
	int key = a[low];
	while (low < high)
	{
		while (low < high&&key <= a[high])
			high--;
		a[low] = a[high];
		while (low<high&&key>=a[low])
			low++;
		a[high] = a[low];
	}
	a[low] = key;
	return low;

}

void QSort(vector<int> &a, int low, int high)
{
	int key_record;
	if (low < high)
	{
		key_record = key(a,low,high);
		QSort(a, low, key_record - 1);
		QSort(a, key_record + 1, high);
	}


}

int main()
{
	vector<int> a{ 1,5,9,7,5,6,85,98,45,65,45,87,5,4,65,0,64,48 };
	int len = a.size();
	
	QSort(a, 0, len - 1);

	cout << "快排:" << endl;
	for (int i = 0; i < len; i++)
	{
		cout << a[i] << "  ";
	}
	cout << endl;
	system("pause");
}

实现memcpy

void* my_memcpy(void *dest, const void*src, size_t length)
{
	assert(dest != NULL && src != NULL);

	char *pdest = (char*)dest;
	const char *psrc = (char*)src;
	while (length--)
	{
		*pdest++ = *psrc++;
	}
	return dest;
}
int main()
{
	char a[] = "sdsf";
	char b[] = "hdfhgh";

	my_memcpy(a, b, 4);

	printf("%s", a);


	system("pause");
}

实现strcpy

char* my_strcpy(char* dest,const char* src)
{
	assert((dest != NULL) && (src != NULL));
	char *pDest = (char*)dest;
	while ((*pDest++ = *src++) != '\0');
		
	return dest;
}



int main()
{
	char a[] = "hdfhgh";
	const char b[] = "sfaa";

	my_strcpy(a,b);

	printf_s("%s", a);


	system("pause");
}

leetcode32,题目:

注意:(()()),输出结果为6

参考:https://blog.csdn.net/yuanliang861/article/details/84847024

解题思路:用栈存储左括号,每次括号成对出现时,就将该左括号出栈;计算方式:若出栈后,栈是空栈,长度为 i-开始计算的位置;若出栈后,栈非空,长度为 i-在栈顶的左括号的位置

int longestValidParentheses(string s) {
	int max_len = 0;
	int start = -1;

	int len = s.length();
	if (len == 0)
		return 0;

	stack<int> sta;
	for (int i = 0; i < len; i++)
	{
		if (s[i] == '(')
			sta.push(i);
		else
		{
			if (!sta.empty())
			{
				sta.pop();
				if (!sta.empty())
					max_len = max(max_len, i - sta.top());
				else
					max_len = max(max_len, i - start);
			}
			else
			{
				start = i;

			}
		}
	}
	return max_len;
}

int main()
{
	string s="(()())";
	cout<<longestValidParentheses(s);
	system("pause");
}

题目:

给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。

示例 1:

输入: "abab"

输出: True

解释: 可由子字符串 "ab" 重复两次构成。
示例 2:

输入: "aba"

输出: False
示例 3:

输入: "abcabcabcabc"

输出: True

解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。
注意:输入:“abcabcabc”      输出:true;

解题思路:找到最大因子,以最大因子为子字符串

bool repeatedSubstringPattern(string s)
{
	int n = s.length();
	int m = n / 2;
	for (int i = m; i >= 1; i--)
	{
		if (0 == n % i)
		{
			int count = n / i;
			string tmp = s.substr(0, i);
			int flag = 1;
			for (int t = 1; t < count; t++)
			{
				string cc = s.substr(t*i, i);
				if (cc != tmp)
				{
					flag = 0;
					break;
				}
			}
			if (flag)
			{
				return true;
			}
		}
	}
	return false;
}


int main()
{
	string s="abcabcabc";
	cout<<repeatedSubstringPattern(s);
	system("pause");
}

题目

https://blog.csdn.net/qq_40879213/article/details/82392218

leetcode 365. 水壶问题

https://blog.csdn.net/qq_36946274/article/details/81349875

题目:反转链表

输入: 1->2->3->4->5->NULL

输出: 5->4->3->2->1->NULL

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL) return NULL;
        ListNode *newhead=NULL,*q=head;
        while(q!=NULL)
        {
            ListNode* tmp=q->next;
            q->next=newhead;
            newhead=q;
            q=tmp;
        } 
        return newhead;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值