
算法
文章平均质量分 54
gwt0425
这个作者很懒,什么都没留下…
展开
-
数据结构-反转链表详解
1. 单链表反转class ListNode<T>{ T val; ListNode next; ListNode(T val){ this.val = val; }}迭代方式public ListNode reverseByIteration(ListNode head){ ListNode prev = null; while(h原创 2017-07-12 19:23:23 · 513 阅读 · 0 评论 -
Leetcode 50. Pow(x, n)
描述Implement pow(x, n).分析传统的方式是迭代地乘,这样是O(N)用类似二分的方式可以降为O(logN)大坑是:如果n < 0, 就需要1 / pow(x, -n)代码class Solution: def posPow(self, x, n): if n == 0: return 1 ...原创 2018-03-04 21:43:12 · 153 阅读 · 0 评论 -
LeetCode 25. Reverse Nodes in k-Group
描述[Hard]Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the ...原创 2018-03-04 19:24:28 · 169 阅读 · 0 评论 -
Leetcode 42. Trapping Rain Water
描述Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,...原创 2018-03-07 20:20:17 · 235 阅读 · 0 评论 -
Leetcode 75. Sort Colors
描述Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integer...原创 2018-03-06 19:14:01 · 186 阅读 · 0 评论 -
算法-红黑树原理
红黑树简介红黑树可以用作Map,来完全O(logN)的查找。和HashMap相比,优点是能遍历保持有序。红黑树与普通二叉查找树相比,红黑树是完美平衡的,如果按顺序由小到大插入,不会出现二叉树那样退化为O(N)的链表。2-3树先看一下2-3树,因为红黑树的发明者(算法红宝书的作者)说,红黑树是一种实现2-3树的数据结构。红黑只是为了获得一种统一的表达形式,而不是直接使用两种节原创 2018-01-26 22:03:55 · 541 阅读 · 0 评论 -
回溯法-Leetcode转
Subsets :https://leetcode.com/problems/subsets/public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayLis原创 2017-11-20 17:40:34 · 213 阅读 · 0 评论 -
Leetcode二分查找算法
Python的二分搜索模块#从左边开始找一个位置bisect.bisect_left(arr, target)#从右边找到一个位置bisect.bisect_right(arr, target)bisect.bisect(arr, target)#有序插入bisect.insort(arr, target)#例:arr:[1,2,2,4,5,6]bisect_left(arr,2) =原创 2017-11-13 17:25:00 · 273 阅读 · 0 评论 -
算法-3Sum问题
给定一个列表,在列表中找出3个数字,使得a+b+c=0.找到所有不同的组合For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]第一次尝试:暴力解法O(n^3)用三层循环,直到找出所有解。但这样做有2个问题。第一个是超时,O(n^3)的解法肯定炸了;第二原创 2017-10-24 11:43:31 · 634 阅读 · 0 评论 -
剑指offer-1
二维数组中查找在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。# -*- coding:utf-8 -*-class Solution: # array def Find(self, target, array): rows = len(array)原创 2017-10-31 20:10:19 · 214 阅读 · 0 评论 -
算法-二叉树解题
题目1:Leecode-107 给定一个二叉树,从左向右地,自底向上的遍历每一层的元素。例:给定二叉树: 3 / \ 9 20 / \ 15 7返回的结果:[ [15,7], [9,20], [3]]首先,要有一个概念,无论是树的题目还是图的题目原创 2017-07-23 19:35:59 · 315 阅读 · 0 评论 -
算法--分治法寻找中值
问题描述给定任意一个整数列,寻找这个整数列的中值。解题思路由于要用到分治算法,会递归求解,所以推广问题为:寻找一个整数列的第k大的数字。 思路1:将序列排序后再寻找第k大的数字,可以用到快速排序或归并排序。 思路2:使用快速排序的切分思想,将序列切为大于,等于,小于pivot的三个数组,分别叫L,M,R。如果k小于L的长度,那么在L里面递归;如果k大于L+M的长度,在R里面寻找第(k - len原创 2017-07-14 10:52:45 · 2797 阅读 · 0 评论 -
算法--位运算
基本运算bin(num) #将十进制整数取二进制a & b #位与,(5 & 9 = 1) => (0101 & 1001 = 0001)a | b #位或,(5 | 9 = 13) => (0101 | 1001 = 1101)a ^ b #位异或(半加),(5 ^ 9 = 12) => (0101 ^ 1001 = 1100)~a原创 2017-07-13 18:24:12 · 248 阅读 · 0 评论 -
算法--一揽子排序算法(1)
冒泡排序持续比较相邻的元素,使前者大于后者第i次排序后,最后的i个数字是有序的,因此内循环有(1,len(lis) - i)复杂度是n+(n-1)+(n-2)+…+1 = O(n^2)def BubbleSort(lis): for i in range(len(lis)): print(lis) for j in range(1,len(lis)-i原创 2017-07-13 12:39:23 · 301 阅读 · 0 评论 -
Leetcode 55. Jump Game
描述Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i...原创 2018-03-05 15:00:44 · 161 阅读 · 0 评论