20: Stack with min function
Title Description
Define the data structure of the stack, implement a min function that can get the smallest element of the stack in the type.
Feel the problem is very boring ... My big JS to do this problem is convenient, but also let me deeply aware of the relative other languages JS inheritance of the power and flexibility ...
var stack = [];function push (node) {Stack.push (node);} function Pop () {Stack.pop ();} function Top () {return stack[stack.length-1];} function min () {return Math.min.call (null,... stack);} /*js do is simple, if you use Java to do have the following idea: The idea: to save data with a stack, with another stack min to save in order to the lowest number of stacks for example, data into the stack, 5, 4, 3, 8, 10, 11, 12, 1 min sequentially into the stack, 5, 4, 3,no,no, No, no, 1 No represents this is not as good as the stack each time the stack, if the elements into the stack than the top of the stack is small or equal to the stack, otherwise than the stack. Import Java.util.stack;public class Solution {stack<integer> data = new stack<integer> (); stack<integer> min = new stack<integer> (); Integer temp = null; public void push (int node) {if (temp! = null) {if (node <= temp) {temp = node; Min.push (node); } data.push (node); }else{temp = node; Data.push (node); Min.push (node); }} public void Pop () {int num = Data.pop (); int num2 = Min.pop (); if (num! = num2) {Min.puSH (num2); }} public int top () {int num = Data.peek (); return num; } public int min () {int num = Min.peek (); return num; }}*/
27: Arrangement of strings
Title Description
Enter a string that prints out all the permutations of the characters in the string in the dictionary order. For example, enter the string ABC and print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c.
Input Description:
Enter a string that is not more than 9 in length (there may be a repetition of characters), and the characters include only uppercase and lowercase letters.
The classic Dfs problem, mainly the exchange of ideas, recursive tree traversal of the first layer (non-root) is a (first position) and all other locations of the exchange, the next layer is the first position is determined, the second position and the rest of the exchange, lower layer ... Push it in turn. In fact, push is the last leaf node, where the leaf node is to the boundary conditions, attention also to the first and their own exchange, the process is not to be push, such as the second layer of ABC or to go back to the third layer of the exchange process ABC can be push to, this is the boundary condition.
Note the biggest pit. In the JS language do not blindly return null or something, mainly to see the topic, if the return of more than an empty array [], a single return 0 or something. This pit for a day .... =-=
function permutation (str) {Let ans = []; Let arr = Str.split (""); if (str.length===0) return []; DFS (ans,arr,0); Ans.map (function (item,index) {//Return Item.join (""); }) ans = ans.filter (function (item,index) {return Ans.indexof (item) ===index; }); return Ans.sort ();} function Dfs (ans,arr,begin) {if (begin = = = Arr.length-1) {//Let TMP = arr; Ans.push (Json.parse (json.stringify (arr))); Let TMP = Arr.join (""); Ans.push (TMP); Ans.push (arr); Console.log (ANS); } for (Let i = begin; I < arr.length; i++) {if (I!== begin && Arr[begin] = = = Arr[i]) con Tinue; Let _arr = Swap (Arr,begin, i); Swap (Arr,begin,i)//Console.log (arr); DFS (ans, arr, begin + 1); Swap (Arr,begin, i); }}function Swap (ARR,A,B) {Let tmp = Arr[a]; Arr[a] = arr[b]; ARR[B] = tmp; return arr;}
28: More than half of the numbers appear in the array
Title Description
If there is a number in the array that appears more than half the length of the array, find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2. Output 0 If it does not exist.
Simple problem, hash, the main note is the JS weak type array processing ....
function MoreThanHalfNum_Solution(numbers){ var len = numbers.length; if(len===0) return 0; let cnt = [],index; for(let i = 0; i < len; i++) { index = numbers[i]; cnt[index]===undefined||NaN? cnt[index]=1 : cnt[index]++; } //console.log(cnt); let max = -1; let cur = 0; let cntlen = cnt.length; for(let i = 0; i < cntlen; i++) { if(!cnt[i]) continue; max = max>cnt[i]? max : (cur = i,cnt[i]) } if(max>(len/2)) { return cur; }else { return 0; }}
Sword Point Offer (20, 21, 22) the stack containing the Min function, the arrangement of the strings, the number of more than half the numbers in the array