![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5872b6fa5f188deb37c39bd5ec8a8f38.png)
完整代码刻在eclipse运行
package leetcode1230;
import java.util.*;
public class singleNumber {
public static void main(String[] args) {
int[] arr1 = {1,1,1,2,2,5,5,5,2,100};
System.out.println(f(arr1));
}
public static int f(int[] nums) {
Map<Integer,Integer> hash = new HashMap<>();
for(int i=0; i<nums.length; i++){
if(hash.containsKey(nums[i])){
hash.put(nums[i],hash.get(nums[i])+1);
}
else{
hash.put(nums[i], 1);
}
}
for(int i=0; i<nums.length; i++){
if(hash.get(nums[i])==1){
return nums[i];
}
}
return -1;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/edc02cf7526d75dbe63e5c44bf05ba38.png)
package leetcode1230;
import java.util.*;
public class twoSum {
public static void main(String[] args) {
int[] nums = {2,7,11,15};
int target = 9;
int[] ret = f(nums,target);
for(int i=0; i<ret.length; i++){
System.out.println(ret[i]);
}
}
public static int[] f(int[] nums, int target) {
Map<Integer,Integer> hash = new HashMap<>();
for(int i=0; i<nums.length; i++){
if(hash.containsKey(nums[i])){
hash.put(nums[i],hash.get(nums[i])+1);
}
else{
hash.put(nums[i],1);
}
}
for(int i=0; i<nums.length; i++){
if(hash.containsKey(target-nums[i])){
if(nums[i]==(target-nums[i])){
if(hash.get(nums[i])>1){
return new int[] {nums[i],target-nums[i]};
}
else{
return null;
}
}
return new int[] {nums[i],target-nums[i]};
}
}
return null;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d802d3d506124b5bd4fa9929d7eb130a.png)
第一段代码是错的,还未找到错误原因
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> que = new LinkedList<>();
List<List<Integer>> ret = new ArrayList<>();
if(root != null) que.add(root);
else return ret;
int level = 0;
while(!que.isEmpty()){
int size = que.size();
LinkedList<Integer> ls = new LinkedList<>();
for(int i=0; i<size; i++){
TreeNode ans = que.poll();
if(level % 2 == 1) ls.addLast(ans.val);
else ls.addFirst(ans.val);
if(ans.left!=null){
que.add(ans.right);
}
if(ans.right!=null){
que.add(ans.left);
}
}
level++;
ret.add(ls);
}
return ret;
}
}
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if(root != null) queue.add(root);
while(!queue.isEmpty()) {
LinkedList<Integer> tmp = new LinkedList<>();
for(int i = queue.size(); i > 0; i--) {
TreeNode node = queue.poll();
if(res.size() % 2 == 0) tmp.addLast(node.val);
else tmp.addFirst(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
res.add(tmp);
}
return res;
}
}