Array
konsy_dong
Java,C++,Python,linux
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode 717. 1-bit and 2-bit Characters
题目: We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).Now given a string represented by several bit...原创 2018-03-20 16:55:19 · 217 阅读 · 0 评论 -
《剑指Offer》 旋转数组的最小数字
题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。思路: 直接调用sort()函数排序,返回第1个元素代码:class Solution {public:原创 2017-04-06 15:45:40 · 326 阅读 · 0 评论 -
LeetCode 287. Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number原创 2017-04-19 10:23:32 · 302 阅读 · 0 评论 -
LeetCode 75. Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2017-04-19 10:22:13 · 510 阅读 · 0 评论 -
LeetCode 4. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1: nums1 = [1, 3]原创 2017-04-19 10:21:31 · 275 阅读 · 0 评论 -
LeetCode 119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].Note: Could you optimize your algorithm to use only O(k) extra space?思路: 大方向是先计算中间值之间的值,原创 2017-04-13 13:28:10 · 621 阅读 · 0 评论 -
《剑指Offer》 二维数组中的查找
题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。思路1: 暴力查找,两个for循环,按顺序查找 。 代码实现:class Solution {public: bool Find(int target, vector<vector<int> > array)原创 2017-03-24 16:39:47 · 516 阅读 · 0 评论 -
C++——获取array,vector,string的元素个数
array: sizeof(array) / sizeof(array[0]) 如果为字符串的字符数组则为 sizeof(array) / sizeof(array[0]) - 1 vector: vector.size()string: string.size()原创 2017-03-26 16:33:09 · 42171 阅读 · 3 评论 -
C++——cout输出流与字符指针
cout输出流cout语句的一般格式为: cout<<表达式1<<表达式2<<……<<表达式n; cin语句的一般格式为: cin>>变量1>>变量2>>……>>变量n;在定义流对象时,系统会在内存中开辟一段缓冲区,用来暂存输入输出流的数据。在执行cout语句时,先把插入的数据顺序存放在输出缓冲区中,直到输出缓冲区满或遇到cout语句中的endl(或’\n’,ends,flus原创 2017-03-27 15:33:10 · 2501 阅读 · 0 评论 -
《C和指针》——指针运算
指针运算的主要问题是边界问题,如果在边界值外的值没在循环判断的条件中使用,那么最后一步的位置(索引)越界是可以的,但是如果使用了,那么会存在潜在问题。 如下程序:for (vp = &values[N_VALUE - 1]; vp >= &values[0]; vp--){ *vp = 0; }原因:在数组第1个元素被清除之后,vp 的值还将减去1,而接下去的一次比较运算是原创 2017-03-30 08:57:43 · 672 阅读 · 0 评论 -
《C和指针》——将无符号整数转换为字符
其实这个程序只是为了更深刻地去理解递归,因为像什么递归方法计算Fibonacci数列是非常浪费的,效率非常地低。很多时候用递归的形式进行解译,只是因为它比非递归形式更为清晰,虽然用迭代实现往往比递归实现效率更高,但是代码的可读性稍差。下面是将无符号整数转换为字符的代码:void binaryToAscii(unsigned int value){ unsigned int quotient;原创 2017-03-31 20:37:55 · 1527 阅读 · 0 评论 -
C/C++——输入输出字符相关,cout.put()和putchar()
cout.put()和putchar()C++的ostream类除了提供上面介绍过的用于格式控制的成员函数外,还提供了专用于输出单个字符的成员函数put。 如 cout.put(‘a’);和cout.put(65 + 32);都是在屏幕上显示一个字符a。 putchar函数是C语言中使用的,在stdio.h头文件中定义,也可以输出一个字符。程序示例: 有一个字符串”BASIC”,要求把它们按相原创 2017-04-01 14:28:39 · 4277 阅读 · 0 评论 -
《C和指针》——声明数组参数
把一个数组参数传递给函数,应该声明为一个指针,因为调用函数时实际传递的是一个指针,如下面两个相等的函数原型:int strlen(char *string);int strlen(char string[]);原创 2017-04-01 16:37:29 · 517 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
题目 : Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] h原创 2017-05-01 11:23:48 · 364 阅读 · 0 评论 -
LeetCode 566. Reshape the Matrix
题目: In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dim原创 2017-05-01 11:27:06 · 1126 阅读 · 0 评论 -
LeetCode 695. Max Area of Island
题目: Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...原创 2018-03-20 16:25:32 · 231 阅读 · 0 评论 -
LeetCode 766. Toeplitz Matrix
题目: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.Now given an M x N matrix, return True if and only if the matrix is Toeplitz.Example 1: Input: ma...原创 2018-03-19 22:22:25 · 222 阅读 · 0 评论 -
LeetCode 746. Min Cost Climbing Stairs
题目: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the to...原创 2018-03-23 00:35:20 · 221 阅读 · 0 评论 -
一维数组的初始化问题(函数体内与函数体外)
数组元素初始化时,若没有显式提供幸元素的值,则元素会被像普通变量一样初始化:函数体外定义的内置类型数组(即内置类型的全局数组),元素初始化为0; 函数体内定义的内置类型数组,元素无初始化(注意,若只初始化部分元素,其后的元素此时也会被初始化为0); 如果不是内置类型,则不管其在哪定义,自动调用其默认构造函数为其初始化,若该类型无默认构造函数则会报错。下面针对1和2的不同给出具体例子:原创 2017-11-19 09:10:37 · 1239 阅读 · 0 评论 -
LeetCode 674. Longest Continuous Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequence.Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing sub原创 2017-09-21 13:39:39 · 459 阅读 · 0 评论 -
C++——二维vector初始化大小方法
初始化二维vector,为r*c的vector,所有值为0.1.直接用初始化方法(刚开始没想到) vector<vector<int>> newOne(r, vector<int>(c, 0));2.用resize()来控制大小 vector<vector<int> > res; res.resize(r);//r行 for (int k = 0; k < r;原创 2017-05-01 11:24:35 · 126178 阅读 · 6 评论 -
华为机试——句子逆序
题目描述 将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I” 所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符输入描述: 将一个英文语句以单词为单位逆序排放。 输出描述: 得到逆序的句子 示例1输入I am a boy输出boy a am I思路:从后往前遍历,遇到空格就将这个词加到output末尾再加上空格,最后一原创 2017-07-29 20:48:30 · 493 阅读 · 0 评论 -
LeetCode 448. Find All Numbers Disappeared in an Array
题目: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Coul原创 2017-07-16 14:58:03 · 274 阅读 · 0 评论 -
LeetCode 628. Maximum Product of Three Numbers
题目: Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note: 1. The length of th原创 2017-07-16 15:37:53 · 644 阅读 · 0 评论 -
LeetCode 581. Shortest Unsorted Continuous Subarray
题目: Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to fin原创 2017-07-16 15:25:06 · 372 阅读 · 0 评论 -
LeetCode 643. Maximum Average Subarray I
题目: Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.Example 1:Input: [1,12,原创 2017-07-16 10:32:09 · 765 阅读 · 0 评论 -
LeetCode 540. Single Element in a Sorted Array
题目: Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.Example 1: Input: [1,原创 2017-04-17 15:32:51 · 1458 阅读 · 1 评论 -
LeetCode 561. Array Partition I
题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as po原创 2017-04-24 09:36:20 · 2443 阅读 · 0 评论 -
《C和指针》——数组的奇怪形式
ap是一个指针名,array是一个数组名 1. ap[-1] 负值的下标,下标引用就是间接访问表达式,只要把它转换为那种形式并对它进行求值。ap如果指向第3个元素(就是那个下标值 为2的元素),所以使用偏移量-1就是得到它的前一个元素,也就是array[1]。 2. ap[6] 反这个下标表达式转换为与其对应的间接访问表达式形式,其实就是*(ap+6)。 3. 2[array] 表示*(2原创 2017-04-01 16:38:18 · 521 阅读 · 0 评论 -
《C和指针》——字符数组和字符串常量的区别
字符数组形式:char message1[ ]=”Hello”; 尽管看上去像是一个字符串常量,实际上它并不量,它与char message[ ]={‘H’,’e’,’l’,’l’,’o’,’\0’}一样是字符数组字符串常量形式:char *message2=”Hello”; 而message2是指向了字符串常量Hello的指针。 区别如下图所示:原创 2017-04-01 16:39:42 · 1069 阅读 · 0 评论 -
LeetCode 169. Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority原创 2017-03-17 20:36:54 · 298 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock
题目: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-04-06 16:43:59 · 549 阅读 · 0 评论 -
LeetCode 283. Move Zeroes
题目: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your原创 2017-04-06 16:48:35 · 360 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit原创 2017-04-06 16:16:58 · 472 阅读 · 0 评论 -
LeetCode 189. Rotate Array
题目: 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 to [5,6,7,1,2,3,4].Note: Try to come up as many solutions as you can,原创 2017-04-06 16:47:15 · 427 阅读 · 0 评论 -
LeetCode 412. Fizz Buzz
题目: Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”.原创 2017-04-04 16:28:59 · 379 阅读 · 0 评论 -
《C和指针》——指向数组的指针(逐个移动和逐行的区别)
定义一个数组 int matrix[3][10]; 第一种初始化: int (*p)[10]=matrix; 第二种初始化: int *pi=&matrix[0][0];与 int *pi=matrix[0];等价第一种初始化的p指向matrix的第1行,p是一个指向拥有10个整型元素的数组的指针。当把p与一个整数相加时,该整数值首先根据10个整型值的长度进行调整,然后再执行加法。可以一行原创 2017-04-01 16:39:03 · 1344 阅读 · 0 评论 -
C++ new一个数组时,指针移动程序崩溃问题
问题代码:#include using namespace std;int main(){ int a[] = { 12, 42, 6, 17, 32, 4, 19 }; int *pia = new int[7]; for (size_t i = 0; i < 7; ++i){ pia[i] = a[i]; } for (size_t i = 0; i < 6;++i){原创 2017-03-15 22:54:53 · 1629 阅读 · 0 评论 -
《剑指Offer》替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。思路: 首先改函数形参,改为引用传入 void replaceSpace(char *&str,int length) space记录空格数,new一个length+2*space+1的指针数组,再循环str,如果str[j]不是空格原创 2017-03-24 21:20:00 · 517 阅读 · 0 评论 -
LeetCode 27. Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant me原创 2017-03-16 13:54:38 · 630 阅读 · 0 评论
分享