Skip to content

Commit

Permalink
add 215 cpp solution
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Mar 31, 2016
1 parent 363c8f0 commit 8562ed8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
52 changes: 52 additions & 0 deletions DivideConquer/215_KthLargestElementArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @Author: [email protected]
* @Last Modified time: 2016-03-31 10:09:26
*/

class Solution {
public:
int partition(vector<int> &nums, int begin, int end) {
int pivot = nums[begin];
int pivot_index = begin;
for(int i=begin+1; i!=end;i++){
if(nums[i] >= pivot){
pivot_index+=1;
if(pivot_index != i){
swap(nums[i], nums[pivot_index]);
}
}
}
swap(nums[begin], nums[pivot_index]);
return pivot_index;
}

int findKthLargest(vector<int> &nums, int k) {
return find_k(nums, 0, nums.size(), k);
}

int find_k(vector<int> &nums, int begin, int end, int k){
if( k<=0 || begin>end-1){
return -1;
}
int position = partition(nums, begin, end);
int left_length = position-begin;
if(left_length== k-1){
return nums[position];
}
else if(left_length > k-1){
return find_k(nums, begin, position, k);
}
else{
return find_k(nums, position+1, end, k-left_length-1);
}
}
};

"""
[1]
1
[3,2,1,5,6,4]
2
[1,2,1,3,9]
2
"""
2 changes: 2 additions & 0 deletions DivideConquer/215_KthLargestElementArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ def findKthLargest_2(self, nums, k):
1
[3,2,1,5,6,4]
2
[1,2,1,3,9]
2
"""
9 changes: 9 additions & 0 deletions More.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Binary Indexed Trees


相关题目: [s307. Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/)

参考:
[夜深人静写算法(三) - 树状数组](http://www.cppblog.com/menjitianya/archive/2015/11/02/212171.html)


5 changes: 4 additions & 1 deletion Week07/49.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: xuelangZF
# @Date: 2015-10-20 23:43:28
# @Last Modified by: xuelangZF
# @Last Modified time: 2016-03-26 10:21:40


class Solution(object):
Expand Down
3 changes: 3 additions & 0 deletions Week07/50.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# @Date: 2015-10-20 23:43:28
# @Last Modified time: 2016-03-26 10:22:44


class Solution(object):
Expand Down

0 comments on commit 8562ed8

Please sign in to comment.