array

剑指offer_1.24_Day_4

不羁的心 提交于 2020-01-24 05:00:55
构建乘积数组   给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。 思路:就是每一行的乘积中没有A[i]。 import java.util.ArrayList; public class Solution { public int[] multiply(int[] A) { int len = A.length; int[] B = new int[len]; for(int i=0;i<len;i++) { B[i]=1; for(int j=i+1;j<len;j++) B[i]*=A[j]; } for(int i=0;i<len;i++) { for(int j=i-1;j>=0;j--) B[i]*=A[j]; } return B; } } 数组中重复数字   在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。 思路:有点迷,暴力求解,从前到后,相同的保存并返回True,检索后没有则false;

lintcode-14-二分查找

旧城冷巷雨未停 提交于 2020-01-23 22:45:56
二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。 样例 在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2。 挑战 如果数组中的整数个数超过了2^32,你的算法是否会出错? 标签 二分法 数组 说明 普通的二分查找,数组中整数超过2^32会导致算法出错。 code class Solution { public: /** * @param nums: The integer array. * @param target: Target number to find. * @return: The first position of target. Position starts from 0. */ int binarySearch(vector<int> &array, int target) { // write your code here int size = array.size(); int low = 0, high = size-1, mid = (high + low) / 2; int find = -1; if(array[low]>target || array[high]<target) {

Python 数据分析

帅比萌擦擦* 提交于 2020-01-23 18:00:49
Python 数据分析(一) NumPy基础:数组与向量化计算 1.数组生成函数: arange:生成连续数字的数组(从0开始) In [1]: np.arange(15) Out[1]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) zeros:一次性创造全0数组 In [2]: np.zeros(10) Out[2]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) ones:一次性创造全1数组 In [3]: np.ones(10) Out[3]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) np.array:生成函数 In [4]: data = [1,2,3,4,5,6] In [5]: arr = np.array(data) In [6]: arr Out[4]: array([1, 2, 3, 4, 5, 6]) 2.ndarray的数据类型: dtype:显示数据元素类型,int表示整型,32表示占32个字节 同样有float64等 In: arr.dtype Out: dtype('int32') shape:显示数组每一维度的数量,(6,)表示6行0列 In : arr . shape

33. Search in Rotated Sorted Array 二分法的变向使用

南笙酒味 提交于 2020-01-23 10:36:34
33. Search in Rotated Sorted Array Medium Suppose an array sorted in ascending order is rotated at 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 are given a target value to search. If found in the array return its index, otherwise return -1 . You may assume no duplicate exists in the array. Your algorithm's runtime complexity must be in the order of O (log n ). Example 1: Input: nums = [ 4,5,6,7,0,1,2] , target = 0 Output: 4 Example 2: Input: nums = [ 4,5,6,7,0,1,2] , target = 3 Output: -1 参考: https://blog.csdn.net/qq_26410101/article/details

[Leetcode]-Remove Duplicates from Sorted Array

只愿长相守 提交于 2020-01-23 02:19:21
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length. Hide Tags :Array 。Two Pointers 题目26:输入一个已经拍好序的数组。当中有可能有反复数据。找出反复数据。栓出多余的反复数据。仅仅保留一个反复数据。 思路:记录当中的反复数据次数。比方当中有三个数据都是5,那么反复次数就为2。 将后面的数据依次填充到反复数据位置. if(nums[i] == nums[i+1]) count+

剑指 Offer——和为 S 的两个数字

穿精又带淫゛_ 提交于 2020-01-23 01:58:05
1. 题目 2. 解答 由于数组是已经排好序的,我们可以定义两个指针,第一个指针指向第一个元素,第二个指针指向最后一个元素,然后求出这两个元素的和,与目标和进行比较。若小于目标和,第一个指针向前移动;若大于目标和,第二个指针向后移动。 若等于目标和,题目中要求输出乘积最小的。由于两个元素的乘积肯定小于目标和的平方,因此我们初始化目标和的平方为一个最小乘积。当找到两个元素和等于目标和的时候,如果他们的乘积小于最小乘积,则更新最小乘积以及这两个元素的索引值,然后两个指针分别向前向后移动一步继续寻找。 时间复杂度 \(O(n)\) ,空间复杂度 \(O(1)\) 。 class Solution { public: vector<int> FindNumbersWithSum(vector<int> array, int sum) { vector<int> result; int n = array.size(); if (n < 2) return result; int first = 0; int last = n-1; int target = 0; int index1 = 0; int index2 = 0; int flag = 0; long long min_product = sum * sum; while (first < last) { target =

Remove Duplicates from Sorted List

若如初见. 提交于 2020-01-23 01:55:50
原题链接在这里: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2] , Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. 题解: 利用counter来更改新array前面的元素. Time Complexity: O(n). Space: O(1). AC Java: 1 public class

java-生成1~n的序列

試著忘記壹切 提交于 2020-01-22 21:43:09
import java.util.Scanner; /** * 生成1~n的排列 * @author NEU-2015 * */ public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n; int[] array; while (input.hasNext()) { n = input.nextInt(); array = new int[n]; print_permutation(n, array, 0); } input.close(); } private static void print_permutation(int n, int[] array, int i) { if (i == n) { //递归边界 for (int j = 0; j < array.length; j++) { System.out.print(array[j] + " "); } System.out.println(); } else { for (int k = 1; k <= n; k++) { //尝试在array[i]中填入各种整数i boolean flag = true; for (int m = 0; m < i; m++

旋转数组中的最小数字

风格不统一 提交于 2020-01-22 20:02:35
这道题感觉坑还是很多的,我有很多条件都没有考虑清楚 if判断条件 对mid 和high 还有low 的递增和递减 盲目的去设计,导致执行多次都没有成功 public int minNumberInRotateArray ( int [ ] array ) { if ( array . length == 0 ) { return 0 ; } int left = 0 ; int right = array . length - 1 ; int mid = 0 ; while ( left < right ) { mid = left + ( right - left ) / 2 ; if ( array [ mid ] > array [ right ] ) { //包括array left = mid + 1 ; } else if ( array [ mid ] == array [ right ] ) { right = right - 1 ; } else { right = mid ; } } return array [ left ] ; } 来源: CSDN 作者: qq_43641886 链接: https://blog.csdn.net/qq_43641886/article/details/104071448

向C#数组添加值

喜欢而已 提交于 2020-01-22 18:50:30
这可能是一个非常简单的方法-我从C#开始,需要向数组添加值,例如: int[] terms; for(int runs = 0; runs < 400; runs++) { terms[] = runs; } 对于那些使用PHP的人,这是我要在C#中尝试做的事情: $arr = array(); for ($i = 0; $i < 10; $i++) { $arr[] = $i; } #1楼 int[] terms = new int[10]; //create 10 empty index in array terms //fill value = 400 for every index (run) in the array //terms.Length is the total length of the array, it is equal to 10 in this case for (int run = 0; run < terms.Length; run++) { terms[run] = 400; } //print value from each of the index for (int run = 0; run < terms.Length; run++) { Console.WriteLine("Value in index {0}:\t{1}",run,