![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 62
pantingting_
TT要好好学习~每天都要比昨天进步一丢丢
展开
-
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原创 2015-11-23 08:44:05 · 292 阅读 · 0 评论 -
LeetCode Remove Element
这里只要对于Vector比较熟悉就很好做了class Solution {public: int removeElement(vector& nums, int val) { vector::iterator it; for(it=nums.begin();it!=nums.end();){ if(*it==val){原创 2015-11-18 08:45:47 · 275 阅读 · 0 评论 -
LeetCode Move Zeroes
class Solution {public: void moveZeroes(vector& nums) { int i=0; int cnt=0; while(i<nums.size()-cnt){ if(nums[i]==0){ for(int j=i+1;j<nums.size原创 2015-11-18 09:57:38 · 354 阅读 · 0 评论 -
LeetCode Search Insert Position
class Solution {public: int searchInsert(vector& nums, int target) { int i; for(i=0;i<nums.size();i++){ if(nums[i]>=target){ break; }原创 2015-11-18 09:27:30 · 307 阅读 · 0 评论 -
LeetCode 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原创 2015-11-19 10:00:20 · 369 阅读 · 0 评论 -
Leetcode Swap Nodes in Pairs
递归做法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListN原创 2016-03-02 09:16:13 · 310 阅读 · 0 评论 -
LeetCode Insertion Sort List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* in原创 2016-03-02 10:51:23 · 362 阅读 · 0 评论 -
Leetcode Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the followin原创 2016-03-03 10:42:54 · 270 阅读 · 0 评论 -
LeetCode Power of Two
Given an integer, write a function to determine if it is a power of two.若一个数字是2的幂,那么它的二进制表示形式里面只有一个1,那么n-1刚好是除了为1的那位之外,其余所有位都为1的数字,举个例子,4的二进制为100,而3 的二进制为011,综上,如果一个数字n&(n-1)==0那么这个数字就是2的幂。class S原创 2016-03-03 13:36:15 · 332 阅读 · 0 评论 -
Leetcode House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2016-03-03 23:16:11 · 346 阅读 · 0 评论 -
LeetCode Binary Tree Right Side View
层次遍历我的想法:层序遍历,然后输出每一层最右边的节点代码如下:class Solution {public: vector rightSideView(TreeNode* root) { vector result; vector high; vector low; if(root==NULL) return re原创 2016-03-05 12:53:29 · 278 阅读 · 0 评论 -
LeetCode Merge Two Sorted Lists
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *ans=new ListNode(-1);//哨位节点,尾差; ListNode * tail=ans; while (l1&&l2) { i原创 2015-11-13 09:57:06 · 302 阅读 · 0 评论 -
LeetCode Roman to Integer
class Solution {public: int hash[256]; Solution(){ memset(hash, 0, sizeof(hash)); hash['I']=1; hash['V']=5; hash['X']=10; hash['L']=50; hash['原创 2015-11-13 09:23:32 · 323 阅读 · 0 评论 -
LeetCode Longest Palindromic Substring Part Manacher ALGORITHM
class Solution {public: string preProcess(string s){//避免回文串的奇偶性讨论 string ans=""; if (s.size()==0) { return "^$"; } ans.append("^"); for (int i=原创 2015-11-13 17:17:37 · 401 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array
这道题的Array是已经排序好的。用i,j两个指针,其中i指针以前的数据全部都保持不重复,j指针用于遍历原数组,j指针遇到与前一个数(即i指针所指向的数据)不同时即将数据添加到i指针末尾,并移动i指针。class Solution {public: int removeDuplicates(vector& nums){ int i=0; int j=1;原创 2015-06-27 09:45:05 · 254 阅读 · 0 评论 -
LeetCode ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2015-11-06 10:57:25 · 384 阅读 · 0 评论 -
LeetCode Two Sum
class Solution {public: vector twoSum(vector& nums, int target) { vectorans,tmp; tmp=nums; int first=0; int second=nums.size()-1; while ((nums[first]+nums[原创 2015-11-09 10:18:30 · 332 阅读 · 0 评论 -
LeetCode Palindrome Number
class Solution {public: char temp[1000];//暂存拆解出来的数字 string str1; string str2; bool isPalindrome(int x) { if (x<0) { return false; } if (x>0) {原创 2015-11-09 09:06:33 · 272 阅读 · 0 评论 -
LeetCode String to Integer (atoi)
String to Integer (atoi)class Solution {public: int myAtoi(string str) { long long ans=0; bool isNegtive=false; //丢弃前面的' ' int validIdx=0; for (int i原创 2015-11-10 10:35:23 · 274 阅读 · 0 评论 -
LeetCode Reverse Integer
class Solution {public: int reverse(int x) { long long ans=0; bool symbol=-1; if (x>0) { symbol=1; } while (x) { ans=ans*10+x%10;//原创 2015-11-10 14:59:22 · 279 阅读 · 0 评论 -
LeetCode Range Sum Query - Immutable
class NumArray {public: vector accu;//保存以i为结尾得子串的和(包含i)。public: NumArray(vector &nums) {// accu[0]=nums[0]; accu=nums; for (int i =1; i<nums.size(); i++) {原创 2015-11-11 17:33:57 · 326 阅读 · 0 评论 -
LeetCode Longest Substring Without Repeating Characters
class Solution {public: int hashtable[128]; int lengthOfLongestSubstring(string s) { int ans=0; memset(hashtable,0,sizeof(hashtable));//标记是否被访问过 for (int i=0; i<s.leng原创 2015-11-10 20:09:34 · 267 阅读 · 0 评论 -
LeetCode Add Two Numbers
//Definition for singly-linked list.struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };class Solution {public: ListNode* addTwoNumbers(Lis原创 2015-11-10 09:12:03 · 250 阅读 · 0 评论 -
LeetCode Longest Common Prefix
class Solution {public: string longestCommonPrefix(vector& strs) { if (strs.size()==0) { return ""; } string ans=strs[0]; for (int i=1; i<strs.size();原创 2015-11-13 09:46:00 · 268 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2016-03-05 22:17:19 · 354 阅读 · 0 评论