![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
cc的leetcode
文章平均质量分 76
echen227
这个作者很懒,什么都没留下…
展开
-
#leetcode#104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.遍历树,深度优先遍历/广度优先遍历深度优先遍原创 2016-03-20 17:42:39 · 242 阅读 · 0 评论 -
#leetcode#198. 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-04-27 16:58:50 · 208 阅读 · 0 评论 -
#leetcode#338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5原创 2016-04-11 21:47:31 · 205 阅读 · 0 评论 -
#leetcode#24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2016-04-28 21:00:11 · 223 阅读 · 0 评论 -
#leetcode#110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2016-04-28 21:27:41 · 183 阅读 · 0 评论 -
#leetcode#232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2016-04-28 21:49:54 · 205 阅读 · 0 评论 -
#leetcode#168 171 Excel Sheet Column Title 与 Excel Sheet Column Number
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 26进制转化原创 2016-04-15 21:22:11 · 195 阅读 · 0 评论 -
#leetcode#169Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2016-04-15 22:06:08 · 184 阅读 · 0 评论 -
#leetcode#217 Contains Duplicate (map&排序)
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2016-04-18 20:47:38 · 228 阅读 · 0 评论 -
#leetcode#206. Reverse Linked List
单链表翻转(递归和迭代两种)迭代/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {原创 2016-04-19 23:09:35 · 164 阅读 · 0 评论 -
#leetcode#235 Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2016-04-23 20:08:23 · 176 阅读 · 0 评论 -
#leetcode#191 Number of 1 Bits n&n-1的用处
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2016-04-23 20:32:51 · 257 阅读 · 0 评论 -
#leetcode#83 Remove Duplicates from Sorted List
Given 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.两个指针,注意细节,先判断head==NULL原创 2016-04-23 20:47:52 · 227 阅读 · 0 评论 -
#leetcode# 344 Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".建立两个指针,一个在头一个在尾交换两个指针的数值,之后一个++,一个--class Solution {public: s原创 2016-04-23 20:58:42 · 263 阅读 · 0 评论 -
#leetcode#21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.按顺序合并两个链表注意链表=初始化以及返回的问题/** * Definition f原创 2016-04-26 20:54:19 · 232 阅读 · 0 评论 -
#leetcode#121. 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-04-26 20:25:57 · 161 阅读 · 0 评论 -
#leetcode#226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1遍历方法/非遍历方法/** * Definition for a binary tree node. * struct TreeNod原创 2016-03-31 20:58:43 · 174 阅读 · 0 评论 -
#leetcode#283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2016-04-05 20:52:12 · 157 阅读 · 0 评论 -
#leetcode#292 Nim Game
class Solution {public: bool canWinNim(int n) { return n % 4 != 0; }};寻找规律,简化代码return n & 0x3?????原创 2016-03-20 16:30:43 · 165 阅读 · 0 评论 -
#leetcode#258 Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2016-03-20 16:49:38 · 170 阅读 · 0 评论 -
#leetcode#237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2016-04-07 21:05:52 · 164 阅读 · 0 评论 -
100. Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.判断树是原创 2016-04-07 21:22:54 · 174 阅读 · 0 评论 -
#leetcode#242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.1,排序后比较2原创 2016-04-07 21:50:15 · 160 阅读 · 0 评论 -
开始leetcode吧,加油
开始吧,看看自己能做到多少为了 ,为了原创 2016-03-20 15:51:20 · 240 阅读 · 0 评论 -
#leetcode#263.Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2016-04-25 18:31:08 · 165 阅读 · 0 评论 -
#leetcode#345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.Example 1:Given s = "hello", return "holle".Example 2:Given s = "leetcode", return "leotcede".只翻转元原创 2016-04-25 19:27:34 · 402 阅读 · 0 评论 -
#leetcode#326. Power of Three
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?求3的幂思路1,迭代,不断除3,能否除尽思路2,原创 2016-04-25 20:57:45 · 213 阅读 · 0 评论 -
#leetcode#202. Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2016-04-25 21:33:18 · 171 阅读 · 0 评论 -
#leetcode#231. Power of Two
Given an integer, write a function to determine if it is a power of two.2的幂n&n-1,消掉最低位的1看是否为0class Solution {public: bool isPowerOfTwo(int n) { return ((n>0) && (n&(n-1))==0);原创 2016-04-26 19:56:43 · 177 阅读 · 0 评论 -
#leetcode#70.Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?斐波那契数列,计算迭代f(n)=f(n-1)原创 2016-04-23 21:09:26 · 207 阅读 · 0 评论