![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
文章平均质量分 65
daniren
这个作者很懒,什么都没留下…
展开
-
[Leetcode]1. Two Sum
快速的查找所以使用hash表的思想,利用map结构实现~class Solution {public: vector twoSum(vector& nums, int target) { map table; vector solution; int res; size_t i=0; bo原创 2016-10-09 16:19:44 · 162 阅读 · 0 评论 -
[Leetcode]First Bad Version
// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: int firstBadVersion(int n) { return binarySearch(1,n); } int binarySearch原创 2016-10-17 22:09:21 · 294 阅读 · 0 评论 -
[Leetcode] Path Sum
在遍历树的同时计算是否到达叶子节点,如果到达叶子节点时,pathsum还不等于sum则跳出。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(in原创 2016-10-03 21:52:32 · 216 阅读 · 0 评论 -
[Leetcode] Bulls and Cows
class Solution {public: string getHint(string secret, string guess) { int m[256] = {0}, bulls = 0, cows = 0; for (int i = 0; i < secret.size(); ++i) { if (secret[i] ==转载 2016-10-03 22:20:09 · 204 阅读 · 0 评论 -
[Leetcode] Minimum Depth of Binary Tree
本题目我自己编写了一个代码,但是运行速度很慢,看了别人的代买才知道,有些树的递归逻辑的判断并不需要一定在本次执行,如果函数内部写了异常处理即树root为空,则对叶子节点的判断可以由自身的返回值判断,这样就会大大减少代码的体积。现贴下我自己和他人的代码以供学习class Solution {public: int minDepth(TreeNode* root) {原创 2016-10-04 10:17:58 · 235 阅读 · 0 评论 -
[Leetcode]Isomorphic Strings
我的做法时建立映射,感觉太慢,百度了一下发下,这道题的本质在于寻找字符的pattern,转换成数字果然好办class Solution { public: string transferStr(string s){ char table[128] = {0}; char tmp = '0'; for (int i=0转载 2016-10-04 11:11:37 · 160 阅读 · 0 评论 -
[Leetcode] Remove Nth Node From End of List
利用快慢指针的方法进行,先让快指针走n步,本体里注明了n一定有效,所以不需要对n的有效性进行检查,若需检查请参考二,需要注意的时链表删除的时候需要注意头指针的处理问题class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(!head) return nullp原创 2016-10-05 15:15:17 · 179 阅读 · 0 评论 -
merged sorted array
class Solution {public: void merge(vector& nums1, int m, vector& nums2, int n) { auto i = nums1.begin(); auto j = nums2.begin(); while(i != nums1.end()&&j != nums2.end()){原创 2016-10-06 22:38:29 · 206 阅读 · 0 评论 -
[leetcode] 400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...Note:n is positive and will fit within the range of a 32-bit signed integer (n 31).Example 1:Input原创 2016-10-07 15:17:54 · 285 阅读 · 0 评论 -
[Leetcode] Binary Tree Paths
二叉树路径查找,利用递归的方法查找,我的方法比较慢,因为需要先遍历到叶子节点再返回路径,如果在遍历的同时生成路径就会快很多/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * Tree原创 2016-10-02 11:12:09 · 264 阅读 · 0 评论 -
[Leetcode]Palindrome Number
class Solution {public: bool isPalindrome(int x) { if(x<0) return false; if(x<10) return true; int n = 0; int y = x; while(y){ n++;原创 2016-09-30 11:28:01 · 174 阅读 · 0 评论 -
[Leetcode] Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.class Solution {public: string longestCommonPrefix(vector& strs) { if(strs.empty()) r原创 2016-10-09 16:34:28 · 238 阅读 · 0 评论 -
[Leetcode]234. Palindrome Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool isPalin原创 2016-10-09 17:25:18 · 175 阅读 · 0 评论 -
[leetcode]Implement strStr()
字符串匹配的KMP算法class Solution {public: int strStr(string haystack, string needle) { int hlen = haystack.size(); int nlen = needle.size(); int next[nlen]; get原创 2016-10-13 16:59:26 · 181 阅读 · 0 评论 -
[Leetcode] Binary Tree Level Order Traversal II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2016-09-28 12:17:33 · 191 阅读 · 0 评论 -
[Leetcode] Balanced Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2016-09-28 20:56:30 · 158 阅读 · 0 评论 -
[Leetcode] Remove Duplicates from Sorted Array
class Solution {public: int removeDuplicates(vector& nums) { if(nums.size() == 0) return 0; stack s; sort(nums.begin(),nums.end()); s.push(nums[0]); for(au原创 2016-09-29 10:49:18 · 197 阅读 · 0 评论 -
Implement Queue using Stacks
用两个栈来操作,t存储队列的逆序,每次加入新元素就倒序使用sclass Queue {public: // Push element x to the back of queue. void push(int x) { stack s; while(!t.empty()) { s.push(t.top());原创 2016-09-29 11:19:24 · 253 阅读 · 0 评论 -
[Leetcode] Pascal's Triangle II
Pascal's Triangle IIGiven an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k)原创 2016-09-30 11:06:32 · 153 阅读 · 0 评论 -
[Leetcode]Combinations
最近学习暴力枚举法的相关知识,首先学习的是增量构造法,该方法的精髓在于首先获取原始资料的特征,是否重复,重复后各个数字出现的次数等,然后再按照一定的构造方法去进行深度搜索,其实也就是在决定每一个数据是否出现在所构成的排列中~收敛条件是达到了排列所需的个数class Solution {public: vector> combine(int n, int k) {原创 2016-12-02 12:59:37 · 203 阅读 · 0 评论