forked from selfboot/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,6 @@ def findKthLargest_2(self, nums, k): | |
1 | ||
[3,2,1,5,6,4] | ||
2 | ||
[1,2,1,3,9] | ||
2 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
|