LeetCode
航xiao天
这个作者很懒,什么都没留下…
展开
-
LeetCode 21. Merge Two Sorted Lists 合并两个有序链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* me...转载 2018-08-26 10:48:32 · 81 阅读 · 0 评论 -
LeetCode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()<2) return 0; int profit=0; int cur_min=prices[0]; for(int i=...转载 2018-10-18 09:13:58 · 77 阅读 · 0 评论 -
LeetCode 70. Remove Duplicates from Sorted List 删除排序链表中的重复元素
class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if(head==nullptr) return nullptr; ListNode *p=head,*cur=head->next; while(cur) ...原创 2018-10-08 08:50:55 · 82 阅读 · 0 评论 -
LeetCode 136 Single Number 只出现一次的数字
解法一:class Solution {public: int singleNumber(vector<int>& nums) { int x=0; for(auto i:nums) x^=i; return x; ...原创 2018-10-15 08:52:26 · 104 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array 合并两个有序数组
class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { for(auto it=nums2.begin();it!=nums2.end();it++) { int i=...原创 2018-10-09 09:10:53 · 92 阅读 · 0 评论 -
LeetCode 169 Majority Element 求众数
解法一:哈希表class Solution {public: int majorityElement(vector<int>& nums) { int n=nums.size(); unordered_map<int,int> count; for(int i=0;i<n;i++) ...转载 2018-10-16 09:24:05 · 94 阅读 · 0 评论 -
LeetCode 100. Same 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...转载 2018-10-10 09:12:15 · 83 阅读 · 0 评论 -
LeetCode 121 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
class Solution {public: int maxProfit(vector<int>& prices) { int sum=0; for(int i=1;i<prices.size();i++) { int diff=prices[i]-prices[i-1]; ...转载 2018-10-19 09:21:47 · 73 阅读 · 0 评论 -
LeetCode 169 Excel Sheet Column Number Excel表列序号
26进制转10进制,注意以'A'而不是0开头,因此要“+1”解法一:class Solution {public: int titleToNumber(string s) { int n = s.size(); int ret=0; for(int i = n-1; i >=0; --i) ret +=...转载 2018-10-17 09:09:34 · 93 阅读 · 0 评论 -
LeetCode 804 Unique Morse Code Words 唯一摩尔斯密码词
class Solution {public: int uniqueMorseRepresentations(vector<string>& words) { string codes[26] = {".-","-...","-.-.","-..",".","..-.","--.","...转载 2018-10-22 15:57:13 · 114 阅读 · 0 评论 -
LeetCode 905 Sort Array By Parity 按奇偶排序数组
解法一:class Solution {public: vector<int> sortArrayByParity(vector<int>& A) { vector<int> ret; for(auto a:A) { if(a%2==0) ...原创 2018-10-22 16:19:06 · 100 阅读 · 0 评论 -
LeetCode 110 Balanced Binary Tree 平衡二叉树
解法一:class Solution {public: bool isBalanced(TreeNode* root) { return balanceHeight(root)>=0; } int balanceHeight(TreeNode* root) { if(root==nullptr) ...转载 2018-10-20 11:10:17 · 85 阅读 · 0 评论 -
LeetCode 461 Hamming Distance 汉明距离
解法一:class Solution {public: int hammingDistance(int x, int y) { int z=x^y; int count=0; while(z) { ++count; z=z&(z-1); } ...原创 2018-10-23 08:40:25 · 128 阅读 · 0 评论 -
LeetCode 657 Robot Return to Origin 机器人能否返回原点
解法一:class Solution {public: bool judgeCircle(string moves) { int l=0,r=0,u=0,d=0; for(int i=0;i<moves.size();++i) { if(moves[i]=='L') ++l;...原创 2018-10-23 08:52:02 · 279 阅读 · 0 评论 -
LeetCode 771 Jewels and Stones 宝石与石头
解法一:class Solution {public: int numJewelsInStones(string J, string S) { int count=0; for(int i=0;i<J.size();++i) for(int j=0;j<S.size();++j) { ...原创 2018-10-21 11:14:03 · 94 阅读 · 0 评论 -
LeetCode 760 Find Anagram Mappings 查找字谜映射
Giventwo lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing theorder of the elements in A.Wewant to find an index mapping P, from A to B. A mapping P[i] = ...转载 2018-10-21 11:34:40 · 131 阅读 · 0 评论 -
LeetCode 709 To Lower Case 转换成小写字母
class Solution {public: string toLowerCase(string str) { for(auto &c:str) { if(c>='A' && c<='Z') c+=32; } return str;...转载 2018-10-21 11:59:04 · 104 阅读 · 0 评论 -
LeetCode 832 Flipping an Image 翻转图像
解法一:class Solution {public: vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) { int temp; for(int i=0;i<A.size();++i) { ...原创 2018-10-24 09:09:23 · 140 阅读 · 0 评论 -
LeetCode Convert Sorted Array to Binary Search 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...转载 2018-10-14 10:57:35 · 106 阅读 · 0 评论 -
LeetCode 107. Binary Tree Level Order Traversal II 二叉树的层次遍历 II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */解法一...转载 2018-10-13 23:06:27 · 68 阅读 · 0 评论 -
LeetCode 70. Climbing Stairs 爬楼梯
解法一:class Solution {public: int climbStairs(int n) { int cur=1,prev=0; for(int i=1;i<=n;++i) { int tmp=cur; cur+=prev; prev=tmp; ...转载 2018-09-28 11:07:19 · 80 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array 删除排序数组中的重复项
class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.empty()) return 0; int n=1; for(int i=1;i<nums.size();i++) { ...原创 2018-08-27 09:51:24 · 75 阅读 · 0 评论 -
LeetCode 9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExpl...转载 2018-08-23 09:36:24 · 94 阅读 · 0 评论 -
LeetCode 7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dea...转载 2018-08-23 09:42:40 · 143 阅读 · 0 评论 -
LeetCode 1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ...原创 2018-08-23 09:47:43 · 65 阅读 · 0 评论 -
LeetCode13 Roman to Integer罗马数字转整数
罗马数字包含以下七种字符:I, V, X, L,C,D 和 M。字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1。...转载 2018-08-23 09:16:50 · 101 阅读 · 0 评论 -
LeetCode 27. Remove Element 移除元素
class Solution {public: int removeElement(vector<int>& nums, int val) { int sum=0; for(auto it=nums.begin();it!=nums.end();it++) { if(*it==val) ...原创 2018-08-28 10:54:53 · 110 阅读 · 0 评论 -
LeetCode 38. Count and Say 报数
解法一:string countAndSay(int n) {if (n == 0) return "";string res = "1";while (--n) {string cur = "";for (int i = 0; i < res.size(); i++) {int count = 1;while ((i + 1 < res.size())...转载 2018-08-31 10:30:53 · 102 阅读 · 0 评论 -
LeetCode 14. Longest Common Prefix最长公共前缀
解法一:纵向比较class Solution {public: string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return ""; for(int idex=0;idex<strs[0].size();idex++...转载 2018-08-24 10:13:24 · 94 阅读 · 0 评论 -
LeetCode 28. Implement strStr() 实现strStr()
class Solution {public: int strStr(string haystack, string needle) { if(needle=="") return 0; int j=0,n=-1; bool ISstr; for(int i=0;i<haystack.size(...原创 2018-08-29 09:23:24 · 73 阅读 · 0 评论 -
LeetCode 20. Valid Parentheses 有效的括号
解法一:class Solution {public: bool isValid(string s) { if(s=="") return true; stack<char> str; string left="'(','{','['"; string right="')','}',...转载 2018-08-25 10:30:54 · 74 阅读 · 0 评论 -
LeetCode 35. Search Insert Position 搜索插入位置
解法一:class Solution {public: int searchInsert(vector<int>& nums, int target) { int i=0; for(;i<nums.size();i++) { if(target==nums[i]) ...原创 2018-08-30 09:35:51 · 81 阅读 · 0 评论 -
LeetCode 58. Length of Last Word 最后一个单词的长度
解法一:class Solution {public: int lengthOfLastWord(string s) { return LengthLastWorld(s.c_str()); } int LengthLastWorld(const char* s) { int length=0; while(*s...转载 2018-09-25 09:59:48 · 104 阅读 · 0 评论 -
LeetCode 66. Plus One 加一
class Solution {public: vector<int> plusOne(vector<int>& digits) { add(digits,1); return digits; } void add(vector<int> &digits,int c) {...转载 2018-09-26 09:37:00 · 72 阅读 · 0 评论 -
LeetCode 53.Maximum Subarray 最大子序和
解法一:class Solution {public: int maxSubArray(vector<int>& nums) { int Max=nums[0]; int sum=0; for(int i=0;i<nums.size();i++) { sum=nums[...原创 2018-09-21 10:46:03 · 88 阅读 · 0 评论 -
LeetCode 67. Add Binary 二进制求和
解法一:class Solution {public: string addBinary(string a, string b) { string c; const size_t n=a.size()>b.size()?a.size():b.size(); reverse(a.begin(),a.end()); re...转载 2018-09-27 09:37:00 · 125 阅读 · 0 评论 -
LeetCode 100. Maximum Depth of 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...转载 2018-10-12 09:15:33 · 75 阅读 · 0 评论 -
LeetCode 69. Sqrt(x) x 的平方根
解法一:class Solution {public: int mySqrt(int x) { int left=1,right=x/2,last_mid; if(x<2) return x; while(left<=right) { const int mid...转载 2018-09-27 23:23:57 · 75 阅读 · 0 评论 -
LeetCode 617 Merge Two Binary Trees 合并二叉树
class Solution {public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { if(t1 && t2) { TreeNode* root=new TreeNode(t1->val+t2->val); ro...转载 2018-10-24 10:20:44 · 153 阅读 · 0 评论