数据结构与算法
Wayne2019
这个作者很懒,什么都没留下…
展开
-
LeetCode: Search Insert Position, Count and Say, Maximum Subarray, Length of Last Word
Search Insert Position Given 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 dupli原创 2017-12-21 10:26:11 · 294 阅读 · 0 评论 -
LeetCode: 继续easy题9
Escape The Ghosts""""""class Solution {public: int distance(vector<int>& s1, vector<int>& s2){ return abs(s1[0]-s2[0])+abs(s1[1]-s2[1]); } bool escapeGho..原创 2018-02-27 06:10:19 · 264 阅读 · 0 评论 -
LeetCode: 继续easy题7
Reverse Vowels of a String """"""class Solution {public: bool isVowel(char a){ if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u') return true; if(a=='A'||a=='E'||a=='...原创 2018-02-16 04:33:48 · 322 阅读 · 0 评论 -
LeetCode: 继续easy题11
Find Mode in Binary Search Tree"""二叉树的递归,迭代中序遍历。Morris 遍历: https://www.zhihu.com/question/21556707"""Base 7""""""class Solution {public: string convertToBase7(int num) {原创 2018-03-16 04:50:30 · 436 阅读 · 0 评论 -
LeetCode: 继续easy题8
Nth Digit"""蛋疼,老Time Limit Exceeded,结果最后是两个long的地方越界"""class Solution {public: int findNthDigit(int n) { /* i=1: 1,...,9: 9 num_i i=2: 10,...,99: 90 ...原创 2018-02-25 12:35:12 · 315 阅读 · 0 评论 -
LeetCode: 继续easy题10
Assign Cookies"""简单"""class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()..原创 2018-03-12 08:40:45 · 331 阅读 · 0 评论 -
LeetCode: 继续easy题12
Subtree of Another Treeclass Solution {public: bool isSame(TreeNode* s, TreeNode* t){ if(!s && !t) return true; if(s && t) return (s-&g...原创 2018-03-21 06:41:22 · 348 阅读 · 1 评论 -
LeetCode: 13
Second Minimum Node In a Binary Tree/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), l...原创 2018-04-15 05:01:42 · 352 阅读 · 0 评论 -
LeetCode: 14
Count Binary Substrings"""简单,但是需要判断啥时候计数“清零”"""class Solution {public: int countBinarySubstrings(string s) { if(s.size()<=1) return 0; int zero = 0, one = 0, ...原创 2018-04-19 10:57:11 · 401 阅读 · 0 评论 -
LeetCode: 15 sum
Longest Substring Without Repeating Characters""""""class Solution {public: int lengthOfLongestSubstring(string s) { unordered_map<char,int> map; int i=0, j=0, ans=0, ...原创 2018-05-06 23:59:56 · 398 阅读 · 0 评论 -
LeetCode: 继续easy题6
Lowest Common Ancestor of a Binary Search Tree"""递归解法,想好边界条件,简单"""/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ...原创 2018-02-12 10:51:11 · 297 阅读 · 0 评论 -
LeetCode: 继续easy题5
Contains Duplicate"""简单O(n), O(n)"""class Solution {public: bool containsDuplicate(vector<int>& nums) { unordered_map<int,int> map; for (int i=0; i<nums...原创 2018-02-11 21:35:43 · 276 阅读 · 0 评论 -
LeetCode: Two Sum
cs231n推荐的starting point:optimizer = tf.train.AdamOptimizer( learning_rate=0.001, #or 5e-4 beta1=0.9,原创 2017-12-17 04:32:12 · 337 阅读 · 0 评论 -
LeetCode: 继续easy题
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each element appear only once."""9ms, beats 55.59%time O(n), space O(1)"""/** * Definition for sing原创 2017-12-30 22:54:26 · 346 阅读 · 0 评论 -
LeetCode: Reverse Integer,Palindrome Number
Reverse Integer Given a 32-bit signed integer, reverse digits of an integer."""1. 26 ms时间复杂度是O(log10(n))"""class Solution {public: int reverse(int x) { int result = 0; int new_原创 2017-12-17 06:24:58 · 327 阅读 · 0 评论 -
LeetCode: 继续easy题2
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root)."""基本想法是队列实现BF原创 2017-12-31 22:47:06 · 313 阅读 · 0 评论 -
LeetCode: Longest Common Prefix, Valid Parentheses
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.直观的想法是弄个while(true)循环,然后遍历array,每遍历一次取出来一个字符,直到取出来的字符不是共有的为止(后面会看到这种其实是最好的,避免了很多重复计算)。也可以不要每原创 2017-12-18 04:38:23 · 242 阅读 · 0 评论 -
LeetCode: Merge Two Sorted Lists, Remove Duplicates/Element, strStr()
Merge Two Sorted Lists/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} //C++11初始化列表 * }; */先自己想一下。同原创 2017-12-18 06:27:18 · 415 阅读 · 0 评论 -
LeetCode:Plus One, Add Binary, Sqrt(x), Climbing Stairs
Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to the integer."""3 ms, beats 7.49%time: O(n)space: O(n), 也可以改进到O(1), 比如位数变化时直接在vector开头插入一个1也行。"""c原创 2017-12-24 08:04:24 · 323 阅读 · 0 评论 -
LeetCode: 继续easy题3
Best Time to Buy and Sell Stock"""扫描,time n, space 19 ms, beats 10.54%"""class Solution {public: int maxProfit(vectorint>& prices) { int small_id = 0; int value = 0;原创 2018-01-05 01:30:43 · 500 阅读 · 0 评论 -
LeetCode: 继续easy题4
Rotate Array"""Cyclic Replacements,这个思路还是挺难写对的==O(n)"""class Solution {public: void rotate(vectorint>& nums, int k) { int current = 0; int next; int buffer, buffer_原创 2018-01-29 21:50:08 · 349 阅读 · 0 评论 -
LeetCode: 16 回溯
Letter Combinations of a Phone Number"""这种题就是DFS,递归+一堆传引用。迭代解法见http://www.cnblogs.com/grandyang/p/4452220.html,也可以用队列实现。"""class Solution {public: void helper(const string& digits, vecto...原创 2018-05-08 12:01:12 · 463 阅读 · 0 评论