Package test;/*** Implement the moving algorithm before and after the positive and negative elements of the array (the division line is 0 and can be any other number)* Move a negative number forward to the front of the array, and place a positive number behind it.* @ Author hao**/Public class MoveNumTest {/*** @ Param args*/Public static void main (String [] args ){Int [] nums = {342,6, 3453645,-324, 1, 6,-4,645,-3534, 34,-3,345,989 56,-6 };MoveNumTes
Given an unsorted array nums, reorder it such, nums[0] ] .... Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2 does it in O (n) Time and /or in-place with O (1) extra space?This problem should be hard,
Problem Definition:Rotate an array of n elements to the right by K steps.For example, with n = 7 and k = 3, the array is [1,2,3,4,5,6,7] rotated to [5,6,7,1,2,3,4] .Note:Try to come up as many solutions as can, there is at least 3 different ways to solve this problem.Hint:Could do it in-place with O (1) extra space?Solution 1: The simplest and most brutal, each time the right to move a hole, move K times. The spatial complexity is O (1), and then soft and timed out.1 defRotate (
Suppose a sorted array is rotated on some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).You is given a target value to search. If found in the array is return its index, otherwise return-1.Assume no duplicate exists in the array.This paper uses the idea of binary algorithm, to determine whether the target is in the first half or the second half, and then separate search. Time: 7ms. The code is as follows:classSolution { Public: intSearch (vectorint>
question, pseudo code has two for loop, complexity is not O (
2
?)? No, the complexity is still O (n), which can be explained by the averaging analysis: if the exchange condition is met, each time an element is in the correct position, because there is a total of n elements, so it is necessary to n-1 the exchange (n-1 elements, the nth element is automatically satisfied) so that all elements in the correct position , that is, the while loop executes at most O (n) times,
first item of an array, followed by the complete array of all itemsThe ① implements the traversal of the array, and can use loopsThe ② loop variable starts at 0 and then takes the maximum subscript of the array (the length of the array-1)③ in the loop body, the value of each item of the array can be removed by using the loop body variable as the subscript{}Full implementation CodeConsole.Write ("The length of the Pro input array:");int len =Int, Parse (Conesole,readline ());//Creates an array b
referenced in the official documentation, not listed here.
4. General sequence operation (method)
From lists, tuples, and strings, you can "abstract" some of the public common methods of a sequence (not what you would think of as crud), including indexing (indexing), fragmentation (sliceing), plus (adding), Multiply (multiplying) and check whether an element belongs to a member of a sequence. In addition, there are built-in functions such as calculating the sequence length, the maximum minimu
title : Matrix 0Difficulty : Easytopic content :Given a set of distinct integers, nums, return all possible subsets (the power set).Note:the solution set must not contain duplicate subsets.translation :Given a different set of integers, nums, returns all possible subsets (both the Empty and self).Note: The solution set cannot contain duplicate subsets.Example:Input:nums = [1,2,3]output:[ [3], [1], [2], [],
Rotate an array of n elements to the right by K steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated [5,6,7,1,2,3,4] to.Note:Try to come up as many solutions as can, there is at least 3 different ways to solve this problem.Hint:Could do it in-place with O (1) extra space?Related Problem:reverse Words in a String IICredits:Special thanks to @Freezen for adding this problem and creating all test cases.Solution 1: Use an extra copy space. Time Complexity:o (n). Space com
sortdefBubble_sort_easy (nums:list): forIinchRange (len (nums)):#The index of the nested two-layer loop starts at 0, so the back is Len (nums)-i-1 forJinchRange (len (nums)-i-1): ifNUMS[J] > nums[j + 1]: nums[j], nums
Closures are functional self-contained modules that can be passed and used in code. Closures in Swift are similar to those in the blocks in C and Objective-c and Lambdas in other programming languages.There are three main types of closures:1. A global function is a closed packet that has a name but does not capture any value2. A nested function is a closure that has a name and can capture the values in its enclosing function domain3. A closure expression is a non-named closure that is written wi
Topic:Given an array with n objects colored red, white or blue, sort them so, objects of the same color is Adjacen T, with the colors in the order red, white and blue.Here, we'll use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You is not a suppose to use the library's sort function for this problem.Click to show follow up.Follow up:A rather straight forward solution is a two-pass algorithm using counting sort.First, iterate the array counting number of
operation
set/a var= 2 "set/a var= 4 ">>" 2 this doesn't quite remember the term in math ....
These symbols can also be used in = ^= |= set/a var "=" 1 equals set/a var =%var% "" 1 Note quotes
4. Set with parameter/p waits for user inputWindows 2000 and Windows are not choice this command, choice waiting for user input function is to be implemented through the set/p
The demo is as follows:
Copy Code code as follows:
@echo off
set/p str= Please enter the character
A peak element is an element, which is greater than its neighbors.Given an input array where num[i] ≠ num[i+1] , the find a peak element and return its index.The array may be contain multiple peaks, in this case return the index to any one of the peaks is fine.May imagine num[-1] = num[n] = -∞ .For example, in array [1, 2, 3, 1] , 3 is a peak element and your function should return the index number 2.Note:Your solution should is in logarithmic complexity.Problem Solving Ideas:In the tip of the t
in the main function to see if we can get the first three elements of the nums array:int Main (intChar *argv[]) { int *nums = getData (); printf ("%d,%d,%d\n", nums[0],nums[1],nums[2 ]); return 0 ;}As shown in the running results, we find that it is OK to return a
number. For example, math. sqrt (4) returns 2 + 0j
2.4 list
A list is the data that can be like a chain. It is a continuous data link;2.4.1 initialization list
List = ['pyu', 'aiyq', 'wgj', 'yb ', 2342,3432]
Nums = [,]2.4.2 Element
A list is generally a set that contains multiple data. Therefore, each data unit contained in a list is called an element.
Just as we put the names of a class in descending order into a roster;
The roster is a list, and ea
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.