920. 会议室
给定一系列的会议时间间隔,包括起始和结束时间[[s1,e1],[s2,e2],…(si < ei),确定一个人是否可以参加所有会议。
样例
给定区间=[[0,30],[5,10],[15,20]],返回false。
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
public boolean canAttendMeetings(List<Interval> intervals) {
// Write your code here
if(intervals==null||intervals.size()==0)
return true;
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval o1, Interval o2) {
return o1.start-o2.start;
}
});
int end=intervals.get(0).end;
for(int i=1;i<intervals.size();i++){
if(intervals.get(i).start<end)
return false;
end=Math.max(end,intervals.get(i).end);
}
return true;
}
}
973. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented
by one bit 0. The second character can be represented by two bits (10
or 11).Now given a string represented by several bits. Return whether the
last character must be a one-bit character or not. The given string
will always end with a zero.
样例
Example 1:
Input:
bits = [1, 0, 0]
Output: True
Explanation:
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
Example 2:
Input:
bits = [1, 1, 1, 0]
Output: False
Explanation:
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
看半天没怎么看懂题目的意思,但答案是异常的简单,奇怪啊
public class Solution {
/**
* @param bits: a array represented by several bits.
* @return: whether the last character must be a one-bit character or not
*/
public boolean isOneBitCharacter(int[] bits) {
// Write your code here
int n=bits.length,i=0;
while (i<n-1){
i+=bits[i]+1;
}
return i==n-1;
}
}
977. Base 7
Given an integer, return its base 7 string representation.
样例
Given num = 100, return “202”.
Given num = -7, return “-10”.
public class Solution {
/**
* @param num: the given number
* @return: The base 7 string representation
*/
int n=7;
String[]str={"0","1","2","3","4","5","6"};
String result="";
int flag;
public String convertToBase7(int num){
if(num<0)
flag=1;
num=Math.abs(num);
result=str[num%n]+result;
if(num/n==0) return flag==1?"-"+result:result;
return convertToBase7(num/n);
}
}
987. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits:
namely, if two adjacent bits will always have different values.
样例
Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.
Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.
Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
public class Solution {
/**
* @param n: a postive Integer
* @return: if two adjacent bits will always have different values
*/
public boolean hasAlternatingBits(int n) {
// Write your code here
int n1=n>>1;
int sum=n1+n;
if((sum&(sum+1))==0)
return true;
else return false;
}
}
988. Arranging Coins
You have a total of n coins that you want to form in a staircase
shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be
formed.n is a non-negative integer and fits within the range of a 32-bit
signed integer.
样例
Example 1:
n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
public class Solution {
/**
* @param n: a non-negative integer
* @return: the total number of full staircase rows that can be formed
*/
public int arrangeCoins(int n) {
// Write your code here
int cur=1,res=n-1;
while (res>=cur+1){
++cur;
res-=cur;
}
return n==0?0:cur;
}
}