leetcode AC rates over35% 从高到低
来源:互联网 发布:android经典项目源码 编辑:程序博客网 时间:2024/05/22 03:10
Best Time to Buy and Sell Stock II
Total Accepted: 22240 Total Submissions: 60488My SubmissionsSay you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int profit = 0; for(int i = 1; i < prices.size();i++) { if(prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1]; } return profit; }};
2.Unique Binary Search Trees
Total Accepted: 23078 Total Submissions: 63359My SubmissionsGiven n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
//参考别人的class Solution {public: int numTrees(int n) { vector<int> v(n+1,0); v[0] = 1; v[1] = 1; v[2] = 2; if(n <= 1) return 1; else if(n == 2) return 2; for(int i = 3;i <= n; i++) { for(int j = 1; j <= i; j++) { v[i] += v[j - 1] * v[i - j]; } } return v[n]; }};
3.Linked List Cycle
Total Accepted: 29113 Total Submissions: 81160My SubmissionsGiven 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; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { ListNode *p = head,*q = head; while(q!= NULL && q->next != NULL) { p = p->next; q = q->next->next; if(p == q) return true; } return false; }};
4.Binary Tree Inorder Traversal
Total Accepted: 29287 Total Submissions: 82207My SubmissionsGiven a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
class Solution {public:void inorder(TreeNode *root,vector<int>&result){if(root == NULL)return;inorder(root->left,result);result.push_back(root->val);inorder(root->right,result);} vector<int> inorderTraversal(TreeNode *root) {vector<int> result;inorder(root,result);return result; }};
5.Binary Tree Preorder Traversal
Total Accepted: 32064 Total Submissions: 90031My SubmissionsGiven a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
class Solution {public: vector<int> preorderTraversal(TreeNode *root) { vector<int>result; result.clear(); stack<TreeNode*>st; if(root == NULL) return result; st.push(root); while(!st.empty()) { TreeNode *node = st.top(); st.pop(); result.push_back(node->val); if(NULL != node->right) { st.push(node->right); } if(NULL != node->left) { st.push(node->left); } }return result; }};
class Solution {public:void preorder(TreeNode *root,vector<int> &result){if(root == NULL)return;result.push_back(root->val);preorder(root->left,result);preorder(root->right,result);} vector<int> preorderTraversal(TreeNode *root) { vector<int>result;preorder(root,result);return result; }};
6.Populating Next Right Pointers in Each Node
Total Accepted: 23205 Total Submissions: 65706My SubmissionsGiven a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
/** * 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; TreeLinkNode *left = root->left; TreeLinkNode *right = root->right; if(NULL != left) { left->next = right; } if(NULL !=right && NULL !=root->next) { right->next = root->next->left; } connect(left); connect(right); }};
7.Search Insert Position
Total Accepted: 24348 Total Submissions: 69507My SubmissionsGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4[1,3,5,6]
, 0 → 0
class Solution {public: int searchInsert(int A[], int n, int target) { if(n == 0) return 0; int low = 0; int high = n - 1; int mid; while(low <= high) { mid = (low+high)/2; if(A[mid] == target) return mid; else if(A[mid] < target) { low = mid+1; } else { high = mid - 1; } } if(A[mid] > target) return mid; else return mid + 1; }};
8.Remove Duplicates from Sorted List
Total Accepted: 25230 Total Submissions: 72036My SubmissionsGiven a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
Discuss
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution { public: ListNode *deleteDuplicates(ListNode *head) { // Start typing your C/C++ solution below // DO NOT write int main() function if (head == NULL) return head; ListNode* cur = head; ListNode* pre = head->next; ListNode* del = NULL; while(pre != NULL) { if (cur->val == pre->val) { del = pre; pre = pre->next; cur->next = pre; delete del; } else { cur = cur->next; pre = pre->next; } } return head; } };
- leetcode AC rates over35% 从高到低
- leetcode AC rates over 40%按从高到低
- LeetCode 56, Merge Intervals 从 TLE 到 AC
- Exchange Rates
- LeetCode AC代码
- 每日AC--leetcode--recordList
- 每日AC--LeetCode-Candy
- 从kmp到ac自动机
- 每日AC--gas-station--LeetCode
- PKU 3639 Exchange Rates
- 2504: Exchange Rates
- 802.11 Supported Basic Rates
- Cisco AP Data Rates
- OpenJudge_P2421 Exchange Rates(DP)
- Data rates using BLE
- 从KMP算法,trie树再到AC自动机
- 从今天开始,我将真正全心ac
- pku3639 Exchange Rates (动态规划)
- UVa12532 - Interval Product(Fenwick树)
- c++ primer 第五版学习笔记-第二章-类型转换
- Selector
- 开源夏令营《基于HackRF开发GPS信号仿真模拟器》终期汇报
- 互联网面试题
- leetcode AC rates over35% 从高到低
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
- Apache FTPServer配置问题总结
- 0801安卓就业班毕业后
- 【HDU】4991 Ordered Subsequence 线段树&树状数组
- Access中修改字段类型SQL语句
- linux学习笔记(2)
- cf 464 Restore Cube
- js 数据类型及常用函数