怒刷leetcode题目(2)235,191,141,217,
2015-07-21 18:30
453 查看
235 | Lowest Common Ancestor of a Binary Search Tree | 38.6% | Easy |
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
2and
8is
6.
Another example is LCA of nodes
2and
4is
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的特点,若两个节点都在根节点的同一侧,就可以将那一侧的子树拿出来做递归,直到遇到根节点是其中一个节点,返回该节点。或者两点分别在节点的两侧,返回该节点。
191 | Number of 1 Bits | 37.6% | Easy |
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,记录次数,返回次数即可
141 | Linked List Cycle | 36.3% | Medium |
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; }
使用快慢指针,快指针一次走两步,慢指针一次走一步,如果运行之后相遇,则是有循环
217 | Contains Duplicate | 36.0% | Easy |
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,不知道该怎么实现,请大神们指教。
相关文章推荐
- java 启动线程三种方式
- Merge Sorted Array
- Extjs-Grid-动态生成columns
- 6.对学生成绩进行统计计算,参加考试的有6名学生,考试成绩分别为94.5,89.0,79.5,64.5,81.5,73.5,显示考试的总分和平均分,之后显示大于考试平均分的成绩信息。请写出实现上述功能
- ERROR 1194 (HY000): Table 't1' is marked as crashed and should be repaired
- 海量数据相似度计算之simhash短文本查找
- mysql dump shell 脚本crontab 计划任务
- vmware装各种系统
- A*算法——第二种
- hdoj 4552 怪盗基德的挑战书 【KMP 求所有前缀在原串种出现的次数之和】
- 微信获取坐标的JS
- Table View Controller注意事项
- 2015年6月积累
- 关于如何改变thinkphp中自定义的成功界面、错误界面、异常界面
- 那个青春
- cocos2dx自学之 动作特效与场景切换特效
- 怎么旋转PDF文件的方向并保存成功
- android 自定义属性步骤
- POJ 2299 逆序对
- 动态集合的两个最近的数字之间的差的