- 博客(21)
- 收藏
- 关注
原创 三种方法解决Leetcode169. Majority Element in Python
1. dictionary class Solution: """ @param: nums: a list of integers @return: find a majority number """ def majorityNumber(self, nums): if len(nums)==0: ret...
2018-09-26 08:23:43 288
原创 三种方法解决Lintcode39 Recover Rotated Sorted Array in Python
1. 三次翻转首先找到最小值的index,而后翻转三次。for example, [4,5,1,2,3], 先找到1的index2,再以2为轴:[4,5]->[5,4]; [1,2,3]->[3,2,1]; [5,4,3,2,1]->[1,2,3,4,5]class Solution: """ @param nums: An integer arra...
2018-09-26 07:29:12 242
原创 两种方法解决leetcode 53. Maximum Subarray
1. 第一种方法,pre_sum是无脑加,max是对比“pre_sum减去前面一个最小的元素”和current max的对比。把我自己绕进去了。上代码:def maxSubArray(self, nums): if len(nums)==0: return 0 pre_sum = 0 min_sum = 0 ...
2018-09-25 07:08:58 151
原创 两种方法解决leetcode206:Reverse Linked List
方法一:递归其实在正序递归的时候已经确定了tail元素。回溯的时候,进行两步1. 让head.next指向head2. 让head指向noneclass Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNo...
2018-09-14 09:31:57 175
原创 中文自然语言处理示例__LSTM with Attention Model运用于中文医学报告预测_Part1
中文的自然语言处理和不像英语那么方便,要遇到各种各样的问题. 几个大方向,除了删去一些data里原本的错误之外,还要创造中文和数字的字典,替代中文中的特殊字符,还要处理文本,保持长度的一致,等等. Part1主要是在model之前,讲讲如何preprocess中文文本. 话不多说,现在开始啦. data长这样,15997个obs, 目的是用description predict conclusio...
2018-06-20 14:51:06 793 2
原创 Python solving Topological Sorting with explanation
拓扑排序: class Solution: """ @param: graph: A list of Directed graph node @return: Any topological order for the given graph. """ def dfs(self,i,countrd,res): # dfs就是把入度为0的ite...
2018-06-20 11:23:15 155
原创 Leetcode78 Subset
构造递归函数。先加进数来找子集,再减去数来找其他子集。class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.results=[] ...
2018-06-02 05:26:05 164
原创 Python- How to format datetime and replace value by multiple datetime conditions
1 Format datetime sample un-formatted date in my dataset:17-Jan-18 use "%d-%b-%y" to impute , which changes to 2017-01-18Aug 20,2017 , use %b %d, %Y" to impute other version see reference: http://strf...
2018-06-01 09:42:24 138
原创 Partition方法(同向/双向双指针)解决 215. Kth Largest Element in an Array
1. quickselect. (相向双指针)。原理和partition里讲的一样。class Solution(object): def findKthLargest(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ ...
2018-05-31 03:51:26 545
原创 Lintcode 31. Partition Algorithm
Partition算法。快排实习的关键。 这里只需要把比k大的放右边,小的放左边,并没有继续再在左右partition里排序。相当于完成了快排的一个步骤。思想:双指针,左边和K对比,if nums[start]<k, 说明就不是一个swap candidate(因为比K小的本来就应该好好在左边呆着), 于是start+1继续往下找。同理,右边和K对比,if nums[end]>=k, ...
2018-05-30 06:48:10 277
原创 双指针解决Leetcode 15. 3 Sum
1. 3sum其实和2sum思路差不多。只要把第三个数变成2sum的target就好了。当然要先sort一下,原数组是无序的。2. 也要考虑重复数组的情况。比如left[i]==left[i-1]说明这个数字在i-1的时候应该考虑过了,需要skip.同理right也是一样。还有一种重复情况是,当target重复的时候,也要skip.3. 有个特殊情况我一直过不去。[0,0,0],差点用特殊情况if...
2018-05-30 04:07:14 286
原创 两种方法解决 Sort Color II - with multiple colors k
1. 最直观的方法,把list存成dictionary,key为color,value为color出现的次数,而后循环一个个导出。 def sortcolor2(colors,k): count={} for c in colors: if c not in count: count[c]=0 else: ...
2018-05-30 01:45:19 301
原创 两种方法解决 Leetcode167. Two Sum II - Input array is sorted
1. 双指针:class Solution: """ @param nums: an array of Integer @param target: target = nums[index1] + nums[index2] @return: [index1 + 1, index2 + 1] (index1 < index2) """ def t...
2018-05-27 00:56:28 100
原创 5. Longest Palindromic Substring
这题。。很迷。。。过一遍char,指针一左一右,找回文。一般是当left=right=某个数字的时候,进入while里找最长回文,同时updateleft and right. 其他情况abs(left-right)=1, 进入下一位。O(n*2)def longestPalindrome(s): ansl, ansr, maxx = 0, 1, 0 length ...
2018-05-19 05:33:23 84
原创 Deep Learning_Autonomous driving application - Car detection_自动驾驶代码实现
Autonomous driving - Car detectionWelcome to your week 3 programming assignment. You will learn about object detection using the very powerful YOLO model. Many of the ideas in this notebook are descri...
2018-04-08 09:02:42 503
原创 Data visualization_Ted Talk Data
df = pd.read_csv(r'ted_main.csv')df.keys()df.dtypespd.isnull(df).sum() # check missing valuecomments 0description 0duration 0event 0film_date...
2018-04-07 03:58:08 351
原创 Python_data pre-processing
All of the tech are basic solving solution. In order to obtain robust predictive model, missing value imputation should be consider case by case. 如果是个小白,比如我,就可以这样非常generally clean data.0. Check for sh...
2018-04-03 08:11:50 879
原创 Leetcode_34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target i...
2018-03-27 10:39:02 93
原创 Leetcode_33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the a...
2018-03-27 10:31:42 107
原创 两种方法解决leetcode 153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exis...
2018-03-25 17:14:22 380
原创 Binary Search_278_First Bad Version
Binary Search 278 First Bad Version Regular binary search question, provide python solution below# first version 32msclass Solution(object): def firstBadVersion(self, n): """ :type...
2018-03-25 16:11:21 114
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人