804. Unique Morse Code Words
先把所有的字母以及对应的morse code存储到map里面,然后对于words的每一个,把对应的transformation存储到一个hashmap里面,要是里面有对应的转换的话,就continue;没有的话就添加,返回transformation这个map的size就好。
import java.util.HashMap;
class Solution {
public int uniqueMorseRepresentations(String[] words) {
if(words==null||words.length==0){
return 0;
}
/**
a".-",b"-...",c"-.-.",d"-..",e".",
f"..-.",g"--.",h"....",i"..",j".---",
k"-.-",l".-..",m"--",n"-.",o"---",
p".--.",q"--.-",r".-.",s"...",t"-",
u"..-",v"...-",w".--",x"-..-",y"-.--",z"--.."
*/
String[] morseCode={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
String[] letters={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
HashMap<String,String> map=new HashMap<>();
for(int i=0;i<26;i++){
map.put(letters[i],morseCode[i]);
}
//System.out.println(map.entrySet());
HashMap<String,Integer> transformations=new HashMap<String,Integer>(); //返回有几种transformations
for(int i=0;i<words.length;i++){
StringBuilder sb=new StringBuilder();
for(int j=0;j<words[i].length();j++){
String code=map.get(String.valueOf(words[i].charAt(j)));
sb.append(code);
}
System.out.println(sb.toString());
if(transformations.containsKey(sb)){
continue;
}else{
transformations.put(sb.toString(),0);
}
}
return transformations.size();
}
}
直接把每个字母及其对应的morse code存储到一个数组里面,每一个位置的索引即为对应的字符的ascii码减去'a'的ascii码。
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] MORSE = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."};
Set<String> seen = new HashSet();
for (String word: words) {
StringBuilder code = new StringBuilder();
for (char c: word.toCharArray())
code.append(MORSE[c - 'a']);
seen.add(code.toString());
}
return seen.size();
}
}
657. Judge Route Circle
import java.util.HashMap;
//往左走和往右走的步数要一样;往上走和往下走的步数要一样
class Solution {
public boolean judgeCircle(String moves) {
if(moves==null||moves.length()==0){
return true;
}
//char[] move={'R','L','U','D'};
HashMap<Character,Integer> map=new HashMap<>();
map.put('R',0);
map.put('L',0);
map.put('U',0);
map.put('D',0);
for(int i=0;i<moves.length();i++){
int count=map.get(moves.charAt(i));
map.replace(moves.charAt(i),++count);
//System.out.println(moves.charAt(i)+" "+map.get(moves.charAt(i)));
//System.out.println(map.size());
}
//System.out.println(map.get('U')+" "+map.get('D'));
int rcount=map.get('R');
int lcount=map.get('L');
int ucount=map.get('U');
int dcount=map.get('D');
if(rcount==lcount&&ucount==dcount){
return true;
}
return false;
}
}
637. Average of Levels in Binary Tree
一道刚开始没思路的题。使用广度优先搜索。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.LinkedList;
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
if(root==null){
return null;
}
LinkedList<Double> list=new LinkedList<>();
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()){
int n=queue.size(); //这一层有n个节点
double sum=0.0;
for(int i=0;i<n;i++){
TreeNode x=queue.poll();
sum+=x.val;
if(x.left!=null){
queue.add(x.left);
}
if(x.right!=null){
queue.add(x.right);
}
}
list.add(sum/n);
}
return list;
}
}
104. Maximum Depth of Binary Tree
递归。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left=1;
int right=1;
if(root.left!=null){
left+=maxDepth(root.left);
}
if(root.right!=null){
right+=maxDepth(root.right);
}
return Math.max(left,right);
}
}
695. Max Area of Island
递归。使用一个二维矩阵visited记录每个位置是否被访问过,初始化visited中所有位置都为false。对于矩阵中的每一个位置,如果没有被访问过且值为1的话,那么把岛屿的area加1,然后再检查它上下左右的位置,注意,在检查上下左右位置的时候要先保证上下左右的位置都不出界!同样,满足上述条件时,area加1,递归!
class Solution {
public int maxAreaOfIsland(int[][] grid) {
if(grid==null||grid.length<0){
return 0;
}
int max=0;
//int count=0;
int row=grid.length;
int column=grid[0].length;
boolean[][] visited=new boolean[row][column];
//对于grid中的每一个区域
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
//int count=0;
//如果没有被访问
//int area=0;
int res=0;
if(!visited[i][j]&&grid[i][j]==1){
res=check(grid,visited,i,j,0);
}
if(max<res){
max=res;
}
}
}
return max;
}
public int check(int[][] grid,boolean[][] visited,int i,int j,int area){
if(i>=0&&i<grid.length&&j>=0&&j<grid[0].length){
//如果没有被访问,且对应位置是1
if(!visited[i][j]&&grid[i][j]==1){
area++;
visited[i][j]=true;
area=check(grid,visited,i,j-1,area);
area=check(grid,visited,i,j+1,area);
area=check(grid,visited,i-1,j,area);
area=check(grid,visited,i+1,j,area);
}
}
return area;
}
}
283. Move Zeroes
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
一开始的思路:参照快排,一个nozero的区域,一个zero的区域,但是后来发现不可以,因为会改变非0数字的出现顺序。
挨个遍历,找到第一个为0的数字,记下他的位置,每次遇到不为0的数,就和第一个0交换位置。
class Solution {
public void moveZeroes(int[] nums) {
if(nums==null||nums.length==0){
return;
}
int start=0;
int firstZero=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
if(i>firstZero){
continue;
}
firstZero=i;
}else{
swap(nums,i,firstZero);
firstZero++;
}
}
}
public void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}