自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

永远不要给自己找借口

再NB的梦想也敌不过SB似的坚持

  • 博客(41)
  • 收藏
  • 关注

原创 Visual Studio 2013中strcpy函数报错

在VS2013中,某些C库的函数如strcpy()不进行参数检测,微软担心使用这些会造成异常,就改写了同样功能的函数。

2016-10-25 14:42:28 5169 1

原创 leetcode 414. Third Maximum Number

解题思路: 首先定义一个数组top3[3] = {INT_MIN, INT_MIN, INT_MIN},由左至右,分别存储第一大,第二大,第三大元素 然后,遍历数组,对于每一个数,找到其在top3中的位置。 以下代码中,shiftCount表示能够加入到top3[3]数组中元素的个数。开始时,top3中没有数组中的元素,第一个加入进去,shiftCount=1, 如果出现一个与第一个不同的数,

2016-10-24 19:21:39 486

原创 leetcode 396. Rotate Function

解题思路:对于数组A[4, 3, 2, 6],以及其系数0,1, 2, 3。数组A向右旋转,等同于其系数向左旋转0 1 2 3 F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 251 2 3 0 F(1) = (1 * 4) + (2 * 3) + (3 * 2) + (0 * 6) = 4 + 6 + 6 +

2016-10-24 15:02:58 247

原创 leetcode 400. Nth Digit

解题思路: 如果n<9, 返回n 如果n<9 + 2 * 90, 说明是某个两位数中的一位 如果n<9 + 2 * 90 + 3 * 900,说明是某个三位数中的一位 …首先判断第n个数是属于几位数,然后判断属于该数的第几位注意 表达式的结果可能溢出,导致结果错误 原题目:Find the nth digit of the infinite integer sequence 1, 2, 3

2016-10-24 14:44:46 453

原创 leetcode 160. Intersection of Two Linked Lists

解题思路:注意是节点相同,而不是节点的值相同。对于两个链表A: a1→a2→c1→c2→c3B: b1→b2→b3→c1→c2→c3由于长度不同,所以同时遍历无法对齐A: a1→a2→c1→c2→c3 b1→b2→b3→**c1→c2→c3**B: b1→b2→b3→c1→c2→c3 a1→a2→**c1→c2→c3**经过这样的遍历,可以使相同的节点进行对齐原题目:Write a

2016-10-24 14:37:40 209

原创 leetcode 290. Word Pattern

解题思路: 同时遍历pattern和str,观察字符与对应单词的数量是否一直保持一致。 pattern的字母数量与Word的单词数量不一致时,返回false原题目:Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that ther

2016-10-24 14:29:03 241

原创 leetcode 58. Length of Last Word

解题思路: 如果字符串不为空,就返回最后一个单词的长度,如果字符串为空就返回0原题目: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 no

2016-10-24 14:25:24 225

原创 leetcode 234. Palindrome Linked List

解题思路: 首先计算链表长度,然后将链表前半部分利用头插法逆序,与后半部分进行比较原题目:Given a singly linked list, determine if it is a palindrome.AC解,C++代码,菜鸟一个,请大家多多指正/** * Definition for singly-linked list. * struct ListNode { * int

2016-10-24 14:22:30 206

原创 leetcode 223. Rectangle Area

解题思路: 关键在于对重复区域面积的计算原题目:Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume t

2016-10-24 14:19:07 262

原创 leetcode 299. Bulls and Cows

解题思路: 首先同时遍历一遍secret和guess,计算出Bulls,然后,以及不属于Bulls的字符及其出现的个数。然后遍历guess,利用第一次遍历时统计的信息,计算出cows 读懂题目意思,在计算cows时,对于重复数字,选取二者之间较小的一个原题目:You are playing the following Bulls and Cows game with your friend: Y

2016-10-24 14:15:14 297

原创 leetcode 112. Path Sum

解题思路: 深度优先,递归遍历二叉树,遇到叶子节点,计算路径和是否满足要求。 注意下面代码中,形参path不能用引用 原题目: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 t

2016-10-24 14:04:29 281

原创 leetcode 374. Guess Number Higher or Lower

解题思路: 类似二分查找,注意是谁比谁小,目标值比猜的值小,返回-1 原题目:We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell

2016-10-24 13:58:51 236

原创 leetcode 36. Valid Sudoku

解题思路: 直接判断,9行,9列,9个子面板是否符合要求原题目:Determine if a Sudoku is valid, according to: [Sudoku Puzzles - The Rules.](http://sudoku.com.au/TheRules.aspx)The Sudoku board could be partially filled, where empty c

2016-10-24 13:55:04 283

原创 leetcode 257. Binary Tree Paths

解题思路: 深度优先,递归遍历二叉树,用path保存当前已经访问过的节点,如果当前节点是叶子节点,则将当前已经访问过的节点作为符合要求的路径加入结果集中。如果当前节点不是叶子节点,递归遍历其左子树,然后递归遍历其右子树。 注意一下代码中,形参path不要用引用 原题目:Given a binary tree, return all root-to-leaf paths.For example,

2016-10-24 13:46:43 238

原创 leetcode 225. Implement Stack using Queues

解题思路: 用队列的基本操作实现栈的基本操作原题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top elemen

2016-10-24 13:32:52 234

原创 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() -- Ge

2016-10-24 12:30:57 225

原创 leetcode 110. Balanced Binary Tree

解题思路: 递归判断左子树的高度与右子树的高度之差是否大于1原题目: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 subt

2016-10-24 12:27:19 244

原创 leetcode 235. Lowest Common Ancestor of a Binary Search Tree

解题思路: 注意到已知条件是二叉搜索树,即左子树节点的值小于根节点的值,根节点的值小于右子树节点的值。 如果两个节点位于根节点的两侧,根节点就是最小祖先节点;如果两个节点都小于根节点,递归判断根节点的左子节点;如果两个节点都大于根节点,递归判断根节点的右子节点。原题目:Given a binary search tree (BST), find the lowest common ancesto

2016-10-24 12:23:04 315

原创 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 [1,2,2,

2016-10-24 12:15:25 246

原创 leetcode 70: Climbing Stairs

解题思路: 动态规划,设w[i]表示走完i层楼梯的方式。 i = 1,w[i] = 1,一次一步 i = 2, w[i] = 2, 两次一步与一次两步两种 i > 2,w[i] = w[i-1] + w[i - 2]。即分两种情况,最后一次走一步有w[i-1]中方式,最后一次走两步有w[i-2]种方式。原题目:You are climbing a stair case. It take

2016-10-24 12:08:39 221

原创 leetcode 401: Binary Watch

解题思路: 本题灯的个数,其实就是二进制位中1的个数。最简单的做法,遍历所有时间,如果hour和minute的二进制值中的1的个数满足要求,当前时间就是一个解元素。不过这种方法耗时较长。原题目:A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom rep

2016-10-24 11:28:38 360

原创 leetcode 189: Rotate Array

解题思路: 对于字符串s,设s由字符串s1和s2两部分组成。先把s1逆序,再把s2逆序, 最后把s逆序,数组s就向右旋转了s2.size()个元素 问题描述: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 ro

2016-10-24 11:18:45 242

原创 leetcode 278: First Bad Version

解题思路: 类似二分查找,因为本题一定会存在解,所以开始时去除两个边界,left=2, right=n-1,middle = (left + right) / 2,如果middle是好的,left = middle + 1, 否则right = middle - 1,到最后时,left = right + 1,最早的版本一定在这两个里面。原题:You are a product manager a

2016-10-24 10:55:05 257

原创 leetcode 125: Valid Palindrome

解题思路: 从string两端分别遍历数组,遇到空格,标点跳过,看两边对应字母是否相等。原题目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

2016-10-24 10:29:51 222

原创 leetcode 219: Contains Duplicate II

解题思路: 遍历数组,把所有相同的数的下标都存储起来,然后对每个单独的数,计算其是否存在两个下标值小于k原题目:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] an

2016-10-24 10:22:37 245

原创 leetcode 204: Count Primes

解题思路: 质数是只能被1和它本身整除的数。 质数2: 2 * 2, 2 * 3, 2 * 4, 2 * 5, 2 * 6, 2 * 7, … 2 * (n / 2)都不是质数 质数3 :3 * 2, 3 * 3, 3 * 4, 3 * 5, 3 * 6, 3 * 7, … 3 * (n / 3)都不是质数 3 * 2 在质数2时考虑过了,直接从3 * 3考虑即可 合数4 :因为4为

2016-10-24 10:02:58 271

原创 leetcode 141: Linked List Cycle

解题思路:定义一个集合,顺序遍历链表,如果当前节点已经在集合里,说明存在环,返回true;如果不在,把当前节点存入集合中。遍历结束,返回false.原题:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?AC解,C++代码/** *

2016-10-24 09:09:35 198

原创 leetcode 198: House Robber

解题思路:采用动态规划,设有n栋房子,编号0,1,... ,n-1. nums[i],表示第i栋房子的存钱数目,数组money[n],moeny[i]表示从第0栋房子到第i栋房子,抢劫者可以抢到的最大钱数。则:当 i = 0,money[i] = nums[0];当 i = 1,money[i] = max(nums[0], nums[1]);当 i > 1, money[i] = max(m

2016-10-23 20:33:43 216

原创 C++ 栈和队列

C++提供了三种顺序容器适配器:queue, prioriety_queue和stack。适配器(adaptor)是标准库库中通用的概念,包括容器适配器、迭代适配器和函数适配器。本质上,适配器是使一事物的行为类似于另一事物的行为的一种机制。容器适配器让一种已存在的容器类型采用另一种不同的抽象类型的工作方式实现。1、适配器通用的操作和类型size_type 一种类型,足以存储此适配

2016-10-21 15:19:01 499

原创 C++ set 详解

set容器是键的结合。set支持的操作基本上与map提供的相同。 1、set的定义set<k> set1; //创建一个名为set1的空set对象,其键类型为kset<k> set2(set1); //创建set1的副本set2,set2与set1必须有相同的键类型和值类型set<k> set3(b, e); //创建set类型的对象set3,存储迭代器b和e标记的范围内所有

2016-10-21 14:52:11 2436

原创 C++ map详解

C++ map详解

2016-10-21 14:25:42 6922

原创 C++ bitset 详解

有些程序需要处理二进制位的有序集,每个位可能包含0(关)或1(开)值。位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法。标准库提供的bitset类简化了位集的处理。声明:#include <bitset>using namespace std::bitset;bitset对象的定义和初始化bitset<n> b; b有n位,每位都为0bitset<n>

2016-10-21 13:06:28 3086

原创 C++ vector详解

1、vector对象的定义和初始化vector<T> v1; vector保存类型为T的对象。默认构造函数,v1为空vector<T> v2(v1); v2是v1的一个副本vector<T> v3(n, i); v3是包含n个值为i的元素vector<T> v4(n); v4含有初始化的元素的n个副本注意:vector对象(以及其他标准库容器对象)的重要属性就在于可以运行

2016-10-21 11:31:04 556

原创 C++ string详解

用户程序要使用string类型对象,必须包含相关头文件。如果提供了合适的using声明,编写出来的程序将会变得简短些。#include <string>using std::string;1、string对象的定义和初始化 常用的构造函数: string s1; 默认构造函数,s1为空串 string s2(s1); 将

2016-10-20 09:56:36 729

原创 leetcode 28: Implement strStr() (KMP算法)

关于KMP算法,关键是部分匹配表的计算,本人觉得阮一峰的关于KMP算法的理解写的很好,文章地址http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html以下是C++代码,在leetcode 28上为AC。不过运行时间超慢,请大家多多指正。class Solution {public

2016-10-20 09:30:29 403

原创 leetcode 46: permutations

生成排列 用数组P[1…n]来存放每一个排列。 算法一: 假定可以生成n-1个数的所有排列,那么就可以扩展方法来生成1,2,… ,n这n个数的排列,方法如下。生成数2,3,…, n的所有排列,并且在每个排列的前面加上数1,接下来,生成数1,3,4, … ,n的所有排列,并且在每个排列的前面加上数2.重复这个过程直到最后生成1,2,… ,n-1的所有 排列,并且在每个排列的前面加上数n。

2016-10-19 17:37:47 611

原创 leetcode 215: the Kth Largest in array

问题描述: 给定一个整形数组array, 一个整型值k,寻找数组array中第k大的数,假定k值总有效,即0<=k<=array.length -1算法 SELECT 输入: n个元素的数组A[1…n]和整数k, 1<=k<=n。 输出:A中的第K大元素。 1、 select(A, 1, n, k) 过程 select(A, low, high, k)

2016-10-17 13:43:03 319

原创 二分查找

二分查找针对的是已经排好序的数组。本文用C++实现,代码如下:int binarySearch(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while (left <= right) { int middle = left

2016-10-17 09:45:06 312

原创 Leetcode 15: 3Sum

问题描述: 给定一个整形数组nums,一个整形目标值target,从中找到3个整数的组合,使其和等于target。找到所有且不相同的组合。本题target为0,不为0时只需把0替换为target即可。 以下是C++代码,Accepted。本人菜鸟一个,欢迎大家指正。class Solution {public: vector<vector<int>> threeSum(vector<i

2016-10-16 08:57:52 403

原创 Leetcode 1: two Sum

leetcode 1: two Sum问题描述: 给定一个整形数组nums,一个整形目标值target。从nums中找到两个数,使其和等于target。返回这两个整数的索引。本题假定结果存在且唯一。以下为C++代码,Accepted。本人菜鸟一个,欢迎大家指正。class Solution {public: vector<int> twoSum(vector<int>& nums, in

2016-10-16 08:49:58 414

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除