LeetCode经典题目笔记(二)


九、判断链表是否是回文链表
解法:O(N)\O(N)的解法。
class  Solution { 
public
    ListNode* temp;
    bool  isPalindrome(ListNode* head) { 
        temp = head; 
         return  check(head); 
    } 
    bool  check(ListNode* p) { 
        if ( NULL == p)  return  true ;
        bool  isPal = check(p->next) & (temp->val == p->val);
       temp = temp->next;
        return  isPal;
    } 
};

十、找出无序数组中出现次数大于n/2的数
解法一、用库函数   nth_element(nums.begin(), nums.begin() + nums.size() / 2 , nums.end());
class  Solution {
public :
    int  majorityElement( vector< int > & nums) {
        nth_element(nums.begin(), nums.begin() + nums.size() /  2 , nums.end());
         return  nums[nums.size() /  2 ];
    } 
};
解法二、随机枚举(居然是最快的-_-
class  Solution {
public :
     int  majorityElement( vector< int > & nums) {
         int  n = nums.size();
        srand( unsigned (time( NULL )));
         while  ( true ) {
             int  idx = rand() % n;
             int  candidate = nums[idx];
             int  counts =  0
             for  ( int  i =  0 ; i < n; i++)
                if  (nums[i] == candidate)  counts++; 
             if  (counts > n /  2
                return  candidate;
        }
    }
};
解法三、
class  Solution { 
public
    int  majorityElement( vector< int > & nums) { 
        int  major, counts =  0 , n = nums.size(); 
         for  ( int i = 0 ; i < n; i++) { 
            if  (!counts) { 
                 major = nums[i]; counts =  1
            } 
            else   counts += (nums[i] == major) ? 1 : - 1
        } 
        return  major;
 } 
};

十一、删除排序数组中重复的元素
解法:遍历一遍,记录下已遍历的元素中重复的个数,把元素向前移动重复个数位置就行了。
class  Solution {
public :
     int  removeDuplicates(vector< int >& nums) {
         int  count =  0 ;
         for  ( int  i =  1 ; i < nums.size(); i++){
             if  (nums[i] == nums[i- 1 ]) count++;
             else  nums[i-count] = nums[i];
        }
         return  nums.size()-count;
    }
};

十二、一个字符串只含大小写字符和空格,返回字符串中最后一个单词的长度
解法:遍历一遍,统计每个单词的长度,若遇到空格则把前一个单词的长度置0.
class  Solution {
public :
     int  lengthOfLastWord(string s) {
         int  len =  0 ;
         for  ( int  i =  0 ;i < s.size();) {
             if  (s[i++] != ' ') len++;
             else if  (s[i] != '\ 0 '&&s[i] != ' ') len =  0 ;
        }
         return  len;
    }
};

十三、二叉树的镜像
解法一:递归。
class  Solution {
public :
    TreeNode* invertTree(TreeNode* root) {
         if  (!root)
             return  root;
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
         return  root;
    }
};
解法二:非递归。
class  Solution {
public :
    TreeNode* invertTree(TreeNode* root) {
        stack<TreeNode*> s;
        s.push(root);
         while  (!s.empty()) {
            TreeNode* temp = s.top();
            s.pop();
             if  (temp) {      
            swap(temp->left,temp->right);
                s.push(temp->right);
                s.push(temp->left);
            }
        }
         return  root;
    }
};

十四、Happy数的判断

解法:利用链表判环方法,快慢”指针“。Happy数最终会以1结束,非Happy数会无限循环,所以只需要判断是否出现环即可。
class  Solution {
public :
     int  digitSquareSum( int  n) {
         int  sum =  0 , tmp;
         while  (n) {
            tmp = n %  10 ;
            sum += tmp * tmp;
            n /=  10 ;
        }
         return  sum;
    }
     bool  isHappy( int  n) {
         int  slow, fast;
        slow = fast = n;
         do  {
            slow = digitSquareSum(slow);
            fast = digitSquareSum(fast);
            fast = digitSquareSum(fast);
             if  (fast ==  1 return  1 ;
        } while  (slow != fast);
         return  0 ;
}
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值