
字符串与数组
文章平均质量分 58
张荣华_csdn
这个作者很懒,什么都没留下…
展开
-
409.最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。注意:假设字符串的长度不会超过 1010。示例 1:输入:"abccccdd"输出:7解释:我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。class Solution {public: in...原创 2018-11-14 12:49:53 · 173 阅读 · 0 评论 -
Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7]and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5...原创 2018-07-12 00:17:48 · 250 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.Example 1:Input: [10,2]Output: "210"Example 2:Input: [3,30,34,5,9]Output: "9534330"Note: The result may be v...原创 2018-07-12 00:17:40 · 180 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.Example 1...原创 2018-06-28 17:23:21 · 176 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array II
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]).Find the minimum element.The array may contain dupl...原创 2018-06-30 22:39:07 · 245 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array
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]).Find the minimum element.You may assume no duplicat...原创 2018-06-30 22:39:16 · 195 阅读 · 0 评论 -
Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation: [2,3] has t...原创 2018-06-30 22:39:23 · 179 阅读 · 0 评论 -
Reverse Words in a String
Given an input string, reverse the string word by word.Example: Input: "the sky is blue",Output: "blue is sky the".Note:A word is defined as a sequence of non-space characters.Input string may cont...原创 2018-06-29 13:34:57 · 245 阅读 · 0 评论 -
string和数值之间的转换
to_string(val):将算数类型转换为string类型;stoi(s,p,b):将string类型转换为int类型;stol(s.p,b):将string类型转换为long类型;stoul(s,p,b):将string类型转换为unsigned long类型;stoll(s,p,b):将string类型转换为long long类型;stoul(s,p,b):将string;类型转换为uns...原创 2018-06-25 00:39:16 · 1616 阅读 · 2 评论 -
字符串常见考点
用字符串常量初始化一个字符串,系统会默认在字符串最后添加结束标值‘\0’;标准字符串处理函数只能处理标准的字符串(包含字符串结束标志);C语言中规定,如果指针变量p指向数组中的一个元素,则p+1指向同一数组中的下一个元素,以此类推,p+i指向同一数组中的第i+1个元素;char *str="abc";这样定义的是一个字符串常量,不可以修改。...原创 2018-07-12 00:18:30 · 384 阅读 · 0 评论 -
翻转单词
首先将整个字符串进行倒置,然后将每一个单词进行倒置。#include<iostream>using namespace std;int reverseWords(char *);int reverseStr(char *, int, int);int reverseWords(char *str){ int wordBegin = 0, wordEnd = ...原创 2018-07-12 00:18:36 · 736 阅读 · 0 评论 -
构建乘积数组
题目:给定一个数组A[0,1,,,,n-1],请构建一个数组B[0,1,,,,n-1],其中B中的元素B[i]=A[0]XA[1]X...XA[i-1]XA[i+1]X...XA[n-1].不能使用除法。思路:将数组B分成两部分,定义C[i]=A[0]XA[1]X...XA[i-1],D[i]=A[i+1]X...XA[n-1]。C[i]可以自上而下的顺序计算出阿里,D[i]可以自下而上的顺序...原创 2018-07-28 00:07:34 · 194 阅读 · 0 评论 -
Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers su...原创 2018-07-08 00:21:56 · 172 阅读 · 0 评论 -
数的分组
题目:有10个任意的正整数,将其分为两组A和B,要求组A中每个数据的和与组B中每个数据的和之差的绝对值最小。#include "stdio.h"#include "math.h"void main(){ unsigned int i,j,k=0,difference=0,r; unsigned int sum_A=0, sum_B=0; int a[10]; ...原创 2018-07-27 00:02:04 · 363 阅读 · 0 评论 -
寻找数组中的次大数
题目:10个互不相等的整数,求其中的第2大的数字,要求数组不能排序,设计的算法效率越高越好。思路:可以设置两个变量max、secmax,将整个数据域分为3个部分:x<secmax,secmax<x<max,x>max。首先初始化max和secmax,将DATA[0]和DATA[1]中的最大值和次大值分别赋给max和secmax。接下来从DATA[2]开始将数组中的每个数...原创 2018-07-27 00:01:34 · 1522 阅读 · 1 评论 -
数组中数字出现的次数
题目:数组中只出现一次的两个数字。一个整形数组除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度为O(n),空间复杂度为O(1)。void FindNumsAppearOnce(int data[], int length, int* num1, int* num2){ if(data == nullptr || length < 2) ...原创 2018-07-22 00:13:48 · 1504 阅读 · 0 评论 -
数组中数值和下标相等的元素
题目:假设一个单调递增的数组里的每个元素都是整数并且是唯一的。请编程实现一个函数,找出数组中任意一个数值等于其下标的元素。例如,在数组{-3,-1,1,3,5}中,数字3和它的下标相等。方法一:从头到尾依次扫描数组中的数字,并逐一检验数字是不是和下标相等。时间复杂度为O(n)。方法二:由于数组是单调递增排序的,因此我们可以尝试二分查找算法来进行优化。假设我们某一步抵达数组中的第i个...原创 2018-07-21 00:10:35 · 2734 阅读 · 0 评论 -
丑数
题目:我们把只包含因子2、3、5的数称为丑数。求按从小到大的顺序的第1500个丑数。例如,6、8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做第一个丑数。创建数组保存已找到的丑数,用空间换时间的解法class Solution {public: int nthUglyNumber(int n) { vector<int> nums(1,1);...原创 2018-07-20 00:14:59 · 265 阅读 · 0 评论 -
找出0/1字符串中0和1连续出现的最大次数
#include<iostream>using namespace std;int getMaxCount(char * str,int * max0,int *max1){ int i,len,tmp_max0=0,tmp_max1=0; if(str==NULL) return 0; len=strlen(str); for(int i=0;i<len;i++) { if...原创 2018-07-13 00:10:04 · 1463 阅读 · 0 评论 -
Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If found in t...原创 2018-06-19 01:12:31 · 149 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by modif...原创 2018-06-19 01:12:23 · 175 阅读 · 0 评论 -
Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is ...原创 2018-06-19 01:11:46 · 188 阅读 · 0 评论 -
Median of Two Sorted Arrays(C++)
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m=nums1.size(),n=nums2.size(); if((m+n)%2==1) re...原创 2018-05-15 17:20:10 · 471 阅读 · 0 评论 -
Longest Substring Without Repeating Characters(C++)
Given a string, find the length of the longest substring without repeating characters.class Solution {public: int lengthOfLongestSubstring(string s) { map<char,int> maps; in...原创 2018-05-15 17:15:41 · 463 阅读 · 0 评论 -
String to Integer (atoi)(C++)
class Solution {public: int myAtoi(string str) { int n=str.size(); int flag=1,i=0,ret=0; if(n==0) return 0; while(str[i]==' ') i++; if(s...原创 2018-05-15 17:32:14 · 608 阅读 · 0 评论 -
3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.The solution set must not contain dupli...原创 2018-05-23 13:11:48 · 351 阅读 · 0 评论 -
3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-05-23 13:13:14 · 146 阅读 · 0 评论 -
4Sum(C++)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of tar...原创 2018-05-23 13:16:01 · 405 阅读 · 0 评论 -
数字序列中某一位的数字
数字以01234567891011121314……的格式序列化到一个字符序列中。求任意第n位数字对应的数字。#include <iostream>#include <algorithm>using namespace std;int countOfIntegers(int digits);int digitAtIndex(int index, int digits);int...原创 2018-06-02 14:29:05 · 305 阅读 · 0 评论 -
把数字翻译成字符串的方法数
采用递归,从右向左int GetTranslationCount(const string& number);int GetTranslationCount(int number){ if(number < 0) return 0; string numberInString = to_string(number); return GetTransla...原创 2018-06-02 15:33:27 · 489 阅读 · 0 评论 -
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.class Solution {public: vector<int> spiralOrder(vector<vector<int>>& ma...原创 2018-05-27 09:33:05 · 586 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array
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]).Find the minimum element.You may assume no duplicat...原创 2018-06-05 08:06:14 · 236 阅读 · 0 评论 -
Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]Example ...原创 2018-06-19 01:11:34 · 128 阅读 · 0 评论 -
矩阵最小路径和
有一个矩阵map,它每个格子有一个权值。从左上角的格子开始每次只能向右或者向下走,最后到达右下角的位置,路径上所有的数字累加起来就是路径和,返回所有的路径中最小的路径和。给定一个矩阵map及它的行数n和列数m,请返回最小路径和。保证行列数均小于等于100.测试样例:[[1,2,3],[1,1,1]],2,3返回:4class MinimumPath {public: int getMin(v...原创 2018-06-24 00:18:18 · 910 阅读 · 0 评论 -
Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.Example:Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], t...原创 2018-06-14 09:15:14 · 209 阅读 · 0 评论 -
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.Example:Input:[ ["1","0","1","0","0"], ["1","0","1",&a原创 2018-06-06 08:15:10 · 184 阅读 · 0 评论 -
Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Example:Input: n = 10Output: 12Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is...原创 2018-06-13 01:51:22 · 179 阅读 · 0 评论 -
Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Note:All numbers will be p...原创 2018-06-06 08:16:23 · 178 阅读 · 0 评论 -
Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,...原创 2018-06-05 08:04:53 · 258 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.Example 1:Input: [10,2]Output: "210"Example 2:Input: [3,30,34,5,9]Output: "9534330"Note: The result may be...原创 2018-06-05 08:05:24 · 265 阅读 · 0 评论