
数据结构和算法
文章平均质量分 59
Mr_Curious_
自律、自律、自律、自律。要时刻保持奋发向上的精神面貌!!!
展开
-
关于快速排序
快速排序是对冒泡排序的一种改进。关于冒泡排序请参考上篇。快速排序的排序效率高,实用性强;它是原址排序,但不稳定;就平均性能而言,它是最好的排序方法。快速排序是基于分治思想的一种算法,先对原数组进行划分,而后对划分后的两个数组用同样的排序操作。分治法有分割、解决,处理三步,其中最核心部分为第一步:划分parition。划分:首先选择一个基准pivot,将数组A[1...n]划分为两个数组A[1......原创 2017-03-05 20:31:10 · 331 阅读 · 0 评论 -
171. Excel Sheet Column Number()
一、题意 Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 2...原创 2018-03-14 20:48:49 · 147 阅读 · 0 评论 -
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 complete as many transactions as you like (ie, bu...原创 2018-03-15 23:00:28 · 178 阅读 · 0 评论 -
用递归打印数字
一、题目 用递归的方法找到从1到最大的N位整数。 注意事项 用下面这种方式去递归其实很容易: recursion(i) { if i > largest number: return results.add(i) recursion(i + 1) } 但是这种方式会耗费很多的递归空间,导致堆栈溢出。你能够用其他的方式来递归使得递归的深度...原创 2018-03-09 19:48:22 · 513 阅读 · 0 评论 -
排序again(冒泡、选择、插入)
1、冒泡排序: (1) public void sortIntegers(int[] A) { // write your code here //冒泡 大的往后放 int len = A.length; for(int i=0;i<len-1;i++) for(int j=0;j<...原创 2018-03-09 21:55:48 · 146 阅读 · 0 评论 -
快排again
public class Solution { /** * @param A: an integer array * @return: nothing */ int partition(int[] A,int low,int high){ int tmp = A[low];//这里容易出错,我写的是A[0] while(...原创 2018-03-09 22:17:09 · 165 阅读 · 0 评论 -
生成括号
一、题意 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。 样例 给定 n = 3, 可生成的组合如下: “((()))”, “(()())”, “(())()”, “()(())”, “()()()” 二、分析和解答 这道题我不会,不过我大概是知道:左括号数必须得大于右括号数,否则不成立。 查阅资料后,这是一个递归的操作,虽然有点难想明白。上限是输入的参数...原创 2018-03-12 22:47:20 · 306 阅读 · 0 评论 -
最近的反思
当我刚刷了十几道LeetCode的时候,听到周围的人多的已经刷第二遍了,有的人第一遍也刷的差不多了。我有点懵了,我有时候一晚上都不能刷一道题的这种存在,大大的拉大了方差的下线。肯定是哪里出问题了,我速度确实是太慢了,或许是时候换一种刷题思路了。毕竟还有几个月就要找工作了,以这个速度刷下去肯定是一遍都刷不完的,何况最好刷三遍才好。 看一道简单的题时,只用5分钟思考,如果不能想出来,直接看答案的思路...原创 2018-03-14 07:29:35 · 146 阅读 · 0 评论 -
283. 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, 12], after calling y...原创 2018-03-14 08:32:50 · 163 阅读 · 0 评论 -
371. Sum of Two Integers(一个问题)
一、题目 Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 二、分析与解答 要求:不让用加减的操作符,来完成两个整数的和。 请恕我想到二进制、左移、右移,就是想不...原创 2018-03-14 20:15:11 · 185 阅读 · 0 评论 -
344. Reverse String 字符串反转
1、题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”. 2、分析与解答 最基本的想法就是首尾交换,将第一个与最后一个,第二个与倒第二个交换,依次进行直到中间即可。 class Sol...原创 2018-02-27 19:41:54 · 214 阅读 · 0 评论 -
冒泡排序
Quicksort快速排序是对冒泡排序的一种改进。那么就不得不先说一下冒泡排序!提出问题:给定的一个随机数组,将数组排好序输出。一、解决思路:将数组第一个位置上的元素与其他所有元素一一对比,得到最小值并将其放到第一个位置;将数组第二位置上的元素与第三个到最后一个元素一一对比,得到最小值并将其放到第二个位置上;依次循环第三个、四个位置上一直到数组最后一个位置。便可得到排好的顺序。(思考:这是冒泡排序...原创 2017-02-25 11:57:50 · 340 阅读 · 0 评论 -
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, and you may not use原创 2017-12-26 09:28:26 · 147 阅读 · 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原创 2017-12-29 11:03:33 · 146 阅读 · 0 评论 -
LeetCode( Roman to Integer)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 看到这道题,我是懵的,为何?我根本就不了解罗马数字的基本知识,想不到它还有这个规律。 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)原创 2018-01-06 09:18:47 · 207 阅读 · 0 评论 -
LeetCode( Valid Parentheses)有效的括号
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid bu原创 2018-01-12 10:08:27 · 186 阅读 · 0 评论 -
771. Jewels and Stones
1、题目 You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of t...原创 2018-02-26 20:22:56 · 120 阅读 · 0 评论 -
LeetCode( Longest Common Prefix)最长公共前缀(需要回顾!)
Write a function to find the longest common prefix string amongst an array of strings. 找出所有字符串数组中的公共前缀。 一、我的第一想法是,拿第一个字符串作为基准,取它的每一位和剩余所有字符串的相对应的位数相比较,如果相同存储这位的字符,并继续向后移动,一旦出现某个字符串的当前位和第一个字符串当前位...原创 2018-01-09 23:20:23 · 361 阅读 · 0 评论 -
合并两个有序链表(Merged 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. Example: Input: 1->2->4, 1->3->4 Output: 1->...原创 2018-02-26 19:48:25 · 223 阅读 · 0 评论 -
二分查找小晋级
问题:二分查找、二分查找与快速排序、二分查找与双指针 1、最简单的二分查找代码(查找等于key的元素): #include &lt;iostream&gt; using namespace std; int binary_search(int arr[],int n,int key); int main() { int arr[] = {0,1,2,2,2,5,6}; int...原创 2018-04-07 11:38:40 · 183 阅读 · 0 评论