169 229 669 【未完成】

 1 class Solution {
 2 public:
 3     int majorityElement(vector<int>& nums) {
 4         QuickSort(vector<int>& nums,0,right);
 5         int count=0;
 6         for(int i=0;i<nums.size();i++){
 7             if(i>=1&&nums[i]!=nums[i-1]){
 8                 count=1;
 9             }else{
10                 count++;
11             }
12             if(count>=nums.size()/2) return nums[i];
13         }
14     }
15     void QuickSort(vector<int>& nums,int left,int right){
16         if(left>=right)return;
17         int index=QuickSort(nums,left,right);
18         QuickSort(nums,left,index-1);
19         QuickSort(nums,index+1,right);
20         
21     }
22     int PartSort(vector<int>& nums,int left,int right){
23         int key=right;
24         while(left<right){
25             while(left<right && nums[left]<=nums[key]){
26                 left++;
27             }
28             while(left<right && nums[right]>=nums[key]){
29                 right++;
30             }
31             swap(nums[left],nums[right]);
32         }
33         swap(nums[left],nums[key]);
34         return left;
35     }
36 };

python   摩尔投票

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dict={}
        for num in nums:
            if num not in dict:
                dict[num]=1
            else:
                dict[num]++
            if dict[num]>len(nums)/2:
                return num

229

笨办法,,,明天好好和大佬讨论下

 1 class Solution(object):
 2     def majorityElement(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[int]
 6         """
 7         List=[]
 8         dict={}
 9         candidate=None
10         count=0
11         for num in nums:
12             if num not in dict:
13                 dict[num]=1
14             else:
15                 dict[num]+=1
16             if dict[num]>len(nums)/3 and num not in List:
17                 List.append(num)
18         return List

669

错误代码:太天真

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* trimBST(TreeNode* root, int L, int R) {
13         if(root==NULL) return root;
14         while(root->val!=L){
15             if(L>root->val) root=root->right;
16             if(L<root->val) root=root->left;
17         }
18         TreeNode* temp=root;
19         while(temp->val!=R){
20             if(R>temp->val){
21                 temp->left=NULL;
22                 temp=temp->right;
23             }else{
24                 temp->right=NULL;
25                 temp=temp->left;
26             }
27         }
28         return root;
29     }
30 };

使用正确的遍历方法:

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution(object):
 9     def trimBST(self, root, L, R):
10         """
11         :type root: TreeNode
12         :type L: int
13         :type R: int
14         :rtype: TreeNode
15         """
16         if root is None:
17             return None
18         if root.val<L:
19             return self.trimBST(root.right,L,R)
20         if root.val>R:
21             return self.trimBST(root.left,L,R)
22         root.left=self.trimBST(root.left,L,R)
23         root.right=self.trimBST(root.right,L,R)
24         return root

背过别人的代码,明天再看

原文地址:https://www.cnblogs.com/joelwang/p/10493568.html