11.String to int, which is atoi function implementation.The main considerations are as follows:1. String is empty2. There are non-numeric characters in string, such as white space characters, ABCD, etc.3. The positive or negative stringCode:public class Test {public static int atoi (String str) {if (str = = NULL | | str.length () 12. Merging ordered arraysGiven two arrays A and B, B is combined into aCode:public void merge (int a[], int m, int. b[], i
1. Find the maximum, minimum, and average values of an array element2. Copying and inversion of arraysScenario 1:Scenario 2: (How to implement Replication)Inverse of the array:Inverse of an array elementfor (int i = 0;i int temp = Arr[i];Arr[i] = arr[arr.length-1-i];Arr[arr.length-1-I] = temp;// }for (int x = 0, y = arr.length-1; x int temp = arr[x];ARR[X] = Arr[y];Arr[y] = temp;}Extension: String str = "ABCDEFG";Sort the array:Insert SortDirect Insert Sort, binary insert sort, shell sortExchang
68. Arrange the array to the smallest number.Title: Enter an array of positive integers, concatenate them into a number, and output the smallest of all the numbers that can be drained.One.For example, the input array {32, 321}, the output of these two can be ranked as the smallest number 32132.Please give an algorithm to solve the problem, and prove that the algorithm1 PackageCom.rui.microsoft;2 3 Importjava.util.Arrays;4 ImportJava.util.Comparator;5
Algorithm exercise: remove duplicates from ordered arrays with two pointersProblem description
An ordered array is provided. duplicate elements are removed in the local area, so that each element appears only once and the length of the new array is returned.
Problem Analysis
This is relatively simple. You can directly use two pointers, one in front and one in the back, to scan the array again. The time comp
Knapsack algorithm Exercise-------------find the maximum of the array smaller than a number: var bests = {val:0,str: ""};var LIMIT; Array.prototype.sum = function () {var s = 0;for (var i = 0;i Knapsack algorithm Exercise------------find the maximum and the number of arrays:
Auxiliary classesIn several classical sorting algorithm learning section, for the convenience of unified testing of different algorithms, a new auxiliary class, the main function is: to produce a specified length of the random array, provide a printout array, exchange two elements and other functions, the code is as follows:functionarraysortutility (numofelements) { This. Dataarr = []; This. pos = 0; This. numofelements =numofelements; This. Insert
Scene
The original post list A, now need to promote the new business B in a, you need to be in a list of 1:1 mixed B data, randomly mixed, but need to keep A and B two lists of the original data sorting. Refer to the effect of the example below.
Principle
Know the total number of elements n;
For Loop n times, take random number;
Gets the value of a or b from the beginning of the random number and pushes it into the new array;
Code:
//随机合并两个数组元素,保持原有数据的排序不变(即各个数组的元素在合并后的数组中排序与自身原
you should create child elements and add content before binding the child elements of the Parent element}}functionBtnhandle () {varAqidata =GetData (); Aqidata=Sortaqidata (Aqidata); Render (Aqidata);}functioninit () {varButton=document.getelementbyid ("Sort-btn"); Button.addeventlistener ("Click", Btnhandle); //under this, bind a click event to SORT-BTN and trigger the Btnhandle function when clicked.}init ();Code exercises (definition of two-dimensional a
This question is similar to the previous one, how to do not repeat printing?First, ensure that the first value in the selected ternary array is not duplicated, and that the rest is the same as the previous question. PackageTT; Public classTest69 { Public Static voidPrintuniquetired (int[] arr,intk) { if(arr==NULL|| Arr.length){ return; } for(inti = 0; i){ if(i==0 | | arr[i]! = arr[i-1]) {printrest (arr, I, I+1, Arr.length-1, KArr[i])
14. Implement STRSTR (): Searches for the first occurrence of a string in another stringCases:#include Output: SThe main consideration here is the special case.Code:public class Test {public static string StrStr (string origin, String needle) {int originlen = origin.length (); int Needlele n = needle.length (), if (Needlelen = = Originlen Originlen = = 0) return "", if (Needlelen = = 0) return origin;for (i NT i = 0; i 16. Find the insertion position: given an ordered array and a target value,
() {returnRemove (size-1); } /*** Delete an element in an array *@paramelement*/ Public voidRemoveelement (intElement) { //first determine if the element exists intindex =find (Element); if(Index! =-1) {remove (index); } } //gets the element that corresponds to the index intGetintindex) { if(Index size) { Throw NewIllegalArgumentException ("Get fail, index is illegal"); } returnData[index]; } //change the value of the corresponding eleme
20. Looking for a 2D matrixGiven a m*n matrix that increments from left to right from top to bottom, determine if Target is in the matrixCases:[[1, 3, 5, 7],[10, 11, 16, 20],[23, 30, 34, 50]]Target=3Return: TrueIdea: two-point searchCode:public class Test {public static Boolean Searchmatrix (int[][] matrix, int target) {if (Matrix = = NULL | | matrix.length = = 0 | | Matrix[0].length = = 0) return false;int m = matrix.length;int n = matrix[0].length;int begin = 0;int end = m * N-1;while (Begin I
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.