Algorithm&DataStructure
文章平均质量分 78
LeaderTech_NJ
做中国最专业的防雷云服务商
展开
-
LinkedList 合并、环的长度、翻转等问题
LinkedList 总结原创 2017-01-01 08:20:16 · 2293 阅读 · 0 评论 -
Last K elements in LinkedList(CC150 2.2)
https://www.hackerrank.com/contests/infinitum15/challenges/k-element-sequences实现一个算法,发现singly linked list中倒数第k个元素;思路1 如果已知linked list 长度length, kth个元素为(length-k); 通过这种方式可以找到这个元素;这种解法明显不是interv原创 2016-09-30 23:42:43 · 640 阅读 · 0 评论 -
CC150 2.8&&Linked List Cycle dection 判断循环LinkedList
https://leetcode.com/problems/linked-list-cycle/《进军硅谷》类似题目:环的长度题目:给出一个单向链表头指针,如果有环,判断环的长度,否则返回0;思路:1 判断链表是否有环。 使用快慢指针,如果快指针已经到尾部,而且他们没有相遇,则无环;如果他们相遇,则有环2 上图为例:慢指针:9->8->7->1;原创 2016-09-30 13:39:22 · 381 阅读 · 0 评论 -
CC150 1.7 ROTATE MATRIX 旋转矩阵
1.7 Rotate MatrixGiven an image represented by an N*N matrix, where each pixel(像素) in the image is 4 bytes, write a method to rotate te image by 90 degrees. Can you do this in place?思路1:1 分层原创 2016-09-30 00:28:28 · 471 阅读 · 0 评论 -
HackRank Stacks: Balanced Brackets;/Leetcode valid-parentheses; 括号匹配
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs原创 2016-09-29 23:05:21 · 785 阅读 · 0 评论 -
Leetcode 102 Binary Tree Level Order Traversal
题目:https://leetcode.com/problems/binary-tree-level-order-traversal/九章算法答案:http://www.jiuzhang.com/solutions/binary-tree-level-order-traversal/思路:Queue使用时要尽量避免Collection的add()和remove()方原创 2016-10-15 07:48:28 · 250 阅读 · 0 评论 -
CC150 1.9 string rotation
1.9 string rotation假定已知isSubstring(),可以判断s2是否是s1的字串; 定义:rotation, 旋转;举例:s1=waterbottle,x=waty=erbottles2=erbottlewats1, s2是rotation关系问题:能否使用一次isSubstring来判断rotation关系?思路原创 2016-09-29 05:54:51 · 339 阅读 · 0 评论 -
CC150 1.6 String Compression
1.6 String Compression Implementing this method to perform basic string compression using the counts of repeated characters. For example, aabcccccaaa, 变成: a2b1c5a3if compressed string would not be原创 2016-09-29 03:38:08 · 362 阅读 · 0 评论 -
CC150 1.5 One Away
Three types of edits that can be performed on strings: insert character, remove character, or replacecharacter.Given 2 strings, write a function to check if they are one edit(or 0 edits) away.E原创 2016-09-29 01:26:59 · 443 阅读 · 0 评论 -
CC 150 1.4 Palindrome Permutation
Given a string , write a function to check if it is a permutation of a palindrome. 给定字符串,写方法检查是否是回文的排列;https://www.interviewcake.com/question/java/permutation-palindrome回文:单词或短语,正序和倒叙都是相同的;比如:原创 2016-09-28 23:56:11 · 618 阅读 · 0 评论 -
Leetcode 160. Intersection of Two Linked Lists/CC150 2.7
https://leetcode.com/problems/intersection-of-two-linked-lists/Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two lin原创 2016-10-04 03:34:14 · 321 阅读 · 0 评论 -
CC150 3.1 Three in one: describe how you could use a single array to implement 3 stacks.
fixed Division思路:1 把数组等分;2 允许stack在其中增长;stack0 [0,n/3)stack1 [n/3,2n/3)stack2 [2n/3,n)class FixedMultiStack { int numberOfStacks = 3; int stackCapacity;原创 2016-10-06 05:08:48 · 451 阅读 · 0 评论 -
算法--动态规划篇
Dynamic Programming(动态规划)简介:1)需要辅助空间,记录子问题解决结果。2)明确各个子问题之间的关系,避免重复计算3)通常对数组和字符串的高难度面试题有效。与分治算法比较类似,但不同的是分治算法把原问题划归为几个相互独立的子问题,从而一一解决,而DP则是针对子问题有重叠的情况的一种解决方案。思路:利用recursive method原创 2017-02-22 08:56:01 · 383 阅读 · 0 评论 -
int MaxWindowTotal(int[] values, int windowSize)
原题:MaxWindowTotal will take an array of integers and a windowSize, and return the maximum total of any consecutive sequence of integers of length windowSize.For example, given (array = [5, 12,原创 2017-02-21 04:40:28 · 515 阅读 · 0 评论 -
算法--全排列、全子集、DFS\BFS问题
排列组合子集合 subset(medium)输入一个含有不同数字的序列,输出所有可能的集合(含空集)。要求1 集合里元素有序排列;2 输出结果不含有重复集合;举例:输入{1,2,3], 输出{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}原题:https://leetcode.com/problems/subsets/思路图:Amor原创 2017-01-18 11:18:53 · 2629 阅读 · 1 评论 -
206. Reverse Linked List
原题:https://leetcode.com/problems/reverse-linked-list/内容:Reverse a singly linked list.A linked list can be reversed either iteratively or recursively. Could you implement both?总思路图:原创 2016-10-19 04:46:46 · 313 阅读 · 0 评论 -
字符串String 算法题
1 字符判重 isUniqueChar(string str)要求:输入字符串,判断该串是否不含有重复字符需要确认问题:encoding是UTF-8还是ASCII码?思路:使用hashtable记录字符出现情况;如果已经存在,就说明有重复;如果是ASCII,使用256位数组即可;UTF8(万国码)编码的解释:http://baike.baidu.com/item/UTF原创 2017-01-06 09:03:13 · 693 阅读 · 0 评论 -
CC150 3.4 Queue via Stacks
queue via stacks: implement a my Queue class which implements a queue using 2 stacks.通过2个stack实现 MyQueue类public class MyQueue { Stack stackNew, stackOld; public MyQueue原创 2016-10-08 04:59:39 · 437 阅读 · 0 评论 -
242. Valid Anagram
原题链接:https://leetcode.com/problems/valid-anagram/Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat原创 2016-10-19 09:56:00 · 264 阅读 · 0 评论 -
CC150 2.6/Leetcode Palindrome LinkedList
判断 Linked List是否属于回文思路:迭代法+快慢指针快指针是慢指针速度的两倍;1 while循环,fast到tail,slow只到一半;2 slow内容均迭代放入stack3 如果linkedlist元素个数是奇数(fast!=null), slow需要前移1 位4 然后对比stack 的顶元素与slow指针指向的内容; 不等-> 返回false; 相等原创 2016-10-03 22:45:25 · 355 阅读 · 0 评论 -
Leetcode 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cov原创 2016-10-06 04:05:40 · 234 阅读 · 0 评论 -
cc150 1.2 Check Permutation(排列)
1.2 Check Permutation(排列): Given 2 strings, write a method to decide if one is a permutation of the other待确认问题:1 是否大小写敏感?2 空格是否可以忽略?思路:1 排序2 判断是否相等C#代码:O(NLOGN) public sta原创 2016-09-28 10:19:21 · 736 阅读 · 0 评论 -
Leetcode 155 Min Stack
链接:https://leetcode.com/problems/min-stack/题目要求:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stac原创 2016-10-14 10:39:38 · 328 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2016-10-23 00:33:08 · 311 阅读 · 0 评论 -
Leetcode 8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2016-09-23 12:16:13 · 336 阅读 · 0 评论 -
LeetCode - 5 Longest Palindromic Substring 最长回文子字符串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.Subscribe to see w原创 2016-09-21 23:03:48 · 323 阅读 · 0 评论 -
Leetcode2 Add Two Numbers
原题链接:https://leetcode.com/problems/add-two-numbers/给出两个整数,每个整数由一个链表表示。其中链表每个节点保存数的数位,这些数位反序存在链表中,即高位在最后,低位在最前面。返回一个链表代表这两个数之和。举例,如图:思路:由于反序存储在链表中,可以从表头开始顺序相加;两个链表长度不同,如何相加链表而没有剩余原创 2016-10-10 23:47:29 · 231 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
https://leetcode.com/problems/validate-binary-search-tree/Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a原创 2016-10-09 11:37:02 · 284 阅读 · 0 评论 -
Leetcode 236. Lowest Common Ancestor of a Binary Tree
原题:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/题目大意:已知一个BT,找到BST中的节点p+q的最小公共祖先;例子: _______3______ / \ ___5__ ___1__ /原创 2016-10-10 04:02:54 · 299 阅读 · 0 评论 -
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/题目大意:已知一个BST,找到BST中的节点p+q的最小公共祖先;思路:递归1 boundary conditions: tree, p, q任意为null, 返回null2 如果p.va, q.val均小于root.val ->原创 2016-10-10 03:24:30 · 240 阅读 · 0 评论 -
CC150 4.6 Successor
Successor: write an algorithm to find the "next" node(i.e. in-order successor) of a given node in a binary search tree. you may assume that each node has a link to its parent已知BST中的一个node, 写算法找到下一个节原创 2016-10-10 01:38:46 · 269 阅读 · 0 评论 -
CC150 4.4 Check Balanced; Leetcode 110. Balanced Binary Tree
Leetcode link: https://leetcode.com/problems/balanced-binary-tree/Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined asa bin原创 2016-10-09 09:38:55 · 524 阅读 · 0 评论 -
Leetcode 合并有序数组 Merge 2 arrays in 1 array
《进军硅谷》原题:合并有序数组给定两个有序数组A和B, 合并B到A, 结果保持有序。假设A有足够空间容纳B。初始时,A、B元素各有m和n个;分析:数组插入新元素没有链表那么方便,因为插入一个新元素会导致插入后的所有元素的移动,为了避免移动,通常从后向前插入,即反向插入;思路:1 找到原有数组A和B的最后一个元素;2 逐个把较大的值放在A数组的m+n-1位置向原创 2016-09-26 12:11:40 · 352 阅读 · 0 评论 -
HackerRank Find first repeating letter in a string
题目:Find first repeating letter in a string. 比如输入“abcba”, 返回“a”思路:找出第1个元素,然后判断剩下的字符串是否包含第1个元素;找到第2个元素,然后判断剩下的字符串是否包含第2个元素; 以此类推……有则返回charArray[i]时间复杂度:O(N)C#代码: publi原创 2016-09-26 11:10:10 · 700 阅读 · 0 评论 -
CC150 1.3 ReplaceSpaces
Write a method to replace all spaces in a string with '%20', You may assume that the string has sufficient space at the end to hod the additional characters, and you are given "true" length of the str原创 2016-09-28 12:11:25 · 331 阅读 · 0 评论 -
判断是否有重复字符 bool IsUniqueChars( String str) CC150 1.1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you connot use additional data structures?询问 是ASCII 还是 UNICODE?ASCII ,ASCII码由一个字节中的7位(bit)表示,范围是0x00原创 2016-09-28 09:57:09 · 1227 阅读 · 2 评论 -
Leetcode 15 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain原创 2016-10-12 05:45:18 · 287 阅读 · 0 评论 -
Implement CircularArray class 实现循环数组
题目内容Circular Array: Implement a circular array class that supports an array-like data structure which can be efficiently rotated. If possible, the class should use a generic type, and support iterat原创 2016-09-27 12:34:48 · 299 阅读 · 0 评论 -
LeetCode 328. Odd Even Linked List Java
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-01-20 11:22:51 · 761 阅读 · 0 评论 -
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2016-02-11 22:52:51 · 347 阅读 · 0 评论