算法是程序员的基本功,也是各个大厂必考察的重点,让我们一起坚持写算法题吧
遇事不决,可问春风,春风不语,即是本心。
我们在我们能力范围内,做好我们该做的事,然后相信一切都事最好的安排就可以啦,慢慢来,会很快,向前走,别回头。
目录
1.盛最多水的容器
2.整数转罗马数字
3.罗马数字转整数
4.最长公共前缀
5.三数之和
1.盛最多水的容器
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/container-with-most-water/description/
思路:双指针,从两侧往中间遍历,每次短的那个边往中间走,计算整个过程中最大的面积即可。
Java版:
class Solution {
public int maxArea(int[] height) {
int max = 0 ;
int left = 0, right = height.length - 1 ;
while(left < right){
int h = Math.min(height[left], height[right]) ;
int area = h * (right - left) ;
max = max < area ? area : max ;
while(left < right && height[left] <= h){
left ++ ;
}
while(left < right && height[right] <= h){
right -- ;
}
}
return max ;
}
}
Python版:
class Solution:
def maxArea(self, height: List[int]) -> int:
max = 0
left = 0
right = len(height) - 1
while left < right:
h = min(height[left], height[right])
area = h * (right - left)
if area >= max:
max = area
while left < right and height[left] <= h:
left += 1
while left < right and height[right] <= h:
right -= 1
return max
Js版:
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let max = 0
let left = 0, right = height.length - 1
while(left < right){
const h = Math.min(height[left], height[right])
const area = h * (right - left)
max = max <= area ? area : max
while(left < right && height[left] <= h){
left ++
}
while(left < right && height[right] <= h){
right --
}
}
return max
};
2.整数转罗马数字
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/integer-to-roman/description/
思路:一共13种罗马数字,针对当前数字,每次找出不大于当前的数字的最大罗马数字,每次拼接该罗马数字,当前数字减去对应的值。
Java版:
class Solution {
public String intToRoman(int num) {
int [] key = {1,4,5,9,10,40,50,90,100,400,500,900,1000} ;
String [] value = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"} ;
String ans = "" ;
for(int i=key.length-1; i>=0; i--){
while(key[i] <= num){
num -= key[i] ;
ans += value[i] ;
}
}
return ans ;
}
}
Python版:
class Solution:
def intToRoman(self, num: int) -> str:
key = [1,4,5,9,10,40,50,90,100,400,500,900,1000]
value = ["I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"]
key.reverse()
value.reverse()
s = ""
for i in range(len(key)):
element = key[i]
while num >= element:
num -= element
s += value[i]
return s
Js版:
/**
* @param {number} num
* @return {string}
*/
var intToRoman = function(num) {
const key = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
const value = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
let res = ""
for(let i=0; i<key.length; i++){
while(num >= key[i]){
res += value[i]
num -= key[i]
}
}
return res
};
3.罗马数字转整数
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/roman-to-integer/description/
思路:从后向前遍历字符串,判断当前字符代表的数字是否大于等于上一个,满足条件就累加,否则就累减,每次更新当前字符所代表的值以及上一个字符所代表的值。
Java版:
class Solution {
public int romanToInt(String s) {
int value =0, num1 = 0, num2 = 0 ;
for(int i=s.length()-1; i>=0; i--){
switch(s.charAt(i)){
case 'I' : num1 = 1; break;
case 'V' : num1 = 5; break ;
case 'X' : num1 = 10; break ;
case 'L' : num1 = 50; break ;
case 'C' : num1 = 100; break ;
case 'D' : num1 = 500; break ;
case 'M': num1 = 1000; break ;
}
if(num1 >= num2){
value += num1 ;
}else{
value -= num1 ;
}
num2 = num1 ;
}
return value ;
}
}
Python版:
class Solution:
def romanToInt(self, s: str) -> int:
value = num1 = num2 = 0
s1 = s[::-1]
for c in s1:
if c == 'I': num1 = 1
if c == 'V': num1 = 5
if c == 'X': num1 = 10
if c == 'L': num1 = 50
if c == 'C': num1 = 100
if c=='D': num1 = 500
if c=='M': num1 = 1000
if num1 >= num2:
value += num1
else:
value -= num1
num2 = num1
return value
JS版:
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
let value = 0, num1 = 0, num2 = 0
for(let i=s.length-1; i>=0; i--){
switch(s.charAt(i)){
case 'I' : num1 = 1; break ;
case 'V' : num1 = 5; break ;
case 'X' : num1 = 10; break;
case 'L': num1 = 50; break ;
case 'C': num1 = 100; break ;
case 'D': num1 = 500; break ;
case 'M': num1 = 1000; break ;
}
if(num1 >= num2){
value += num1
}else{
value -= num1
}
num2 = num1
}
return value
};
4.最长公共前缀
题目连接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/longest-common-prefix/description/
思路:写一个求两个字符串公共前缀和的方法,然后用该方法依次比对当前公共前缀与下一个字符串的公共前缀即可。
Java版:
class Solution {
public String commonPrefix(String s1, String s2){
if(s1.length() > s2.length()){
String tmp = s1 ;
s1 = s2 ;
s2 = tmp ;
}
for(int i=0; i<s1.length(); i++){
if(s1.charAt(i) != s2.charAt(i)){
return s1.substring(0,i) ;
}
}
return s1 ;
}
public String longestCommonPrefix(String[] strs) {
String ans = strs[0] ;
for(int i=1; i<strs.length; i++){
ans = commonPrefix(ans, strs[i]) ;
}
return ans ;
}
}
Python版:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
prefix = strs[0]
for i in range(1,len(strs)):
prefix = self.commonPrefix(prefix, strs[i])
return prefix
def commonPrefix(self, s1, s2):
if len(s1) > len(s2):
tmp = s1
s1 = s2
s2 = tmp
for i in range(len(s1)):
if s1[i] != s2[i]:
return s1[0:i]
return s1
Js版:
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let prefix = strs[0]
for(let i=1; i<strs.length; i++){
prefix = commonPrefix(prefix, strs[i])
}
return prefix
};
var commonPrefix = function(s1, s2){
if(s1.length > s2.length){
const tmp = s1
s1 = s2
s2 = tmp
}
for(let i=0; i<s1.length; i++){
if(s1.charAt(i) != s2.charAt(i)){
return s1.substring(0,i)
}
}
return s1
};
5.三数之和
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/3sum/description/
思路:排序+双指针,当前指针固定最左侧,另外两个指针分别在中间和右侧。
Java版:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums) ;
List<List<Integer>> ans = new ArrayList<>() ;
for(int i=0; i<nums.length-1; i++){
int cur = nums[i] ;
int left = i+1, right = nums.length - 1 ;
if(i>0 && nums[i] == nums[i-1]){
continue ;
}
while(left < right){
int res = cur + nums[left] + nums[right] ;
if(res == 0){
List<Integer> tmp = new ArrayList<>() ;
tmp.add(nums[left]) ;
tmp.add(cur) ;
tmp.add(nums[right]) ;
while(left < right && nums[left] == nums[left+1]){
left ++ ;
}
while(left < right && nums[right] == nums[right - 1]){
right -- ;
}
ans.add(tmp) ;
left ++ ;
right -- ;
}else if(res > 0){
right -- ;
}else{
left ++ ;
}
}
}
return ans ;
}
}
python版:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
ans = []
nums.sort()
for i in range(len(nums)):
if i>0 and nums[i] == nums[i-1]:
continue
cur = nums[i]
left = i+1
right = len(nums) - 1
while left < right:
res = cur + nums[left] + nums[right]
if res == 0:
tmp = []
tmp.append(cur)
tmp.append(nums[left])
tmp.append(nums[right])
while left < right and nums[left] == nums[left+1]:
left += 1
while left < right and nums[right] == nums[right-1]:
right -= 1
ans.append(tmp)
left += 1
right -= 1
elif res > 0:
right -= 1
else:
left += 1
return ans
Js版:
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const ans = []
// js对负数进行排序
const nums1 = nums.sort(function(a,b){
return a - b
})
for(let i=0; i<nums1.length; i++){
if(i>0 && nums1[i-1] == nums1[i]){
continue
}
let cur = nums1[i]
let left = i + 1
let right = nums1.length - 1
while(left < right){
const res = cur + nums1[left] + nums1[right]
if(res == 0){
tmp = []
tmp.push(cur)
tmp.push(nums1[left])
tmp.push(nums1[right])
while(left < right && nums1[left] == nums1[left + 1]){
left ++
}
while(left < right && nums1[right] == nums1[right - 1]){
right --
}
ans.push(tmp)
left ++
right --
}else if(res > 0){
right --
}else{
left ++
}
}
}
return ans
};