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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值