一. 电面经典模拟
642. Sliding Window Average from Data Stream:点击打开链接
public class MovingAverage {
Queue<Integer> queue;
int size;
double sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue=new LinkedList<>();
this.size=size;
}
public double next(int val) {
if(queue.size()>=size){ //queue.size()=size,但是一直在sliding window向后滑动
queue.offer(val);
sum=sum-queue.poll()+val;
return sum/size;
}else{ //queue.size()<size或者第一次到达给定size
sum+=val;
queue.offer(val);
return sum/queue.size();
}
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param = obj.next(val);
*/
644.Mirror Numbers:
点击打开链接
思路:从左往右扫一遍,看对应的位置是不是反着的
public class Solution {
/**
* @param num a string
* @return true if a number is strobogrammatic or false
*/
public boolean isStrobogrammatic(String num) {
if(num==null || num.length()==0){
return false;
}
Map<Character,Character> map=new HashMap<>();
map.put('6','9');
map.put('9','6');
map.put('0','0');
map.put('1','1');
map.put('8','8');
char[] array=num.toCharArray();
int left=0,right=num.length()-1;
while(left<=right){
if(!map.containsKey(array[left])){ //因为map已经装好了所有的可能情况,所以map没有就是false
return false;
}else{ //当扫到map里有的情况,要看map的value和相对应的位置是否一样
if(map.get(array[left++])!=array[right--]){
return false;
}
}
}
return true;
}
}
640. Edit Distance II:点击打开链接
思路:三种情况:两个字符串长度差>1,直接false;
两个字符串长度差=1,两个字符串分别跳过一个或两个字符到下一个两边相等的字符,做比较
两个字符串长度差=0,两个字符串同时扫一遍,判断不等的字符个数是否为1
例如:两个字符串长度差=1,“abefcd”和"aefcd","abefcd"向后移动一位,两串再从e开始做比较
public class Solution {
/**
* @param s a string
* @param t a string
* @return true if they are both one edit distance apart or false
*/
public boolean isOneEditDistance(String s, String t) {
if(s==null || t==null){
return false;
}
int i=0,j=0;
char[] sArr=s.toCharArray();
char[] tArr=t.toCharArray();
int diff=Math.abs(s.length()-t.length());
if(diff>1){
return false;
}else if(diff==1){
while(i<s.length() && j<t.length()){
if(sArr[i]!=tArr[j]){
return sArr[i]==tArr[j+1] || sArr[i+1]==tArr[j]; //因为不知道哪个长度长
}
i++;
j++;
}
}else if(diff==0){
int count=0;
while(i<s.length() && j<t.length()){
if(sArr[i]!=tArr[j]){
count++;
}
i++;
j++;
}
return count==1;
}
return true;
}
}
二. 系统操作模拟实现
660. Read Characters From File-multiple calls:点击打开链接
思路:1. 调用read4函数从File中读取内容每次可以读取4个字符,要调用多少次read4(),就要看给定的n的字符多少而定,有两种情况
如果给定的n的字符数<=4,一次读完,读出来先放到queue这个缓存里,然后再写进buf数组里,判断已读完,break
如果给定的n的字符数>4,第一次要读4个,读出来先放到queue这个缓存里,然后再写进buf数组里,判断还未读完
继续读,直到可以一次读完,break
2. 而且在执行过程中给定的n的字符在每次调用read()函数的时候也是不一样的,返回的total值是从file里一共读取了多少个字符数
例如: "yellowred"
read(6)
read(3)
结果: 6,buf="yellow"
3,buf="red"
分析:调用read(6):n>=4,一次读不完,先读前4个"yell",放到queue,写进buf里,
再读剩下的2个"ow",放到queue,写进buf,目前buf有"yellow"
调用read(3):n<4,一次读完,读所有的"red",放到queue,写进buf里,目前buf有"red",最后buf有"yellowred"
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */
public class Solution extends Reader4 {
/**
* @param buf destination buffer
* @param n maximum number of characters to read
* @return the number of characters read
*/
Queue<Character> queue=new LinkedList<>();
public int read(char[] buf, int n) {
int total=0;
while(true){
char[] temp=new char[4];
int len=read4(temp);
for(int i=0;i<len;i++){
queue.offer(temp[i]);
}
len=Math.min(n-total,queue.size());
for(int i=0;i<len;i++){
buf[total++]=queue.poll();
}
if(len==0){
break;
}
}
return total;
}
}
659. Strings Serialization:点击打开链接
思路:以长度编码是常见的,也是行得通的方法
例如:strs={"lint","dog"};
encode:4$lint3$dog
decode:{"lint","dog"}
public class Solution {
/**
* @param strs a list of strings
* @return encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
String result="";
for(int i =0;i<strs.size();i++){
result+=strs.get(i).length()+"$"+strs.get(i);
}
return result;
}
/**
* @param str a string
* @return dcodes a single string to a list of strings
*/
public List<String> decode(String str) {
List<String> list=new ArrayList<>();
int start=0;
while(start<str.length()){
int sign=str.indexOf('$',start); //从start索引开始第一个位置出现$的索引
int size=Integer.valueOf(str.substring(start,sign)); //从索引start到sign取子字符串,包括头,不包括尾
list.add(str.substring(sign+1,sign+1+size));
start=sign+1+size;
}
return list;
}
}
645. Indentify Celebrity:点击打开链接
思路:设定最开始第0个为候选人,从1开始扫一遍,如果候选人认识i,i就是候选人,扫一遍之后得到一个最终候选人
然后来检验最终候选人是不是名人,如果与题给条件冲突,也就是候选人认识i或者i不认识候选人,就不是名人,否则就是名人。
注意:在检验是不是名人的时候,候选人不和自己比较,因此要candidate!=i
由于在找候选人的时候每遍历一个都会排除一个,因此时间复杂度为O(n)
/* The knows API is defined in the parent class Relation.
boolean knows(int a, int b); */
public class Solution extends Relation {
/**
* @param n a party with n people
* @return the celebrity's label or -1
*/
public int findCelebrity(int n) {
int candidate=0;
for(int i=1;i<n;i++){
if(knows(candidate,i)){
candidate=i;
}
}
for(int i=0;i<n;i++){
if(candidate!=i && (knows(candidate,i) || !knows(i,candidate))){
return -1;
}
}
return candidate;
}
}
419. Roman to Integer:点击打开链接
例如:VI=6, i=0,nums[i]>nums[i+1],result=result+nums[i]=0+5=5,return result+nums[1]=5+1=6
IV=4, i=0,nums[i]<nums[i+1],result=result-nums[i]=0-1=-1,return result+nums[1]=-1+5=4
public class Solution {
/**
* @param s Roman representation
* @return an integer
*/
public int romanToInt(String s) {
int[] nums=new int[s.length()];
for(int i=0;i<s.length();i++){
switch(s.charAt(i)){
case 'I':
nums[i]=1;
break;
case 'V':
nums[i]=5;
break;
case 'X':
nums[i]=10;
break;
case 'L':
nums[i]=50;
break;
case 'C':
nums[i]=100;
break;
case 'D':
nums[i]=500;
break;
case 'M':
nums[i]=1000;
break;
}
}
int result=0;
for(int i=0;i<nums.length-1;i++){
if(nums[i]<nums[i+1]){
result-=nums[i];
}else{
result+=nums[i];
}
}
return result+nums[nums.length-1];
}
}
418. Integer to Roman:点击打开链接
例如:12->XII
n=12,n>=10,n=n-10=2,result=result+X=X
n=2, n>=1, n=n-1=1, result=result+I=I
n=1, n>=1, n=n-1=1, result=result+I=I
public class Solution {
/**
* @param n The integer
* @return Roman representation
*/
public String intToRoman(int n) {
String[] romans={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int[] values={1000,900,500,400,100,90,50,40,10,9,5,4,1};
String result="";
for(int i=0;i<values.length;i++){
while(n>=values[i]){
n-=values[i];
result+=romans[i];
}
}
return result;
}
}