C++
lyh_xd
一匹正在努力提高算法、编程水平的机器学习马
展开
-
Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.原创 2016-05-29 15:34:03 · 207 阅读 · 0 评论 -
Leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for a binary tree node.原创 2016-05-29 17:11:37 · 219 阅读 · 0 评论 -
Leetcode 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.原创 2016-05-29 20:46:11 · 239 阅读 · 0 评论 -
Leetcode 290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2016-05-30 16:10:17 · 339 阅读 · 0 评论 -
Leetcode 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, the原创 2016-05-30 23:30:42 · 203 阅读 · 0 评论 -
Leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list be原创 2016-09-01 10:57:29 · 207 阅读 · 0 评论 -
Leetcode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as原创 2016-09-01 11:39:10 · 215 阅读 · 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.原创 2016-05-23 20:41:06 · 240 阅读 · 0 评论 -
Leetcode 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-05-23 21:33:49 · 192 阅读 · 0 评论 -
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-05-24 09:04:59 · 210 阅读 · 0 评论 -
Leetcode 226. Invert Binary Tree
Invert a binary tree. 题目意思是将二叉树每层的左右孩子交换。原创 2016-05-24 14:30:42 · 222 阅读 · 0 评论 -
Leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].原创 2016-05-24 15:21:43 · 249 阅读 · 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 3,原创 2016-05-24 20:23:10 · 186 阅读 · 0 评论 -
Leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space? 判断链表是否有环原创 2016-05-25 11:12:41 · 204 阅读 · 0 评论 -
Leetcode 121. Best Time to Buy and Sell Stock
股票利益最大化问题原创 2016-05-25 17:32:38 · 204 阅读 · 0 评论 -
Leetcode 206. Reverse Linked List
Reverse a singly linked list.原创 2016-05-26 09:23:33 · 193 阅读 · 0 评论 -
Leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric:1/ \ 2 2 / \ / \ 3 4 4 3 But the following is not:原创 2016-05-27 11:50:24 · 224 阅读 · 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 between two原创 2016-05-26 16:11:00 · 228 阅读 · 0 评论 -
Leetcode 189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].题目要求讲一个数组循环右移k位。方法一:严格按照右移定义。可按k和(1/2)n的相对大小设计左移和右移,但是时间原创 2016-06-06 22:40:23 · 239 阅读 · 0 评论 -
C++ map的基本操作和使用
map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。使用时要加入头文件 #include < map >。map的几个主要特点自动建立Key - value的对应。key 和 value可以是任意你需要的类型。根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录原创 2016-03-25 15:46:23 · 457 阅读 · 0 评论 -
c++ set 常用用法介绍
set 集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子数根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。平衡二叉检索树使用中序遍历算法,检索效率高于 vector、deque 和 list 等容器,另外使用了中序遍历可将键值按照从小到大遍历出来。构造 set 集转载 2016-04-08 22:22:28 · 318 阅读 · 0 评论 -
层次打印二叉树
有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。 给定二叉树的根结点root,请返回打印结果,结果按照每一层一个数组进行储存,所有数组的顺序按照层数从上往下,且每一层的数组内元素按照从左往右排列。原创 2016-04-26 11:16:19 · 507 阅读 · 0 评论 -
C++ STL -- stack / queue / priority_queue 的使用方法
stackstack 模板类的定义在 < stack >头文件中。 stack 模板类需要两个模板参数,一个是元素类型,一个是容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型为 deque。转载 2016-04-26 12:25:24 · 345 阅读 · 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”.原创 2016-05-24 20:37:08 · 248 阅读 · 0 评论 -
Leetcode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].原创 2016-05-24 23:18:44 · 644 阅读 · 0 评论 -
Leetcode 171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28原创 2016-05-25 09:03:14 · 189 阅读 · 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-05-26 16:47:51 · 356 阅读 · 0 评论 -
Leetcode 107. 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).原创 2016-05-26 22:45:30 · 188 阅读 · 0 评论 -
Leetcode 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.原创 2016-05-29 11:22:02 · 201 阅读 · 0 评论 -
Leetcode 88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional原创 2016-05-29 21:19:10 · 177 阅读 · 0 评论 -
Leetcode 299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that原创 2016-05-29 22:06:04 · 192 阅读 · 0 评论 -
C++中的 istringstream 的用法
转载自:http://blog.sina.com.cn/s/blog_a9303fd90101adt6.html今天看到了一个比较有用的c++的输入输出控制类。 C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象要包含< sstream >这个头文件。转载 2016-05-30 16:20:07 · 247 阅读 · 0 评论 -
Leetcode 204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n.原创 2016-05-30 17:07:54 · 189 阅读 · 0 评论 -
Leetcode 125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not a palin原创 2016-06-07 15:33:57 · 248 阅读 · 0 评论 -
Leetcode 217. Contains Duplicate
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 is distinct原创 2016-05-23 21:22:23 · 213 阅读 · 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.原创 2016-05-26 15:47:31 · 226 阅读 · 0 评论