算法
为不为
这个作者很懒,什么都没留下…
展开
-
java算法(1)
原题:// Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.// Note: The input string may contain letters other than the parentheses ( an原创 2017-10-18 08:07:21 · 180 阅读 · 0 评论 -
java算法(2)
原题:// Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.// Do not allocate extra space for another array, you must do this in place原创 2017-10-18 08:08:03 · 143 阅读 · 0 评论 -
java算法(3)
原题:// Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.// For example,// Given [[0, 30],[5, 1原创 2017-10-18 08:11:02 · 213 阅读 · 0 评论 -
java算法(4)
原题:// Implement a trie with insert, search, and startsWith methods.// Note:// You may assume that all inputs are consist of lowercase letters a-z.// Your Trie object will be instantiated and called as原创 2017-10-18 08:12:26 · 198 阅读 · 0 评论 -
java算法(5)
原题:// Given an array of strings, group anagrams together.// For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], // Return:// [// ["ate", "eat","tea"],// ["nat","tan"],// ["bat"]/原创 2017-10-18 08:13:57 · 193 阅读 · 0 评论 -
java算法(6)
原题:// Given a nested list of integers, implement an iterator to flatten it.// Each element is either an integer, or a list -- whose elements may also be integers or other lists.// Example 1:// Given t原创 2017-10-18 08:14:37 · 185 阅读 · 0 评论 -
java算法(7)
原题:// A message containing letters from A-Z is being encoded to numbers using the following mapping:// 'A' -> 1// 'B' -> 2// ...// 'Z' -> 26// Given an encoded message containing digits, determine t原创 2017-10-18 08:15:27 · 162 阅读 · 0 评论 -
java算法(8)
原题:// The count-and-say sequence is the sequence of integers beginning as follows:// 1, 11, 21, 1211, 111221, ...// 1 is read off as "one 1" or 11.// 11 is read off as "two 1s" or 21.// 21 is read o原创 2017-10-18 08:16:08 · 174 阅读 · 0 评论 -
java算法(9)
原题:// Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.// Example:// nums = [1, 2, 3]// target = 4/原创 2017-10-18 08:16:53 · 169 阅读 · 0 评论 -
java算法(10)
原题:// Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.// Note: // A word cannot be split into原创 2017-10-18 08:19:40 · 216 阅读 · 0 评论 -
java题(1)
趣味题输入一个数字,当它转为二进制时,它含有几个1。并返回。 如:输入1 返回1。因为二进制为1输入2 返回1。因为二进制为10输入3 返回2。 因为二进制为11输入7 返回3。因为二进制为111解法1: public int countBitsToOneBasedOnString(int n) { int result = 0; String binaryNumber原创 2017-10-19 08:38:44 · 169 阅读 · 0 评论 -
java快速排序
快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。public abstract class SortingAlgorithm { public abstract voi原创 2017-10-19 09:03:17 · 175 阅读 · 0 评论 -
动态规划(1)
原题:/** * Created by gouthamvidyapradhan on 17/03/2017. * Say you have an array for which the ith element is the price of a given stock on day i. * <p> * If you were only permitted to complete at mo原创 2017-10-19 09:34:32 · 207 阅读 · 0 评论 -
动态规划(2)
原题:/** * Created by gouthamvidyapradhan on 04/07/2017. * In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to原创 2017-10-19 09:36:05 · 167 阅读 · 0 评论 -
动态规划(3)
原题:/** * Created by gouthamvidyapradhan on 01/04/2017. * You are climbing a stair case. It takes n steps to reach to the top. * <p> * Each time you can either climb 1 or 2 steps. In how many distin原创 2017-10-19 09:36:43 · 183 阅读 · 0 评论 -
动态规划(4)
原题:/** * Created by gouthamvidyapradhan on 23/06/2017. * You are given coins of different denominations and a total amount of money amount. * Write a function to compute the fewest number of coins原创 2017-10-19 09:37:21 · 620 阅读 · 0 评论 -
动态规划(5)
原题:/** * Created by gouthamvidyapradhan on 22/03/2017. * You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make u原创 2017-10-19 09:37:57 · 228 阅读 · 0 评论 -
动态规划(6)
原题:/** * Created by gouthamvidyapradhan on 12/06/2017. * Accepted * Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words原创 2017-10-19 09:38:38 · 214 阅读 · 0 评论 -
动态规划(7)
原题:/** * Created by gouthamvidyapradhan on 01/04/2017. * A message containing letters from A-Z is being encoded to numbers using the following mapping: * <p> * 'A' -> 1 * 'B' -> 2 * ... * 'Z' ->原创 2017-10-19 09:39:13 · 236 阅读 · 0 评论 -
动态规划(8)
原题:/** * Created by gouthamvidyapradhan on 12/07/2017. * The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. * The dungeon consists of M x N rooms la原创 2017-10-19 09:39:45 · 252 阅读 · 0 评论 -
精通算法系列-最佳买卖股票
给出一个数组,里面的值代表当日股价,你需要选定最佳的时间买入股票,之后卖出股票。以此来获得最佳利润// Example 1:// Input: [7, 1, 5, 3, 6, 4]// Output: 5// Example 2:// Input: [7, 6, 4, 3, 1]// Output: 0public class Solution { public int maxProf原创 2017-10-11 14:38:19 · 405 阅读 · 0 评论 -
精通算法系列-三值更小
原题:// Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.// For example, g原创 2017-10-11 15:46:07 · 225 阅读 · 0 评论 -
精通算法系列-二叉树路径
原题:// Given a binary tree, return all root-to-leaf paths.// For example, given the following binary tree:// 1// / \// 2 3// \// 5// All root-to-leaf paths are:// ["1->2->5", "1->3"]/*原创 2017-10-11 15:55:33 · 254 阅读 · 0 评论 -
动态规划(9)
动态规划: 原题:/** * Created by pradhang on 4/3/2017. * You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping原创 2017-10-19 09:40:28 · 238 阅读 · 0 评论 -
动态规划(10)
原题:/** * Created by pradhang on 7/11/2017. * After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. * Th原创 2017-10-19 09:41:24 · 145 阅读 · 0 评论 -
动态规划(11)
原题:/** * Created by gouthamvidyapradhan on 02/04/2017. * Given an unsorted array of integers, find the length of longest increasing subsequence. * * For example, * Given [10, 9, 2, 5, 3, 7,原创 2017-10-19 09:41:59 · 197 阅读 · 0 评论 -
动态规划(12)
原题:/** * Created by gouthamvidyapradhan on 24/02/2017. * Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. * * Example: *原创 2017-10-19 09:42:38 · 175 阅读 · 0 评论 -
动态规划(13)
原题:/** * Created by gouthamvidyapradhan on 27/03/2017. * Given an unsorted array of integers, find the length of longest increasing subsequence. * * For example, * Given [10, 9, 2, 5, 3, 7,原创 2017-10-19 09:43:03 · 141 阅读 · 0 评论 -
懒人读算法(一)-寻找名人
趣味题:假设一场聚会有n个人,他们中间可能存在一个名人。名人的定义是在场的人都认识他,而他却不认识所有人。 已知条件: 1.函数knows(a,b)告诉你a是否认识b,返回bool值你要做的事:确定这场聚会是否有名人的存在,如果不存在则return -1答案:public class Solution extends Relation { public int findCelebr原创 2017-10-11 16:47:22 · 1335 阅读 · 0 评论 -
动态规划(14)
原题:/** * Created by gouthamvidyapradhan on 02/04/2017. * Find the contiguous subarray within an array (containing at least one number) which has the largest product. * * For example, given t原创 2017-10-19 09:43:34 · 161 阅读 · 0 评论 -
动态规划(15)
原题:/** * Created by gouthamvidyapradhan on 07/07/2017. * Find the contiguous subarray within an array (containing at least one number) which has the largest sum. * * For example, given the ar原创 2017-10-19 09:43:58 · 160 阅读 · 0 评论 -
动态规划(16)
原题:/** * Created by pradhang on 4/3/2017. * Given a string s, partition s such that every substring of the partition is a palindrome. * * Return the minimum cuts needed for a palindrome parti原创 2017-10-19 09:44:35 · 153 阅读 · 0 评论 -
动态规划(17)
原题:/** * Created by gouthamvidyapradhan on 19/08/2017. * Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: * * Copy All:原创 2017-10-19 09:45:02 · 193 阅读 · 0 评论 -
动态规划(18)
原题:/** * Created by gouthamvidyapradhan on 07/04/2017. * Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where原创 2017-10-19 09:45:44 · 178 阅读 · 0 评论 -
动态规划(19)
原题:/** * Created by gouthamvidyapradhan on 31/03/2017. * Given n, how many structurally unique BST's (binary search trees) that store values 1...n? * * For example, * Given n = 3, there are原创 2017-10-19 09:46:14 · 151 阅读 · 0 评论 -
动态规划(20)
原题:/** * Created by gouthamvidyapradhan on 31/03/2017. * Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. * * For example, * Given n原创 2017-10-19 09:46:46 · 206 阅读 · 0 评论 -
懒人读算法(二)-细胞问题
细胞问题: 每个细胞有两种状态:1为活细胞,0为死细胞,对于每个位置都满足如下的条件:如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡如果死细胞周围正好有三个活细胞,则该位置死细胞复活public class Solution { public voi原创 2017-10-12 09:36:20 · 558 阅读 · 0 评论 -
懒人读算法(三)-添加新区间
// Example 1:// Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].// Example 2:// Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].// This原创 2017-10-12 09:59:34 · 472 阅读 · 1 评论 -
懒人读算法(四)-寻找最大连续值
趣味题给一个没有排序的整数数组,寻找最大的连续值 如[100, 4, 200, 1, 3, 2] ,则最长的数组是[1, 2, 3, 4],返回值最大长度是4答案:public class Solution { public int longestConsecutive(int[] nums) { int res = 0; HashMap<Integer,原创 2017-10-12 10:31:07 · 509 阅读 · 0 评论 -
懒人读算法(五)-求最大子数组
趣味题给一个数组,求最大的连续子数组的和 如数组:[-2,1,-3,4,-1,2,1,-5,4] 则最大的连续子数组[4,-1,2,1] 和为6答案:public class Solution { public int maxSubArray(int[] nums) { int[] dp = new int[nums.length]; dp[0] = nums[原创 2017-10-12 11:14:10 · 186 阅读 · 0 评论