
算法
Promise_kk
这个作者很懒,什么都没留下…
展开
-
地牢逃脱(BFS)
题目描述 给定一个 n 行 m 列的地牢,其中 ‘.’ 表示可以通行的位置,’X’ 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。 输入描述: 每个输入包含原创 2017-08-23 11:32:31 · 218 阅读 · 0 评论 -
字典树解决句子相似性问题
【问题描述】给定一个段落,由 N 个句子组成。第 i 个句子的长度为 L[i],包含的单词个数为 W[i]。 句子不包含任何除字母和空格( ) 外的符号。 每个句子内部,含有若干个单词,由空格( ) 分隔。句子不会包含连续的空格。 随后给定 M 个查询,每个查询包含一个句子,需要在段落中寻找相同单词数量最多的句子。重复的单词只计一次,且不区分大小写。 输入数据将保证结果是存在且唯一的。 输原创 2017-08-04 17:22:53 · 289 阅读 · 0 评论 -
Trie树解决字典中查找单词问题
package com.kai.util;import java.util.HashSet;/** * Created by Administrator on 2017/8/4. */public class TrieTree { Node root=new Node(); private class Node{ private Node[] child=ne原创 2017-08-04 12:50:51 · 215 阅读 · 0 评论 -
迷宫问题
1.问题描述: 小青蛙有一天不小心落入了一个地下迷宫,小青蛙希望用自己仅剩的体力值P跳出这个地下迷宫。为了让问题简单,假设这是一个n*m的格子迷宫,迷宫每个位置为0或者1,0代表这个位置有障碍物,小青蛙达到不了这个位置;1代表小青蛙可以达到的位置。小青蛙初始在(0,0)位置,地下迷宫的出口在(0,m-1)(保证这两个位置都是1,并且保证一定有起点到终点可达的路径),小青蛙在迷宫中水平移动一个单位距原创 2017-08-08 17:29:58 · 842 阅读 · 0 评论 -
数组中求俩个数,三个数,K个数和为给定sum
给定一个数组,一个数sum,在数组中快速找到俩个数和为sum。 给定一个数组,一个数sum,在数组中快速找到三个数和为sum。 给定一个数组,一个数sum,求数组中可以找到多少种组合使得和为sum。不限制组合里面数的个数。package com.liu.common;/** * Created by kaizige on 2017/8/5. */import java.util.*;pu原创 2017-08-08 14:30:22 · 3318 阅读 · 0 评论 -
自定义类的比较
1.题目介绍: 某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额最大 2.输入描述: 输入包括m+2行。 第一行两个整数n(1 <= n <= 50000),m(1 <= m <= 50000) 第二行为n个参数a,即每个桌子可容纳的最大人数,原创 2017-08-08 09:41:31 · 440 阅读 · 0 评论 -
N个环形加油站环绕一圈到起点
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next原创 2017-06-03 15:22:43 · 757 阅读 · 0 评论 -
N个孩子站一行分糖
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one can原创 2017-06-03 15:10:32 · 395 阅读 · 0 评论 -
整数数组中找出只出现了一次的数
Given an array of integers, every element appears three times except for one. Find that single one.public class Solution { public int singleNumber(int[] A) { int ones=0,twos=0,threes=0;原创 2017-06-03 14:59:29 · 335 阅读 · 0 评论 -
复制带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.public class Solution { public Ra原创 2017-06-03 14:33:58 · 275 阅读 · 0 评论 -
字符串插空来满足每个词都是单词集中的词
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s =”catsan原创 2017-06-03 14:27:08 · 243 阅读 · 0 评论 -
链表检测是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.public class Solution { public ListNode detectCycle(ListNode head) { if(head==null||head.next==原创 2017-06-03 14:03:01 · 264 阅读 · 0 评论 -
调整列表顺序
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes’ values. For example, Given{1,2,3,4}, reorder it to{1,4,2,原创 2017-06-03 11:10:34 · 2421 阅读 · 0 评论 -
使用插入排序方法排序列表
public class Main { public ListNode insertionSortList(ListNode head) { ListNode h=new ListNode(0); h.next=head; while(head!=null&&head.next!=null){ if(head.val原创 2017-06-03 10:47:05 · 484 阅读 · 0 评论 -
用归并排序列表(常数空间复杂度)
Sort a linked list in O(n log n) time using constant space complexity.public class Solution { public ListNode sortList(ListNode head) { if(head==null||head.next==null){ ret原创 2017-05-22 00:02:21 · 725 阅读 · 1 评论 -
大整数排序
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); while(s.hasNext()){原创 2017-06-03 10:24:47 · 215 阅读 · 0 评论 -
N阶楼梯上楼问题
题目:一次可以走两阶或一阶,问有多少种上楼方式。(要求采用非递归) import java.util.*; public class Main { public static long get(int n){ if(n<=1){ return n==1?1:0; } long[] a=new l原创 2017-06-03 10:15:38 · 613 阅读 · 0 评论 -
KMP算法
在长度为M字符串S1中找到长度为N字符串S2的匹配位置,没有找到返回-1。 使用KMP算法可以在O(M+N)时间复杂度完成。import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Arrays;import java.util.Scanner;public class Main { publ原创 2017-08-27 11:56:11 · 183 阅读 · 0 评论