LeetCode_easy_中文解析50题_day01

1、Tow Sum

一、题目:

给定一个数组和目标数字,目标数字等于数组中某两个元素的和;(注意这两个数不是同一个数),返回这两个元素的索引

思想:

将数组先存进一个map,而map的特点正是key不能重复,而value存储对应nums[i]元素的索引。使用目标数字,减去每次遍历的nums[i],得到一个temp数字,看看这个数字是否在map集合中,还要确保这个数字不是target减去的那个元素。如此得到的另一个元素。然后就是把他们各自的索引存储到result[]数组中,这个数组也就只能存两个数字。

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
package cn.hnu.leetcode;
import java.util.HashMap;

public class _001_TowSum {

	public int[] towSum(int[] nums,int target){
		
		//定义一个map,key存储的是每一个nums[i],value存储的是每个nums[i]的索引i
		HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
		
		//保存最终的结果
		int[] result = new int[2];
		
		/***************************************************
		如果有重复元素,那么key不变,value被后面重复的数字的索引覆盖
		比如数组2 3 1 2
		在map中的存储形式就是
		2 3
		3 1
		1 2
		这样可以避免如果目标数字是4 
		取出来的索引都是0 0 
		而实际上取出来的索引应该是0 3
		****************************************************/
		for(int i = 0;i<nums.length;i++){
			map.put(nums[i], i);
		}
		
		for(int i = 0;i<nums.length;i++){
			//用target减去遍历到的数字,就是另一个需要的数字
			int temp = target - nums[i];
			
			//直接在HashMap中查找其是否存在
			//map.get(t)获得的索引跟遍历数组的索引不是一回事
			//比如target是4,遍历到了一个2,那么另外一个2不能是之前那个2
			if(map.containsKey(temp)&&map.get(temp)!=i){
				result[0] = i;
				result[1] = map.get(temp);
				break;
			}
		}
		
		return result;
		
	}
}

7.、Reverse Integer

二、题目:

给定一个Integer类型的整数,将这个整数反转;注意在反转过程中如果溢出,则输出0。

思想:

怎样逆序取出每一位,其实就是连续取模后再除以10,直到为0。比如123%10 取到个位3 ,然后123/10就是12;12%10取得十位2,12/10就是1;1%10取到百位1,1/10等于0。由此每一位数就逆序取得3 2 1;怎样组合成一个新的三位数,就是:3,3*10+2;(3*10+2)*10;((3*10+2)*10)+1 ---> 3 、32 、320、321。

之后就是判断越界,最终的结果是result = result*10 + mod;如果result越界应该是result > Integer.MAX_VALUE或者result < Integer.MIN_VALUE;以越界最大整数为例,那么result*10 + mod > Integer.MAX_VALUE,相比较于Integer.MAX_VALUE,mod可以忽略不计,那么result>Integer.MAX_VALUE/10 为越上界,result < Integer.MIN_VALUE/10为越下界。

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21
package cn.hnu.leetcode;
public class _007_Reverse {

	public int reverse(int x) {
		//存储最终的结果
		int result = 0;
		
		while(x!=0){
			//依次取出个位、十位、百位...
			int mod = x % 10;
			
			//比如依次从四位数变成三位数、两位数、一位数
			x = x / 10;
			
			//判断是否越界
			if(result>Integer.MAX_VALUE/10||result<Integer.MIN_VALUE/10){
				return 0;
			}
			
			//计算结果
			result = result*10 + mod;
			
		}
		
		return result;
        
    }
}

9、Palindrome Number

三、题目:

给定一个整数,不能把它变成字符串,问这个整数是否是回文数,所谓回文数就是顺着读或者逆着读都是同一个数。

思路:

将这个整数拆两半,要考虑是偶数位或者是奇数位,比较拆成两半后的数字是否相同;代码中有详细说明。

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
package cn.hnu.leetcode.easy;

public class _009_IsPalindrome {

	public boolean isPalindrome(int x) {
		
		//如果这个数是负数,那么一定不是回文数
		//如果这个数是10的整数倍,那么这个数的最高位是0,也没有这样的回文整数
		if(x<0||(x%10==0&&x!=0)){
			return false;
		}
		
		int right = 0;
		
		//这个数的左半边和右半边比较

		/**
		 * 比如这个数是:12821 奇数位
		 * x = 12821 right = 0; 
		 * 12821 > 0; right = 1 , x = 1282 ; 
		 * 1282 > 1; right = 12 , x = 128 ; 
		 * 128 > 12; right = 128 , x = 12;
		 * x<128 循环结束 ; 此时right = 128/10 , x = 12;
		 * 就是不管中间那位数是多少,只管两边是否一样
		 */
		
		/**
		 * 比如这个数是:1221 偶数位
		 * x = 1221  right = 0;
		 * 1221 > 0;right = 1 , x = 122;
		 * 122 > 1;right = 12 , x = 12;
		 * x = right ; 循环结束
		 * 两边一样就是回文数了
		 */
		while(x>right){
			right = right*10 + x%10;
			x = x / 10;
		}
		
		
		return x==right||x==right/10;
        
    }
}

13、Roman to Integer

四、题目:

罗马数字由七个不同的符号表示:I,V,X,L,C,D和M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

例如,2 用罗马数字可以表示成II; 12 写为XII,简称为X + II;第27可以表示成XXVII,即XX + V + II。罗马数字通常从左到右从最大到最小。 但是,四个数字不是IIII。 而是IV。 因为 I 个在 V 之前,所以5-1=4。 同样的原则适用于9,即 IX。现有以下使用减法的实例:

I(1) 可以放在V(5)或者X(10)之前得到4和9;

X(10)可以放在L(50)和C(100)之前得到40和90;

C(100)可以放在D(500)和M(1000)之前,以产生400和900;

给定罗马数字,将其转换为整数。 输入保证在1到3999的范围内。

思想:

先将罗马字母和数字,按照键值对的形式放在一个map中;我们从字符串的末尾开始一个一个读,读到的字母都视为前置字母,获取字母对应的整数值preVal,一开始val=0;只要preVal >= val,结果result 都加上对应的preVal;否则做减法,最后再将val = preval;详见代码;

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
package cn.hnu.leetcode.easy;

import java.util.HashMap;

public class _013_RomanToInt {

	public int romanToInt(String s) {	
		
		//将罗马字母与对应的整数值以键值对存在map中
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();

		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);

		//当前字母,也可以理解成当前数字
		int val = 0;
		
		//存储最终的结果集
		int result = 0;
		
		//从字符串的最后遍历
		
		/**
		 * 举例:MCMXCIV
		 * val = 0; preval = 5;
		 * V (preval=5) > 0 ; result = 0 + 5 = 5; val = 5;
		 * I (preval=1) < 5 ; result = 5 - 1 = 4; val = 1;
		 * C (preval=100) > 1 ; result = 4+100=104; val = 100;
		 * X (preval=10) < 100 ; result = 104-10=94; val = 10;
		 * M (preval=1000) > 10 ; result = 94+1000=1094; val = 1000;
		 * C (preval=100) < 1000 ; result = 1094-10=994; val = 100;
		 * M (preval=1000) > 100 ; result = 994+1000=1994; val = 1000;
		 * 
		 * 只要当前获取到的值大于或者等于上一次获取的值,就+,否则就-
		 */
		for (int i = s.length() - 1; i >= 0; i--) {
			
			//定位某个字符
			char key = s.charAt(i);
			
			//可以想成最后希腊数字的后面还有一个0,就是val
			//每次获取的都是前置希腊字母
			Integer preVal = map.get(key);

			if (preVal >= val) {
				result = result + preVal;
			} else {
				result = result - preVal;
			}

			//在进行下一次循环获取前置希腊字母时,将本次的preVal赋值给val
			//这样就能进行如上例的V和I进行比较,I和C进行比较
			val = preVal;
		}

		return result;

	}
}

14、Longest Common Prefix

五、题目:

编写一个函数来查找字符串数组中最长的公共前缀字符串。如果没有公共前缀,则返回空字符串;
所有给定的输入都是小写字母a-z。

思想:

将字符串数组中出现的第一个字符串当做是公共前缀,然后从字符串数组的第二项开始遍历,利用String类的indexOf()方法匹配这个公共前缀,(可以想一下,如果前两个字符串存在公共前缀,而后面匹配不了这个公共前缀,那么公共前缀就是不存在的)

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
package cn.hnu.leetcode.easy;

public class _014_LongestCommonPrefix {

	public String longestCommonPrefix(String[] strs) {
		
		if(strs.length==0){
			return "";
		}
		
		//将字符串数组中的第一个字符串作为公共前缀
		String conPrefix = strs[0];
		
		for(int i = 1;i<strs.length;i++){
			//indexOf()方法返回子串第一次出现的索引,如果没找到就返回-1,如果找到字符串的开头还没找到,也就退出循环
			while(strs[i].indexOf(conPrefix)!=0){
				
				//如果前缀不匹配,就将前缀的长度-1,再继续匹配,直到前缀的长度为0
				conPrefix = conPrefix.substring(0, conPrefix.length()-1);
				
				//如果公共前缀长度为0
				if(conPrefix.isEmpty()){
					return "";
				}
			}
			
		}
		
		return conPrefix;
	
	} 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值