怒刷leetcode题目(2)235,191,141,217,
来源:互联网 发布:靠谱韩代淘宝 豆瓣 编辑:程序博客网 时间:2024/05/20 03:39
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { if(root==NULL || p==NULL || q==NULL){ return false; } if(p->val<root->val &&q->val<root->val ){ return lowestCommonAncestor(root->left, p,q); }else if(p->val>root->val &&q->val>root->val){ return lowestCommonAncestor(root->right, p,q); }else if(p->val == root->val ||q->val == root->val){ return root; }else{ return root; }}首先大家必须了解BST的特点,若两个节点都在根节点的同一侧,就可以将那一侧的子树拿出来做递归,直到遇到根节点是其中一个节点,返回该节点。或者两点分别在节点的两侧,返回该节点。
191Number of 1 Bits37.6%Easy
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
int hammingWeight(uint32_t n) { int count= 0; while(n!=0){ n = n & (n-1); count++; } return count;}n-1可以将最右的1变成0,右方的0全部变成1。
例如:1011011000会变成1011010111
将n&(n-1)可以将最右边的0去掉,重复就可以将n变成0,记录次数,返回次数即可
141Linked List Cycle36.3%Medium
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) { if(head==NULL){ return false; } struct ListNode *pointer1 = head; struct ListNode *pointer2 = head; while(pointer2!=NULL && pointer2->next!=NULL){ pointer1 = pointer1->next; pointer2 = pointer2->next->next; if(pointer1 == pointer2){ return true; } } return false;}
使用快慢指针,快指针一次走两步,慢指针一次走一步,如果运行之后相遇,则是有循环
217Contains Duplicate36.0%EasyGiven an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
bool containsDuplicate(int* nums, int numsSize) { if(nums==NULL||numsSize<2){ return false; } for(int i = 0; i<numsSize;i++){ for(int j=i+1;j<numsSize;j++){ if(nums[i]==nums[j]){ return true; } } } return false;}
这个方法不是最好的,看tags提醒这道题应该用hashtable做,不过c没有现成的hashtable,不知道该怎么实现,请大神们指教。
- 怒刷leetcode题目(2)235,191,141,217,
- leetcode题目解答报告(2)
- leetcode题目总结<2>
- LeetCode题目2
- leetcode部分题目(C++)
- 怒刷leetcode的题目(1)237、104、136、100
- 怒刷leetcode题目(3)226,83,142,86
- leetcode题目
- LeetCode 题目
- < 题目 > LeetCode
- [LeetCode] 650. 2 Keys Keyboard ## 题目描述
- leetcode 题目标记(补充版)
- MaxGap leetcode题目(桶排序)
- leetcode题目:Word Pattern (Javascript版)
- LeetCode经典题目笔记(一)
- LeetCode经典题目笔记(二)
- (Leetcode)backtracking回溯法 题目汇总
- [Leetcode] 题目索引(不断更新中)
- 2015/7/21/接口/工厂设计模式/封装/抽象
- 项目的导入导出
- php const和define的区别(php类中如何定义常量)
- OJ第六批——Problem A: A代码完善--向量的运算
- OJ 纯虚函数
- 怒刷leetcode题目(2)235,191,141,217,
- cocos2dx自学之 事件监听
- CSS-预处理器
- linux: 几个常用makefile模板
- 微信API获取坐标的JS
- mysql分表的3种方法
- get传参如何传递数组
- 嘻唰唰第六批 A代码完善--系统日期
- qt使用tab管理多个界面