11. Container With Most Water
public int maxArea(int[] height) {
int l=0;
int r=height.length-1;
int area=0;
while(l<r){
area=Math.max(area,Math.min(height[l],height[r])*(r-l));
if(height[l]<height[r]){//高度大的不动
l++;
}else r--;
}
return area;
}
3. Longest Substring Without Repeating Characters
使用滑动窗口
public int lengthOfLongestSubstring(String st) {
int n=st.length();
int s=0,e=0,a=0;
HashSet<Character> set=new HashSet();
while(s<n&&e<n){
if(!set.contains(st.charAt(e))){
set.add(st.charAt(e++));
a=Math.max(a,e-s);
}else{
set.remove(st.charAt(s++));//重复则删除
}
}
return a;
}
264. Ugly Number II
找第n个丑数
public int nthUglyNumber(int n) {
int[] nums=new int[n];
nums[0]=1;
int f2=2,f3=3,f5=5;
int fp2=0,fp5=0,fp3=0;//最小的下标
for(int i=1;i<n;++i){
int t=Math.min(Math.min(f2,f3),f5);
nums[i]=t;
if(t==f2) {
f2=2*nums[++fp2];
}
if(t==f3) {
f3=3*nums[++fp3];
}
if(t==f5) {
f5=5*nums[++fp5];
}
}
return nums[n-1];
}
50. Pow(x, n)
使用递归
class Solution {
static double powhelper(double x, int n)
{
if (n == 0)
return 1;
double res = powhelper(x, n / 2);
if (n % 2 == 0)
return res * res;
else
return res * res * x;
}
double myPow(double x, int n)
{
// 二分实现 logn
if (n == 0)
return 1;
else if (n < 0)
return 1.0 / powhelper(x, -n);
return powhelper(x, n);
}
}
56. Merge Intervals
public List<Interval> merge(List<Interval> intervals) {
LinkedList<Interval> l=new LinkedList<Interval>();
Collections.sort(intervals,new Comparator<Interval>(){
public int compare(Interval a,Interval b){
return a.start-b.start;
}
});
for(Interval i:intervals){
if(l.isEmpty()||l.getLast().end<i.start){
l.add(i);
}else{//覆盖
l.getLast().end=Math.max(l.getLast().end,i.end);
}
}
return l;
}
73. Set Matrix Zeroes
public void setZeroes(int[][] matrix) {
if(matrix==null) return ;
int m=matrix.length;
int n=matrix[0].length;
Set<Integer> rows = new HashSet<Integer>();
Set<Integer> cols = new HashSet<Integer>();
for(int i=0;i<m;i++){
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
rows.add(i);
cols.add(j);
}
}
}
for(int i=0;i<m;i++){
for (int j = 0; j < n; j++) {
if(rows.contains(i)||cols.contains(j)){
matrix[i][j]=0;//存在set的行和列值为零
}
}}
}
98. Validate Binary Search Tree
要注意二叉搜索树的左子树的所有值都小于根节点值,右子树反之
ArrayList<Integer> l=new ArrayList();
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
if (root.left == null && root.right == null) return true;
toList(root);
for(int i=0;i<l.size()-1;i++){
if(l.get(i)>=l.get(i+1)) return false;
}
return true;
}
public void toList(TreeNode t){//中序遍历
if(t==null) return ;
toList(t.left);
l.add(t.val);
toList(t.right);
}
23. Merge k Sorted Lists
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
private static Comparator<ListNode> comp =new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
// TODO Auto-generated method stub
return o1.val-o2.val;
}
};
public static ListNode mergeKLists(List<ListNode> lists){
if(lists.size()==0||lists==null){
return null;
}
PriorityQueue<ListNode> queue=new PriorityQueue<ListNode>(lists.size(),comp);
for(int i=0;i<lists.size();i++){
ListNode node=lists.get(i);
while(node!=null){
ListNode temp=node;
node=node.next;
temp.next=null;
queue.add(temp);
}
}
ListNode result=new ListNode(-1);
ListNode t=result;
while(!queue.isEmpty()){
t.next=queue.poll();
t=t.next;
}
return result.next;
}
}
49. Group Anagrams
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
String就有valueOf()方法,意识是说,要把参数中给的值,转化为String类型,Integer的valueOf()就是把参数给的值,转化为Integer类型。
public List<List<String>> groupAnagrams(String[] strs) {
if(strs.length==0) return new ArrayList();
Map<String,List> map=new HashMap<String,List>();
for(String s:strs){
char[] ca=s.toCharArray();
Arrays.sort(ca);//排序后再查找
String key=String.valueOf(ca);
if(!map.containsKey(key)) map.put(key,new ArrayList());
map.get(key).add(s);
}
return new ArrayList(map.values());
}
7. Reverse Integer
public int reverse(int x) {
int res = 0;
while (x != 0) {
if (res > Integer.MAX_VALUE / 10 || res < Integer.MIN_VALUE / 10) {
return 0;//乘以10溢出就返回0
}
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
22. Generate Parentheses(n对括号)
public List<String> generateParenthesis(int n) {
List<String> list=new ArrayList<String>();
Parent("",list,n,n);
return list;
}
public void Parent(String s,List<String> list,int l,int r){
if(l==0&&r==0) list.add(s);
if(r>l) Parent(s+')',list,l,r-1);
if(l>0) Parent(s+'(',list,l-1,r);
}
62. Unique Paths
public int uniquePaths(int m, int n) {
if(m<=0||n<=0){
return 0;
}
if(m==1||n==1){
return 1;
}
int[][] arr=new int[n][m];
for(int i=0;i<n;i++){
arr[i][0]=1;
}
for(int i=0;i<m;i++){
arr[0][i]=1;
}
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
arr[i][j]=arr[i-1][j]+arr[i][j-1];
}
}
return arr[n-1][m-1];
}
102. Binary Tree Level Order Traversal
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> lists=new ArrayList<List<Integer>>();
if(root==null) return lists;
List<Integer> list=new ArrayList<Integer>();
lists.add(list);
TreeNode flag=root; //标识此层最后一个元素
LinkedList<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode node=queue.poll();
list.add(node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
if(node==flag&&!queue.isEmpty()){
list=new ArrayList();
lists.add(list);
flag=queue.getLast(); //队列的最后一个元素,此层最后一个元素
}
}
return lists;
}
347. Top K Frequent Elements
public List<Integer> topKFrequent(int[] nums, int k) {
List<Integer> list=new ArrayList<Integer>();
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
PriorityQueue <Map.Entry<Integer,Integer>> pq=new PriorityQueue <Map.Entry<Integer,Integer>>(new Comparator<Map.Entry<Integer,Integer>>(){
public int compare(Map.Entry<Integer,Integer> o1,Map.Entry<Integer,Integer>o2){
return o2.getValue()-o1.getValue();
}
});//优先队列是有最大堆实现的
for(int i=0;i<nums.length;i++){
if(map.containsKey(nums[i])){
map.put(nums[i],map.get(nums[i])+1);
}else
map.put(nums[i],1);
}
Set<Map.Entry<Integer,Integer>> s=map.entrySet();
for (Map.Entry<Integer,Integer> entry : s)
pq.add(entry);
for(int i=0;i<k;i++){
list.add(pq.poll().getKey());
}
return list;
}