
String Algorithm
旧城故人往事
一只正在学习翱翔的菜鸟
展开
-
字符串算法——String转换为Integer
问题:给定一个String,将其转换为Integer。 注: 1. 丢弃掉字符串前面的所有空格字符,直到遇到第一个非空格字符 2. 遇到第一个字符元素初始化整数的符号 3. 字符串可以包含其他字符,不影响字符的转换,遇到非数字字符则停止转换输出整数 4. 如果转化的整数超出整数的范围,则输出整数的最大值或者最小值class原创 2017-10-12 09:07:57 · 1753 阅读 · 0 评论 -
字符串算法——单一数(Single Number II)
问题: Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could原创 2017-10-12 15:07:00 · 288 阅读 · 0 评论 -
字符串算法——反转字符串单词表(Reverse Words in a String)
问题: Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.Update (2015-02-12): For C programmers: Try to solve it in-place in O(1原创 2017-10-12 15:18:03 · 344 阅读 · 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 duplicate原创 2017-10-14 09:01:48 · 224 阅读 · 0 评论 -
字符串算法——查找有序数组旋转后最小值(有重复元素)(Find Minimum in Rotated Sorted Array II)
问题: Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated原创 2017-10-14 09:21:14 · 337 阅读 · 0 评论 -
字符串算法——查找数组中多数元素(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 element al原创 2017-10-14 09:22:58 · 1056 阅读 · 0 评论 -
字符串算法——查找数组第K个最大值( Kth Largest Element in an Array)
问题: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.原创 2017-10-14 09:47:21 · 405 阅读 · 0 评论 -
字符串算法——查找数组多数元素(Majority Element II)
问题: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路一:与前述的majority element类似,采用直接遍历计算元素出现的次数,如果超过原创 2017-10-14 10:33:56 · 368 阅读 · 0 评论 -
字符串算法——查找二维数组中元素(Search a 2D Matrix II)
问题: 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 in ascending from left to right. Integers in ea原创 2017-10-14 10:40:03 · 782 阅读 · 0 评论 -
字符串算法——单一数(Single Number)
问题: Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2017-10-12 14:59:15 · 399 阅读 · 0 评论 -
字符串算法——旋转数组中查找目标值(有重复元素)( Search in Rotated Sorted Array II)
问题: Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated at so原创 2017-10-12 10:56:48 · 513 阅读 · 0 评论 -
字符串算法——字符串匹配
问题:给定一个字符串和一个子串,找到子串在字符串中出现的第一个索引位置,如果没有则返回-1. 解决思路:如果考虑到时间复杂度,则可以采用KMP算法,时间复杂度为O(m+n)O(m+n),暂不详细介绍其原理过程。class Solution { public int strStr(String haystack, String needle) { //KMP算法原创 2017-10-12 09:18:00 · 210 阅读 · 0 评论 -
字符串算法——最长子串(无重复元素)
问题:给定一个字符串,找出最长的无重复元素的子串 例如: “bbbbbb”的子串为”b” “abcabccd”的子串为”abc”原创 2017-10-11 22:29:45 · 732 阅读 · 0 评论 -
字符串算法——两个有序数组的中位数
问题:有两个有序的数组nums1和nums2,长度分别为m和n,找到两个有序数组的中位数,运行时间复杂度为O(log(m+n))O(log(m+n)) 例如: nums1 = [1,3],nums2 = [2],中位数为2.0 nums1 = [1,2],nums2 = [3,4],中位数为2.5 解决思路:如果没有要求时间复杂度,可以对两个数组的每个元素依次比较排序或者使用归并排序,这里要原创 2017-10-12 08:25:06 · 394 阅读 · 0 评论 -
字符串算法——最长回文子串
问题:给定一个字符串,找出最长的回文子串 例如: “babad”的最长回文子串为”bab”,当然”aba”也是可以的 解决思路:这种问题要考虑到回文子串是奇数还是偶数情况,分为两种可能。这里可以采用从内向外扩散的方法来解决该问题。 例如:回文子串为奇数,从第一个字符元素开始扩散。第一个子串为”b”,然后第二个字符元素为“a”,向左右扩散一位为”bab”,还是回文子串,再向左右扩散不满足回文条原创 2017-10-11 22:43:05 · 257 阅读 · 0 评论 -
字符串算法——旋转数组中查找目标值(Search 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).You are given a target value to search. If found in t原创 2017-10-12 09:46:38 · 729 阅读 · 0 评论 -
字符串算法——数组或字符串全排列(Permutations)
问题: Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]这原创 2017-10-12 10:00:46 · 283 阅读 · 0 评论 -
字符串算法——有重复字符的数组或字符串全排列(Permutations II)
问题: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1]原创 2017-10-12 10:26:54 · 591 阅读 · 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 r原创 2017-10-12 10:42:53 · 475 阅读 · 0 评论 -
字符串算法——单一数(Single Number III)
问题: ven an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums = [原创 2017-10-14 10:42:53 · 514 阅读 · 0 评论