算法
文章平均质量分 78
zoufangyingzi
这个作者很懒,什么都没留下…
展开
-
五大常用算法
1、分治法一、基本概念在计算机科学中,分治法是一种很重要的算法。字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并。这个技巧是很多高效算法的基础,如排序算法(快速排序,归并排序),傅立叶变换(快速傅立叶变换)……任何一个可以用计算机求解的问题所需的计算时转载 2015-02-04 09:13:50 · 428 阅读 · 0 评论 -
刷leetcode:Find Peak Element
题号:162 题目A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain原创 2015-03-14 10:32:38 · 398 阅读 · 0 评论 -
刷leetcode:Climbing Stairs
题号:70 题目You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Hide T原创 2015-03-12 11:00:17 · 385 阅读 · 0 评论 -
二分查找的Java实现
二分查找,也成为折半搜索算法(Wiki).使用二分查找的前提是数组是有序的,时间复杂度O(logn),空间复杂度O(1).实现上有递归和非递归两种方式。递归方式:/** * 二分查找:递归方式 * * @param a * 有序数组 * @param t * 需要查找的目标值 * @return 目标值在数组原创 2015-03-06 15:26:48 · 691 阅读 · 0 评论 -
刷leetcode:Intersection of Two Linked Lists
题号:160 题目Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2原创 2015-03-06 22:31:19 · 319 阅读 · 0 评论 -
刷leetcode:Merge Sorted Array
题号:88 题目问题描述:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additi原创 2015-03-06 14:03:04 · 335 阅读 · 0 评论 -
刷Leetcode:Evaluate Reverse Polish Notation
题号:150 题目Evaluate Reverse Polish NotationEvaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an inte原创 2015-03-06 11:09:55 · 394 阅读 · 0 评论 -
重组字符串
在面试过程中遇到的这个问题。对于输入:***a*b*cd**e*....期望输出:********abcde....要求:将字符串中的*全部移动到前半部分,字母移动到后半部分,并保持顺序不变思路1:定义两个StringBuilder,遍历输入,如果是*存入第一个StringBuilder,否则存入第二个StringBuilder。最后拼接两个字符串。时间复杂度0(N),空间原创 2015-03-12 09:26:11 · 1237 阅读 · 0 评论 -
刷leetcode:Roman to Integer/ Integer to Roman
public class Solution { public int romanToInt(String s) { /** Map hashMap=new HashMap(); hashMap.put("I",1); hashMap.put("V",5); hashMap.put("X",10); hashMap.put("L",50); hashM原创 2015-02-12 10:42:27 · 306 阅读 · 0 评论 -
刷leetcode:Reverse Integer
public class Solution { public int reverse(int x) { int sign=1; if(x<0){ sign=-1; } StringBuilder intStr=new StringBuilder(String.valueOf(x*sign));原创 2015-02-12 10:37:59 · 282 阅读 · 0 评论 -
刷leetcode:Majority Element
public class Solution { public int majorityElement(int[] num) { if(num.length==1) return num[0]; /** delete different elements Time:O(n) Space:O(n) Integer[] numB原创 2015-02-12 10:48:16 · 427 阅读 · 0 评论 -
刷leetcode:String to Integer (atoi)
public class Solution { private static final int maxDiv10 = Integer.MAX_VALUE / 10; public int atoi(String str) { if(str.isEmpty()){ return 0; } int MAX原创 2015-02-12 10:39:01 · 265 阅读 · 0 评论 -
刷leetcode:Two Sum
题号:1public class Solution { public int[] twoSum(int[] numbers, int target) { int[] result=new int[2]; HashMap map = new HashMap(); int[] result = new int[2]; for (int i原创 2015-02-12 10:22:58 · 357 阅读 · 0 评论