面试笔试杂项积累-leetcode 161-165
来源:互联网 发布:海康提示网络不可达 编辑:程序博客网 时间:2024/05/20 23:04
162.162-Find Peak Element-Difficulty: Medium
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1]
, find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that 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.
click to show spoilers.
Your solution should be in logarithmic complexity.
思路
找到大于相邻节点的节点
遍历比较
public class Solution { public int FindPeakElement(int[] nums) { if (nums.Length < 2) return 0; int end = nums.Length - 2; for (int i = 1; i < end+1; i++) { if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) return i; if (nums[end] > nums[end - 1] && nums[end] > nums[end + 1]) return end; --end; } if (nums[0] > nums[1]) return 0; if (nums[nums.Length - 1] > nums[nums.Length - 2]) return nums.Length - 1; return 0; }}
164.164-Maximum Gap-Difficulty: Hard
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Try to solve it in linear time/space.
Return 0 if the array contains less than 2 elements.
You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
方法一
思路
求最大相邻数差(排序后)
public class Solution { public int MaximumGap(int[] nums) {//最大相邻数差 Array.Sort(nums); int max = 0; for (int i = 1; i < nums.Length; i++) { if (max < nums[i] - nums[i - 1]) max = nums[i] - nums[i - 1]; } return max; }}
其他方法
思路
桶排序:http://www.cnblogs.com/ganganloveu/p/4162290.html基数排序:https://leetcode.com/discuss/53636/radix-sort-solution-in-java-with-explanation
165.165-Compare Version Numbers-Difficulty: Easy
Compare two version numbers version1 andversion2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the.
character.
The .
character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5
is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
思路
比较版本号大小。1.1.3、20.3.4.5这种
每个.与.之间数的比较,相同进入下一个比较,分出大小就有比较结果了
public class Solution { public int CompareVersion(string version1, string version2) { if (version1 == null || version2 == null) return 0; int v1 = 0, v2 = 0; int i = 0, j = 0; while (i < version1.Length || j < version2.Length) { if (i < version1.Length && version1[i] != '.') { v1 = v1 * 10 + (version1[i] - '0'); i++; } if (j < version2.Length && version2[j] != '.') { v2 = v2 * 10 + (version2[j] - '0'); j++; } if (v1 > v2) return 1; else if (v1 < v2) return -1; } return 0; }}
- 面试笔试杂项积累-leetcode 161-165
- 面试笔试杂项积累-leetcode 1-5
- 面试笔试杂项积累-leetcode 6-10
- 面试笔试杂项积累-leetcode 11-15
- 面试笔试杂项积累-leetcode 16-20
- 面试笔试杂项积累-leetcode 21-25
- 面试笔试杂项积累-leetcode 26-30
- 面试笔试杂项积累-leetcode 31-35
- 面试笔试杂项积累-leetcode 36-40
- 面试笔试杂项积累-leetcode 41-45
- 面试笔试杂项积累-leetcode 46-50
- 面试笔试杂项积累-leetcode 51-55
- 面试笔试杂项积累-leetcode 56-60
- 面试笔试杂项积累-leetcode 61-65
- 面试笔试杂项积累-leetcode 66-70
- 面试笔试杂项积累-leetcode 71-75
- 面试笔试杂项积累-leetcode 76-80
- 面试笔试杂项积累-leetcode 81-85
- javascript计算器工作原理
- could not open parameter file '/u01/app/oracle/product/11.1.0/db_1/dbs/initorc11g.ora
- android数据存储的四种方案(三)
- 面试笔试杂项积累-leetcode 156-160
- POJ 1182_食物链
- 面试笔试杂项积累-leetcode 161-165
- 深夜学算法之SkipList:让链表飞
- POJ 2240_Arbitrage
- utf8解决JSP中文乱码问题
- linux习惯每天一个命令之mount命令
- Hibernate细节
- 今天是春节
- Codeforces Round #342 (Div. 2)(A)贪心,数学
- UVA 11400_ Lighting System Design