Populating Next Right Pointers in Each Node
来源:互联网 发布:vr的前景 知乎 编辑:程序博客网 时间:2024/06/07 10:17
Populating Next Right Pointers in Each Node
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */class Solution {public: void connect(TreeLinkNode *root) { if(root == NULL) return;queue<TreeLinkNode* > myqueue;TreeLinkNode *p = root;myqueue.push(root);int pre = 1, cur =0;while(!myqueue.empty()){p = myqueue.front();myqueue.pop();-- pre;if(p->left != NULL){myqueue.push(p->left);++ cur;}if(p->right != NULL){myqueue.push(p->right);++ cur;}if(pre == 0){p->next = NULL;pre = cur;cur = 0; }else{p->next = myqueue.front();}} }};
Search Insert Position
class Solution {public: int searchInsert(int A[], int n, int target) { if(target < A[0]) return 0;if(target > A[n-1]) return n;int left = 0, right = n-1,mid = 0;while(left <= right){mid = (right+left)/2;if(A[mid] == target) return mid;else if(A[mid] > target) right = mid-1;else left = mid+1;}return left; }};
Remove Duplicates from Sorted List O(N)
1->1->2
, return 1->2
.Given
1->1->2->3->3
, return 1->2->3
.ListNode *deleteDuplicates(ListNode *head) {if(head == NULL || head->next == NULL) return head;ListNode* p= head;ListNode* q= head->next; int pre = head->val;while(true ){//1. 当前q值与前一个值不同重新设置新的开始点for(;q!=NULL && q->val != pre;q = q->next) {p = q;pre = q->val;}//1.1 q往后都不同,则推出if(q == NULL)break;//2. 第一个相同的是q,计算q往后相同的for(;(q->next != NULL) && (q->next->val == pre); q = q->next);//q相同的节点移动,不过q->next才是不同的节点,或者是走到了最后//2.1 q走到最后了,q后面的节点都相同结束if(q->next == NULL){p->next = NULL;break;}//2.2 q->next是不同的节点,将不同点与p连接,并且重新置q点p->next = q->next;q->next = NULL;//2.2.1 注意需要p是q的前一个节点,所以两者是一起改变的,一起指向新的节点if(p->next->next != NULL){p = p->next;pre = p->val;q = p->next;}//2.2.2 不同的节点是最后一个节点elsebreak;}return head;}
Given a roman numeral, convert it to an integer.O(N)
int romanToInt(string s) {map<char, int> rmap;rmap['I'] = 1;rmap['V'] = 5;rmap['X'] = 10;rmap['L'] = 50;rmap['C'] = 100;rmap['D'] = 500;rmap['M'] = 1000;if(s.length() == 0) return 0;if(s.length() == 1) return rmap.find(s[0])->second;int i = s.length()-1;//从后往前读取,大是加上,小于是减掉int pre = rmap.find(s[i--])->second;int next = 0;int num = pre;//看来真正的规则就是简单的依次相邻的判断,没有想象的那么难到底是取几个字母一组,因为罗马字母本来也是有规律的//不太可能是乱排的while(i>=0){next = rmap.find(s[i])->second;if(pre <= next)num += next;elsenum -= next;pre = next;-- i;}return num;}
Given an integer, convert it to a roman numeral. O(N)
class Solution {public: string intToRoman(int num) {if(num <= 0) return "";map<int, string> rmap;rmap[1] = "I";rmap[5] = "V";rmap[10] = "X";rmap[50] = "L";rmap[100] = "C";rmap[500] = "D";rmap[1000] = "M";string s = "";int count = 0;int i = 0;int small = 0;while(num != 0){count = num%10;//个位数int key1 = pow(10,i);//如果是9,左1右10if(count == 9){s = rmap[key1]+rmap[10*key1]+s;}else if (count >= 5)//5-8,5左1右{small = count -5;for(;small >0 ;-- small)s = rmap[key1]+s;s = rmap[5*key1]+s;}else if(count == 4)//4,1左5右{s = rmap[key1]+rmap[5*key1]+s;}else{for(;count >0 ;-- count)s = rmap[key1]+s;}num = num/10;++ i;}return s; }};
Remove Element
O(N)My SubmissionsGiven an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
class Solution {public: int removeElement(int A[], int n, int elem) { if(n <= 0)return 0;int i = 0, j=0,k=0;set<int > indexset;for(;i < n; ++ i){if(A[i] == elem)indexset.insert(i);}if(indexset.size() == 0) return n;if(indexset.size() == n) return 0;//如果全是一样的i=0;//找到第一个相同的点;while(i<n && !indexset.count(i))++ i;//如果相同的点就是最后一个点if(i == (n-1))return n-1;//往后找到第一个不同的点;j = i+1;while(j<n && indexset.count(j))++ j;while(j < n){A[i++] = A[j++];while(indexset.count(j))++ j;//往后找到第一个不同的点;}return i; }};
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
class Solution {public: int maxSubArray(int A[], int n) {if(n<=0) return 0;if(n == 1) return A[0];int max = A[0], sum = 0, i = 0;while(i < n){sum += A[i];if(sum > max)max = sum;if(sum < 0){++ i;sum = 0;}else++ i;}return max; }};
Climbing stairs
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
因为f[n] = f[n-1] + f[n-2] fibonacci数列
class Solution {public: int climbStairs(int n) { if(n == 0)return 0;if(n == 1 || n == 2) return n;int* f =new int[n+1];f[0] = 1;f[1] = 1;for(int i =2; i <= n;++i){f[i] = f[i-2] + f[i-1];} return f[n]; }};
0 0
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- C# System.DBNull string
- vc2013 atomic_int and atomic<int> bug
- kendoui仪表盘和柱状图 示例
- 适合2015年开发的10个新锐框架(上)
- 图论基础
- Populating Next Right Pointers in Each Node
- android.os.SystemProperties反射帮助类
- 莆籍北漂青年刘永宗在京举行个人作品签售会(图)
- 【bzoj 2330】 糖果 candy
- Swift初体验
- Microsoft Office Project 2010 操作手册
- nodejs中exports与module.exports的区别
- POJ1466 Girls and Boys
- 利用 squid 反向代理提高网站性能