算法
Frank Kong
这个作者很懒,什么都没留下…
展开
-
LeetCode——78. 子集和90. 子集 II
这两道题是同一类型的题目,有着相同的规律,子集II在I的基础上加了一些判断条件 题目描述: 78.子集 给定一组不含重复元素的整数数组nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2],...原创 2019-10-13 22:28:23 · 131 阅读 · 0 评论 -
LeetCode——79. 单词搜索
题目: 给定一个二维网格和一个单词,找出该单词是否存在于网格中。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "A...原创 2019-10-13 16:24:57 · 133 阅读 · 0 评论 -
leetcode——删除链表的倒数第N个节点
这道题属于学完数据结构的进阶题,考研的时候经常会见到,现在leetcode见到了它 描述: 给定一个链表,删除链表的倒数第n个节点,并且返回链表的头结点。 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5. 说明: 给定的 n保证是有效的。 思路: 快慢两...原创 2019-10-03 19:30:11 · 148 阅读 · 0 评论 -
LeetCode——33. 搜索旋转排序数组
题目描述如下: 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组[0,1,2,4,5,6,7]可能变为[4,5,6,7,0,1,2])。 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回-1。 你可以假设数组中不存在重复的元素。 题目要求的时间复杂度是O(log n),很容易就想到了二分法,设定三个变量,low,mid,high,...原创 2019-10-05 20:57:48 · 136 阅读 · 0 评论 -
LeetCode——24. 两两交换链表中的节点
题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 解题: 这道题不算难,设置三个指针用于节点交换,然后考虑进去一些特殊情况,没有太大问题 特殊情况主要有以下几种: 链表只有一个节点或者为N...原创 2019-10-06 11:15:59 · 134 阅读 · 0 评论 -
LeetCode——39. 组合总和(回溯法)
这道题是典型的回溯法求解问题,首先简单介绍一下回溯法,学过图的深度遍历,求解过图的连通图问题之后,应该能够理解。 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。 用回溯算法解决问题的一般步骤: 1、 针对所给问题,定义问题的解空间,它至少包含问题的一个(最优)解。 2 、确定易于搜索的解空间结构,使得能...原创 2019-10-07 10:34:54 · 234 阅读 · 0 评论 -
LeetCode——40. 组合总和 II
题目描述: 给定一个数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。 candidates中的每个数字在每个组合中只能使用一次。 这道题与上一题公共部分是都使用回溯算法,区别是: 每个数字只能使用一次 数组中有重复的数字 这样,算法中就必须增加判断是否重复的判断条件 void backtrack(ve...原创 2019-10-07 11:30:59 · 118 阅读 · 0 评论 -
回溯算法(以全排列为例)
首先是题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列。 回溯算法是什么?回溯算法是系统搜索问题解集的一种方法,说白了就是一种搜索方法。回溯法按深度优先策略搜索问题的解空间树。首先从根节点出发搜索解空间树,当算法搜索至解空间树的某一节点时,先利用剪枝函数判断该节点是否可行(即能得到问题的解)。如果不可行,则跳过对该节点为根的子树的搜索,逐层向其祖先节点回溯;否则,进入该子树,继续...原创 2019-10-07 18:27:17 · 3069 阅读 · 0 评论 -
LeetCode——47. 全排列 II
这道题与上一道全排列题目的区别是序列中有重复数字,并且要返回所有不重复的全排列组合。 思路: 依旧使用回溯算法,要加入判断去重,因为回溯算法是在n个for循环的基础上改进而来,所以下图用三个for循环的方式来说明重复 当回到最外层循环的时候,i 指向1,再向内深入就会造成(1, 1, 2)重复,且此时i > 0 && nums[i] == nums[i - 1...原创 2019-10-10 19:49:29 · 127 阅读 · 0 评论 -
LeetCode——77. 组合
给定两个整数n和k,返回 1 ...n中所有可能的k个数的组合。 敲定回溯算法, 输入:n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 这个例子可以看出,深度为2时,进行回溯。回溯的过程: 1,2,3,4 2,3,4 3,4 当前位置为i时,下次回...原创 2019-10-11 14:17:04 · 111 阅读 · 0 评论 -
二刷leetcode——3Sum(双指针法)
在提交代码的时候,一直提示Runtime error,后来发现是对vector操作的问题,内存访问出错, nums[k] == nums[k - 1] 这一句,当k = 0的时候出错,改成 k > 0 && nums[k] == nums[k - 1] 对3Sum的解决方法如下: 对nums进行排序,如果nums.size() < 3,则返回空vecto...原创 2019-10-03 18:48:30 · 196 阅读 · 0 评论 -
LeedCode——3Sum Closest(求最接近指定数的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2018-01-17 17:21:49 · 309 阅读 · 0 评论 -
Median of Two Sorted Arrays(两个顺序数组求中位数)
Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 有两种方法: 1、在合并的时候就排好序,再求中位值 2、先合并,然后进行排序,最后求中位值 第原创 2018-01-10 20:44:01 · 359 阅读 · 0 评论 -
Longest Palindromic Substring 找最长的回文子字符串
所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的。比如"level" 、 “aaabbaaa” Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "bab原创 2018-01-11 16:58:11 · 325 阅读 · 0 评论 -
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 N A P L S I原创 2018-01-12 13:40:18 · 188 阅读 · 0 评论 -
Add Two Numbers(两个链表求和)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return原创 2018-01-12 13:46:37 · 397 阅读 · 0 评论 -
(LEEDCODE)String to Integer (atoi) 字符串转整型
Requirements for atoi: The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional原创 2018-01-13 10:29:48 · 203 阅读 · 0 评论 -
(LEEDCODE)Palindrome Number判断回文数
Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However,原创 2018-01-13 11:00:13 · 203 阅读 · 0 评论 -
Container With Most Water(装最多的水)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find原创 2018-01-13 15:36:24 · 266 阅读 · 0 评论 -
LeedCode——三数求和为0
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain d原创 2018-01-16 19:32:23 · 296 阅读 · 0 评论 -
C++写算法经常会用到的库函数总结(日常更新)
——————2018-1-17更———————— 1、求绝对值函数 abs( ),需要的头文件: 2、string.substr(i,j) 截取字符串 3、去首尾空格函数 str.erase(0,str.find_first_not_of(" ")); str.erase(str.find_last_not_of(" ") + 1);原创 2018-01-17 17:06:25 · 735 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b",原创 2018-01-11 10:58:24 · 224 阅读 · 0 评论