Design and implement a data structure for Least recently Used (LRU) cache. It should support the following operations: get and set .get(key)-Get The value ('ll always be positive) of the key if the key exists in the cache, otherwise return-1.set(key, value)-Set or insert the value if the key is not already present. When the cache is reached its capacity, it should invalidate the least recently used item before inserting a new item.Idea: LRU here involves the use of vectors to record memory addre
Tenth lineHow would you print just the 10th line of a file?For example, assume. has the file.txt following content:Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10Your script should output the tenth line, which is:Line 10[Show hint]Hint:1. If The file contains less than ten lines, what should do you output?2. There ' s at least three different solutions.Try to explore all possibilities. https://leetcode.com/problems/tenth-line/
The Magic of awkawk ' nr==10 ' file. txt[
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return-321Problem Solving Ideas:It is not difficult to flip a number, can be turned into a string type flip, can also be flipped, the main problem involves the boundary and overflow problems, using a long or biginteger can be solved.The topic is not difficult:The Java implementation is as follows: Public classSolution {Static Public intReverseintx) {if(x==0| | x==-2147483648)return0; BooleanIsnagetive=false; if(x) {is
Given A collection of candidate numbers (C) and a target number (T), find all unique combinations in
c where the candidate numbers sums to
T.
Each number in C is used once in the combination.Note:
All numbers (including target) would be positive integers.
Elements in a combination (a1, a 2, ..., aK) must is in non-descending order. (ie, a1≤ a2≤ ... ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set and 10,1,2,7
Title Description:Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.Problem Solving Ideas:The title means to synthesize an ordered list of two ordered linked lists.Add to the new list by comparison.The code is as follows:public static ListNode mergetwolists (ListNode L1, ListNode L2) {ListNode list = new ListNode (0); ListNode tmp = list;while (L1! = NULL | | L2! = NULL) {if (L1 = = null) {Tmp.next = new L
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Problem solving idea One:By observing the situation of n=2 and n=3 you can know that as long as the string at the beginning of the n=2, the end, ' (' Insert ' () ', attention to prevent duplication.The Java implementation is as follows:static public listTwo ways to solve problems:It can be observed that
Slow.next2. Dummy head application: Dummy Head is we create a new node, and then let the original head node to the back of the new node. I think using the dummy head with three more obvious benefits.One is to minimize the changes to the original linked list, if the head = Head.next Such a method, head will move, so change the original situation, not very good.Two benefits are convenient to return, directly return to Dummy.next can be.Three is not the right to do special treatment, if the deleti
Given A linked list, reverse the nodes of a linked list K at a time and return its modified list.If the number of nodes is not a multiple of K then left-out nodes in the end should remain as it is.You may not alter the values in the nodes, and only nodes itself is changed.Only constant memory is allowed.For example,Given This linked list:1->2->3->4->5For k = 2, you should return:2->1->4->3->5For k = 3, you should return:3->2->1->4->5Problem Solving Ideas:Similar to the k=2 situation, in view of
Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / 2 2/\/3 4 4 3But the following are not: 1 / 2 2 \ 3 3Problem Solving Ideas:Overload a issymmetric to determine whether two trees are symmetrical, Java implementations are as follows: public Boolean issymmetric (TreeNode root) { if (root==null) return true; Return Issymmetric (root.left,root.right); } public
Problem Solving Ideas:Middle sequence traversal, Zuozi-root node-right subtreeThe Java implementation is as follows: Public listJava for Leetcode 094 Binary Tree inorder traversal
Issue: Remove duplicates from Sorted Array II Difficulty: MediumFollow up for "Remove duplicates":What if duplicates is allowed at the most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].Answer:Two ways of thinking:(1) Two reference pointers, step back, and when the 3rd is equal to the first 2, skip(2) A reference pointer, which is compared with 3rd and 1th, when equal, skips; unequal assignmentIt is obvious that (1) is eas
Thought:Sum the squares of each number of bits for the input dataTo get the result, if it's 1, return to the real, or the result is recursive.When to return a fake:Return false instructions into the infinite loop.When will there be infinite loops?The result of a one-time squared sum, which has been obtained before, is infinitely cyclic.So, I save the results every time I get it, and if I find one that gets the results before, then I'm sure it's an infinite loop. return False1 classSolution:2
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.