算法技巧
茂升快跑
等我做程序员赚了钱,就买设备去烤冷面。
展开
-
面试算法题常用Java技巧
对List排序3.翻转数组4.翻转字符串原创 2022-06-27 14:21:47 · 304 阅读 · 1 评论 -
茂升面试中遇到的算法题-2022
面试算法题原创 2022-06-12 16:16:19 · 212 阅读 · 0 评论 -
LeetCode题解:342. Power of Four
题目链接:342. Power of Four题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follo原创 2016-05-16 15:55:24 · 606 阅读 · 0 评论 -
LeetCode:319. Bulb Switcher(C版本)
题目链接:这里写链接内容题目内容: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s原创 2016-02-28 10:22:06 · 564 阅读 · 0 评论 -
LeetCode:39. Combination Sum(C++版本)
题目链接:39. Combination Sum题目内容: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be原创 2016-02-28 09:25:30 · 1151 阅读 · 0 评论 -
A*算法解决八数码问题(C++版本)
八数码问题定义: 八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始转变成目标状态的移动棋子步数最少的移动步骤。A*算法的通用伪代码 :A*算法解决八数码问题的关键之处: 关键之处: 要维护两个结构:open表原创 2016-03-09 11:34:07 · 13431 阅读 · 0 评论 -
LeetCode:142. Linked List Cycle II
题目链接:Linked List Cycle II题目描述: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve原创 2016-02-19 10:58:31 · 397 阅读 · 0 评论 -
A*算法解决八数码问题时,第N步的解空间是多少?
我们在解决八数码问题是,会关心一个解空间的问题,比如我们进行了27步操作时,当前状态有多少可能的方案呢? 统计解空间的大小,有助于我们更好的优化算法。我们可以利用广度优先策略,来统计第N步的可能方案。利用hash表来存储可行方案,从而剔除重复方案。利用回溯的方式,从第N步开始往起点回退,能回退到起点的方案就是可能性方案,记录该方案。下面就是我们统计八数码问题进行到第27步以后,统计解空间大原创 2016-03-29 19:33:37 · 963 阅读 · 0 评论 -
利用图搜索来优化八数码问题的A*算法
在最开始的用A*算法的时候,我们使用树形结构来生成后继的拓展节点,导致我们的解空间是按指数增长,但是实际上我们的路径节点的总个数是一定的,相互链接成了一个图的结构,我们利用树形结构来生成子节点的时候实际上生成了重复的节点。所以图搜索的关键是对生成的重复节点的处理,从而减小了解空间。下面是算法的为代码: 灰色的区域是利用图搜索技术对算法进行改进。下面是C++代码:#include <iostream原创 2016-03-29 19:24:45 · 2439 阅读 · 0 评论 -
LeetCode题解:Search a 2D Matrix
题目链接:Search a 2D Matrix题目描述: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 1.Integers in each row are sorted from left to rig原创 2016-01-06 10:04:14 · 420 阅读 · 0 评论 -
LeetCode题解:Binary Tree Paths
题目链接:Binary Tree Paths写在之前:这个题目的思路比较清晰,采用递归方式去遍历叶节点即可,即深度优先搜索(DFS),比较难处理的就是如何构造结果集,换句话说就是如何将已经遍历的节点保存起来,并将已遍历完的路径按要求方式添加到结果数组中。 我们采用的方式是:利用函数参数传递的方式,将正在遍历的路径使用string变量保存,并传递给下一次函数调用,遍历完一条路径后添加到vector中原创 2015-12-20 21:14:40 · 505 阅读 · 0 评论 -
LeetCode题解:122. Best Time to Buy and Sell Stock II
题目链接:122. Best Time to Buy and Sell Stock II题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may co原创 2016-04-11 23:38:49 · 508 阅读 · 0 评论 -
LeetCode题解:121. Best Time to Buy and Sell Stock
题目链接: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 transactio原创 2016-04-09 11:07:34 · 417 阅读 · 0 评论 -
LeetCode:485. Max Consecutive Ones
题目链接:485. Max Consecutive Ones题目描述: Given a binary array, find the maximum number of consecutive 1s in this array. Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the las原创 2017-04-20 11:22:58 · 515 阅读 · 0 评论 -
LeetCode: 461. Hamming Distance
题目名称: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 Hammin原创 2017-04-14 00:36:15 · 467 阅读 · 0 评论 -
LeetCode:69. Sqrt(x)
题目链接:69. Sqrt(x)题目描述: Implement int sqrt(int x) Compute and return the square root of x.题目解释:提干非常简单,就是实现一个整数的求平方根的函数,输入为int,输出也是int。解题方案:求一个整数的平方根,最土的办法是从0开始尝试0,1,2,3,4…判断是不是原创 2017-03-05 22:34:24 · 628 阅读 · 0 评论 -
LeetCode题解:189. Rotate Array
题目链接: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].题目解释:提干描述的比较简单,就是给一个整原创 2017-01-07 21:52:04 · 755 阅读 · 0 评论 -
LeetCode题解:1.Two Sum
题目链接:1. Two Sum题目描述:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution. Given nums原创 2016-11-17 23:16:08 · 486 阅读 · 0 评论 -
算法题:Patterns and Pictures
前言:这道题是:2008 ACM ICPC South Central USA Regional Programming Contest 的比赛试题,也是2016年10月15日链家网的笔试题。题目链接:Patterns and Pictures题目描述: Fabrics often have repeating patterns on them, such as a tessellation o原创 2016-10-31 12:36:47 · 443 阅读 · 0 评论 -
IP从整型地址转换为点分形式
题目描述: 通常我们在存储IPv4地址的时候不会直接使用字符串,例如192.168.1.1,我们不用“192.168.1.1”这个字符串来存储,而是将这个IP地址转换为一个无符号整数,因为一个IPv4的地址总体上刚好是32位二进制数,只是用了“.”符号每八位进行了一个分割,所以我们只要使用一个32位的无符号整型来存储即可,这样只要4字节,如果使用字符串则需要更多的字节,我们需要做的就是每次在使用原创 2016-09-27 13:39:53 · 5765 阅读 · 0 评论 -
LeetCode题解:345. Reverse Vowels of a String
题目链接: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:原创 2016-05-16 19:36:30 · 413 阅读 · 0 评论 -
LeetCode题解:First Bad Version
题目链接:这里写链接内容题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is dev原创 2015-12-16 22:07:01 · 386 阅读 · 0 评论 -
LeetCode题解:Nim Game
题目链接:Nim Game题目描述: 两个人取一堆石子,每人移走1、2、3块石头,移除最后一个石子的人获胜,把自己作为第一个取石子的人,在游戏中我们总是采取最优策略来取石子,以获得最后的胜利。题目解释:由题目描述我们可以得出一下几点:用户本身作为第一个取石子的人。采取最优策略,也就是说最后的结果是唯一的:要么赢要么输。如何取石子已经不重要了。解题方案:最开始的时候,我的想法是采用递归的原创 2015-11-28 09:40:54 · 615 阅读 · 0 评论 -
LeetCode题解:Power of Two
题目链接: ## Pow of Two题目描述: Given an integer, write a function to determine if it is a power of two.题目解释:给定一个整数,实现一个函数,来判断这个整数是否是2的次幂。 比如 8 为 2 的 3次幂, 5 则不是 2 的次幂。解题方案:如果一个数能一路被2整除,直到变成数字1,那么这个数就是2的次幂原创 2015-07-15 09:00:35 · 531 阅读 · 0 评论 -
LeetCode题解:Invert Binary Tree
题目链接:Invert Binary Tree 题目描述: Invert a binary tree 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1原创 2015-07-03 21:08:36 · 416 阅读 · 0 评论 -
位向量的解读(初始化、赋值、取值)
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl原创 2015-06-28 09:51:13 · 2407 阅读 · 0 评论 -
LeetCode题解:Merge Sorted Array
题目链接:merge-sorted-array题目描述:Give two sorted integer arrays nums1 and nums2.merge nums2 into nums1 as one sorted array.注意:You may assume that nums1 has enough space (sizethat is gre原创 2015-05-29 17:52:06 · 470 阅读 · 0 评论 -
LeetCode题解:Reverse Linked List
题目链接:Reverse Linked List题目描述:Reverse a singly linked List题目解释:翻转一个单链表。所谓的单链表就是一个节点只有一个后继的数据结构。解题方案:我们使用是三个临时变量来存储当前节点、当前节点的前一个节点和当前节点的后继。struct ListNode *pre = NULL; struct Lis原创 2015-05-21 10:45:20 · 435 阅读 · 0 评论 -
LeetCode题解:Same Tree
题目链接:same-tree题目描述:Give two binary trees,write a function to check if they are equal or not .Two binary trees are considered equal if they arestructurally identical and the nodes have th原创 2015-04-06 09:54:03 · 501 阅读 · 0 评论 -
LeetCode题解:excel-sheet-column-title
题目链接:excel-sheet-column-title题目描述:Give a positive integer, return its corresponding column title as appear in an Excel sheet. For example : 1 -> A 2 -> B 3 -> C ...原创 2015-03-27 11:57:27 · 520 阅读 · 0 评论 -
LeetCode题解:excel-sheet-column-number
题目链接:excel sheet column number题目描述:Give a column title as appear in an Excel sheet,return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27原创 2015-03-26 20:38:04 · 435 阅读 · 0 评论 -
LeetCode题解:number-of-1-bits
在云南出差的时候我开始刷LeetCode上的算法题了,一共一百八十多道题,不知道我得做到什么时候。好了闲话不多说,先来对我做出的第一道题写个报告:题目链接:点击打开链接number-of-1-bits。题目描述:Write a function that takes an unsigned integerand return the number of '1'原创 2015-03-26 15:07:14 · 588 阅读 · 0 评论 -
LeetCode题解:Remove Element
题目链接:Remove Element 题目描述: Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave原创 2015-07-16 07:45:19 · 461 阅读 · 0 评论 -
LeetCode题解:Path Sum
题目链接: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. For example: Given the be原创 2015-07-16 10:31:32 · 497 阅读 · 0 评论 -
LeetCode题解:Range Sum Query - Immutable(C++版)
题目链接:Range Sum Query - Immutable题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example Given nums = [-2, 0, 3, -5, 2, -1] sumRang原创 2015-12-10 23:51:41 · 528 阅读 · 0 评论 -
LeetCode题解:Range Sum Query - Immutable(C++版本)
题目链接:Range Sum Query - Immutable题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRan原创 2015-11-22 10:44:03 · 685 阅读 · 0 评论 -
LeetCode题解:Move Zeroes
题目链接: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, 1原创 2015-11-15 23:29:58 · 409 阅读 · 0 评论 -
LeetCode题解:Missing Number
题目链接:Missing Number题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Not原创 2015-09-24 10:14:18 · 592 阅读 · 0 评论 -
LeetCode题解:Longest Increasing Subsequence O(N^2解法)
题目链接:Longest Increasing Subsequence题目描述: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest原创 2015-11-06 21:00:05 · 518 阅读 · 0 评论 -
LeetCode题解:Add Binary
题目链接:Add Binary 题目描述: Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”.题目解释:给定两个01串(字符串内的字符全是0或者1),返回这连个串相加的结果。 例如: 串a=”11”原创 2015-09-08 08:27:52 · 447 阅读 · 0 评论