自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(34)
  • 收藏
  • 关注

原创 算法概论第8章课后习题 8.3

吝啬SAT问题是这样的:给定一组字句(每个子句都是其中文字的析取)和整数k,求一个最多有k个变量为true的满足赋值——如果该辅助存在。证明吝啬SAT是NP-完全问题。证明:要想证明吝啬SAT是NP-完全的,可以证明SAT问题可以归约到吝啬SAT问题,因为SAT也是一个NP-完全问题。设g是一个SAT问题的实例,当SAT中的变量数目为k时,则该问题就是一个吝啬SAT实例。如果实例g的解存在的话

2018-01-03 10:59:01 262

原创 413. Arithmetic Slices

题目:我们称一组数列为arithmetic的,当数列至少有三个数,相邻的数字间的差是相同的。换句话说,就是判断该数列是不是等差数列。给定一数组,判断该数组中可以提取多少个等差数列。例子如下:A = [1, 2, 3, 4]return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itse

2017-12-15 21:41:28 173

原创 693. Binary Number with Alternating Bits

题目:给定一个正整数,判断该整数的二进制形式相邻的两位是否是不同的。例子如下:Example 1:Input: 5Output: TrueExplanation:The binary representation of 5 is: 101Example 2:Input: 7Output: FalseExplanation:The binar

2017-12-15 17:11:00 182

原创 553. Optimal Division

题目:给定一数组,相邻的整数执行除法,但是我们可以通过添加括号改变计算的优先级,求这组数字应该如何添加括号,从而得到的结果值最大,同时结果中不应该有多余的括号,以字符串形式返回结果。例子如下:Input: [1000,100,10,2]Output: "1000/(100/10/2)"Explanation:1000/(100/10/2) = 1000/((100/10)/2) =

2017-12-15 15:53:12 188

原创 136. Single Number

题目:给定一个整数数组,除了一个元素之外,其他每个元素都出现两次,求这个值出现一次的数组。解题思路:我们知道,每个数字与本身抑或,得到的结果都是0,而每个数字与0抑或,得到数字本身。所以,我们直接遍历数组,将所有的元素抑或,就可以得到那个值出现一次的元素。代码如下:class Solution {public: int singleNumber(vector& nums) {

2017-12-15 13:33:35 136

原创 515. Find Largest Value in Each Tree Row

题目:给定一颗二叉树,求每一层节点中的最大值。例子如下:Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]其实,解决这道题目的思路很简单,跟之前写过的一两道二叉树的题目差不多。问题是,我们怎么知道每一层的开始跟结束呢?这里还是要用

2017-12-15 13:17:10 155

原创 292. Nim Game

题目:你和朋友玩一个游戏:桌子上有一堆石头,每次你们轮流拿走1到3块石头,最后拿走最后一块石头的人就是胜利者,而每次游戏开始你都是第一个拿石头的人。现在,给定石头的数目n,判断这回游戏你能否可以取得胜利。按照题目的要求,我们知道,如果n是1,2,3的话,一定可以取得胜利,如果是4的话,无论第一次拿走多少块石头,最终都无法取得胜利。所以,为了取得胜利,我们必须保证不会有这样的情景出现:当你拿石头

2017-12-15 12:49:52 233

原创 728. Self Dividing Numbers

题目:self-divide数的定义是,一个整数可以被它的每一位数整除。比如说,128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。现在给定两个整数,left跟right,求出在left跟right之间的self-divide数。例子如下:Input: left = 1, right = 22Output: [1, 2, 3, 4, 5, 6, 7, 8, 9

2017-12-14 22:18:46 175

原创 637. Average of Levels in Binary Tree

题目:给定一颗二叉树,求每一层节点的平均值。例子如下:Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3, on level 1 is 14.5, and on

2017-11-21 19:32:42 139

原创 647. Palindromic Substrings

题目要求:给定一个字符串,求字符串有多少个回文子字符串。例子如下:Example 1:Input: "abc"Output: 3Explanation: Three palindromic strings: "a", "b", "c".Example 2:Input: "aaa"Output: 6Explanation: Six palindr

2017-11-17 23:15:22 131

原创 540. Single Element in a Sorted Array

题目的要求是:给定一个排好序的数组,数组中只有一个数出现一次,其他的数字都出现两次,要我们找到出现一次的数字。算法的时间复杂度要求是O(log N),空间复杂度为O(1)。例子如下:Example 1:Input: [1,1,2,3,3,4,4,8,8]Output: 2Example 2:Input: [3,3,7,7,10,11,11]Output:

2017-11-16 20:54:11 134

原创 513. Find Bottom Left Tree Value

题目的要求很简单,就是给定一颗二叉树,要求我们返回最低层中在最左端的子节点的值。例子如下:Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6

2017-11-16 20:37:52 128

原创 406. Queue Reconstruction by Height

题目:假设现在有一队列,队列中的人随机站立。每个人都被一个数对(h,k)所描述,h表示这个人的身高,k表示站在这个人前面身高比他高的人数。要求我们重新排列顺序,使得每个人的站位都符合数对的描述。例子如下:Input:[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]Output:[[5,0], [7,0], [5,2], [6,1], [4,4

2017-11-16 15:58:42 149

原创 442. Find All Duplicates in an Array

题目要求:给定一个数组,数组大小为n,数组中每个元素的大小在1到n之间,有一些数字出现了两次,其他出现了一次,我们的目的就是找出这些出现两次的数字。然后题目要求希望我们在不需要额外空间下实现时间复杂度为O(n)的解法。例子:Input:[4,3,2,7,8,2,3,1]Output:[2,3]我们注意到,每个元素的大小都是在1到n之间,也就是说,如果按照顺序排列的话,

2017-11-14 17:04:23 160

原创 496. Next Greater Element I

题目的要求是给定数组1跟数组2,数组1是数组2的子集,遍历数组1的每个数组,找到在数组2中比数组1中该元素大的第一个元素,若没有比该元素大的,则返回1。最终返回一个数组。例子如下:Example 1:Input: nums1 = [4,1,2], nums2 = [1,3,4,2].Output: [-1,3,-1]Explanation: For number 4

2017-11-14 10:15:12 192

原创 463. Island Perimeter

题目的要求是:给定一个二维数组,由0跟1组成,0代表水,1代表陆地,所有的1组成一个小岛,让我们计算小岛的周长。例子如下:Example:[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]Answer: 16Explanation: The perimeter is the 16 yellow stripes in the imag

2017-11-13 10:27:45 174

原创 669. Trim a Binary Search Tree

题目的要求是,给定一个二叉树跟一个数值区间,现在需要我们重构二叉树,使得每个节点的值都在区间内里面。例子如下:Example 1:Input: 1 / \ 0 2 L = 1 R = 2Output: 1 \ 2面对这道题,我们最直观的想法是用递归去解决,因为根据题目的要求,我们对每一个

2017-11-13 09:52:09 148

原创 566. Reshape the Matrix

题目的要求是:给定一个矩阵,根据给出的r跟c,即行数跟列数,去重塑矩阵,即返回一个行数为r,列数为c的矩阵。其实解题思路很简单,因为不管矩阵怎么排列,元素的总个数是不变的,我们只需要重新计算每个元素的位置即可,就是计算出每个元素在新矩阵中的下标。完整的代码如下:class Solution {public: vector> matrixReshape(vector>& nums, i

2017-11-03 13:11:26 138

原创 412. Fizz Buzz

题目的要求是实现一个功能函数,给定一个数字n,输出1-n的字符串数组。其中如果该数字是3的倍数,替换为“Fizz”;如果是5的倍数,替换为“Buzz”;如果既是3的倍数又是5的倍数,替换为“FizzBuzz”。题目给出的例子如下:n = 15,Return:[ "1", "2", "Fizz", "4", "Buzz", "

2017-10-27 13:24:10 144

原创 575. Distribute Candies

题目的要求是给定一串数组,数组代表了不同种类的糖果。数组内的每个元素代表该糖果的种类。现在有一对兄妹,将糖果平分,求妹妹最多可以分到多少种糖果。其实解决的思路很简单:我们声明一个变量sum来记录妹妹得到的糖果种类数,初始化为1。我们遍历整个数组,如果有妹妹还没到的糖果种类,sum增加1,说明将该糖果分给妹妹,当数组遍历完之后,妹妹可以获得最多种类的糖果。当然,这要建立在妹妹的糖果数与哥哥的是相

2017-10-27 12:41:10 139

原创 682. Baseball Game

题目的要求是,按照给定的字符串,计算出每个回合得到的分数,同时计算出总分,最后返回总分。计算分数的规则如下:数字:当前回合的直接得分C:上一回合的得分取消D:本回合得到的分数为上一次得到的分数的两倍+:本回合的得分为前两次得到的分数之和解决问题的思路是:遍历给定的字符串,判断每次回合得到的分数。利用vector去存储每一回合得到的分数:数字:入栈C:取消上一回合得到的分

2017-10-23 19:36:20 411

原创 344. Reverse String

题目要求是反转给定的字符串,思路很简单,我们只要从后往前遍历一遍字符串,生成新的字符串就可以了。代码如下:class Solution {public: string reverseString(string s) { string re = ""; for (int i = s.size() - 1; i >= 0; i--)

2017-10-23 18:40:20 125

原创 557. Reverse Words in a String III

题目的要求是反转字符串,以空格为间隔,将每一个单词反转,然后返回结果。我的思路是,利用vector实现栈,以空格为标记,遍历每个字符,如果不是空格,则入栈,如果是空格,说明该单词已结束,出栈,实现反转。完整的代码如下:class Solution {public: string reverseWords(string s) { vector word;

2017-10-17 00:04:10 114

原创 500. Keyboard Row

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.Example 1:Input: ["Hello", "Alaska", "Da

2017-10-14 23:28:11 156

原创 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

2017-10-14 21:54:42 115

原创 476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range of

2017-09-30 23:34:44 138

原创 419. Battleships in a Board

Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:You receive a valid

2017-09-30 22:53:48 121

原创 537. Complex Number Multiplication

Given two strings representing two complex numbers.You need to return a string representing their multiplication. Note i2 = -1 according to the definition.Example 1:Input: "1+1i", "1+1i"O

2017-09-21 21:50:09 160

原创 561. Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large a

2017-09-20 09:57:06 251

原创 617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tr

2017-09-17 13:13:55 138

原创 657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.The move sequence is represented by

2017-09-17 11:40:42 154

原创 461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x, y <

2017-09-14 22:32:05 127

原创 654. Maximum Binary Tree

原题如下:Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array.The left subtree is the maximum tree constr

2017-09-13 23:13:33 983

原创 535. Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.Design the encode and de

2017-09-11 10:22:22 251

空空如也

空空如也

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

TA关注的人

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