leetcode随笔III
2016-02-28 19:35
288 查看
leetcode题目
1.1. 题目描述
1.2知识点及思路
1.3代码
总结
一.leetcode题目
1.Determine whether an integer is a palindrome. Do this without extra space.
题目描述:判断一个整数是否是回文,不能用额外的空间
知识点:数字最低与最高位取法;二分查找[思想]
思路:①求得整数位数n,②判断条件是最高位(num/pow(10,n)%10)是否等于最低位(num/1%10) ③二分迭代 注:num为输入整数
代码如下:
2.Given a singly linked list, determine if it is a palindrome.
题目描述:给定一个单链表,判断其值是否是回文,要求为时间复杂度O(n),空间复杂度O(1)
知识点:链表翻转;链表中间结点;二分查找[思想];分和[思想]
思路:①查找链表的中间结点②翻转单链表后半部分③依次比较两个链表
代码如下:
3.Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
题目描述:给定一棵二叉树,进行层次遍历
知识点:二叉树的先序遍历;广度优先遍历(BFS)
思路:①先序遍历二叉树,同时②用队列保存其中的值③队列是否为空作为结束条件 注:leetcode题目当中并非简单输出其顺序,要用两个队列,严格按照分析的顺序执行出队入队操作,方可AC
代码如下:
二.总结
I.编程问题一般都有些小技巧,故不急跬步,无以至千里II.让我们一同努力,明天会更好!
1.1. 题目描述
1.2知识点及思路
1.3代码
总结
一.leetcode题目
1.Determine whether an integer is a palindrome. Do this without extra space.
题目描述:判断一个整数是否是回文,不能用额外的空间
知识点:数字最低与最高位取法;二分查找[思想]
思路:①求得整数位数n,②判断条件是最高位(num/pow(10,n)%10)是否等于最低位(num/1%10) ③二分迭代 注:num为输入整数
代码如下:
class Solution { public: bool isPalindrome(int x) { if (x < 0) return false; if (x < 10) return true; int digits = 0; int t = x; int d = 0; while(t != 0) t /= 10, ++d; int left = pow(10, d - 1); int right = 1; while( left >= right) { if (x / left % 10 != x / right % 10) return false; left /= 10; right *= 10; } return true; } };
2.Given a singly linked list, determine if it is a palindrome.
题目描述:给定一个单链表,判断其值是否是回文,要求为时间复杂度O(n),空间复杂度O(1)
知识点:链表翻转;链表中间结点;二分查找[思想];分和[思想]
思路:①查找链表的中间结点②翻转单链表后半部分③依次比较两个链表
代码如下:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { private: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; return reverse(head, NULL); } ListNode* reverse(ListNode* head, ListNode* add) { if(head == NULL) return add; ListNode* temp = head->next; head->next = add; return reverse(temp, head); } public: bool isPalindrome(ListNode* head) { if(!head) return true; if(!head->next) return true; ListNode *fast,*slow; fast=slow=head; while(fast&&fast->next) { fast=fast->next->next; slow=slow->next; } if(!fast) slow=reverseList(slow); else//结点为奇数 slow=reverseList(slow->next); ListNode*p=slow; ListNode*q=head; while(p) { if(p->val==q->val) { p=p->next; q=q->next; } else return false; } return true; } };
3.Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
题目描述:给定一棵二叉树,进行层次遍历
知识点:二叉树的先序遍历;广度优先遍历(BFS)
思路:①先序遍历二叉树,同时②用队列保存其中的值③队列是否为空作为结束条件 注:leetcode题目当中并非简单输出其顺序,要用两个队列,严格按照分析的顺序执行出队入队操作,方可AC
代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<int>temp; queue<TreeNode*>sto; vector<vector<int>>result; if(!root) return result; sto.push(root); TreeNode*tempNode=NULL; while(!sto.empty()) { temp.clear(); queue<TreeNode*>q2; while(!sto.empty()) { tempNode=sto.front(); sto.pop(); temp.push_back(tempNode->val); if(tempNode->left) q2.push(tempNode->left); if(tempNode->right) q2.push(tempNode->right); } result.push_back(temp); sto=q2; } return result; } };
二.总结
I.编程问题一般都有些小技巧,故不急跬步,无以至千里II.让我们一同努力,明天会更好!
相关文章推荐
- [MEMO] Linux Kernel Debugging Training 琐碎(补充ing)
- Loadrunner重要概念——参数化
- UVA 11300(p4)----Spreading the Wealth
- 高数试题与答案
- CSU 1646: HearthStone
- hdoj Clarke and MST 5627(求位运算and后得到的最大生成树)(并查集&位运算)好题
- Python 数据库备份脚本(邮件通知+日志记录)
- UVA 11292(p1)----The Dragon of Loowater
- 回声消除技术(1)
- 部分数学课件
- Eclipse汉化后怎么改回英文版(可切换中英文)
- 设计模式(java) 单例模式 单例类
- MSSQL数据库连接池
- Centos环境下MongoDB安装流程
- Loadrunner重要概念——事务
- 在spark环境中运行demo的时候报错
- 软件工程第一次作业
- <春游西湖>
- Eclipse调试Bug的七种常用技巧
- Windows环境下Android Studio v1.0安装教程