[算法训练营day1]LeetCode704. 二分查找 LeetCode27. 移除元素 LeetCode704. 二分查找 题目链接:704. 二分查找 初次尝试 看到题目标题是二分查找,所以尝试使用二分查找的思想,代码思路是一直循环二分,直到两个指针相等,再判定所指元素是否等于target,这样对于任何输入都需要二分至尽头才能得出结论,果不其然提交后超时. class Solution { public: int search(vector<int>& nums, int tar
题目地址:http://oj.tsinsen.com/A1082 问题描述 给定一个大小为n的数组s和一个整数K,请找出数组中的第K小元素. 这是一个补充程序的试题,你需要完成一个函数: int findKth(int *s, int n, int K) 表示在s指向的数组中找到第K小的元素(如果K=1,表示找最小元素),你需要返回该元素的值. 此题对时间的要求比较高,请注意下面的算法描述. 算法描述 你可以直接将s中的元素进行排序后输出第K小的元素,但使用这种方法你大概只能得到30%的分数.
方法一: >>> mylist = [1,2,2,2,2,3,3,3,4,4,4,4] >>> myset = set(mylist) >>> for item in myset: print("the %d has found %d" %(item,mylist.count(item))) the 1 has found 1 the 2 has found 4 the 3 has found 3 the 4 has found 4