package Solution领扣网;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
public class Solution349 {
//两个数组的交集|
//Set用来去重复
public static void main(String[] args) {
int[] nums1=new int[]{1,2,3,4,5,6};
int[] nums2=new int[]{3,4,5,6,2,1};
System.out.println(Arrays.toString(intersection(nums1,nums2)));
}
public static int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> hashSet1=new HashSet<Integer>();
HashSet<Integer> hashSet2=new HashSet<Integer>();
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=0;i<nums1.length;i++){
hashSet1.add(nums1[i]);
}
for(int i=0;i<nums2.length;i++){
hashSet2.add(nums2[i]);
}
Iterator<Integer> hashSetIt1=hashSet1.iterator();
while(hashSetIt1.hasNext()){
int e=hashSetIt1.next();
if(hashSet2.contains(e)){
list.offer(e);
}
}
int i=0;
int[] arrnew=new int[list.size()];
while(!list.isEmpty()){
arrnew[i++]=list.poll();
}
return arrnew;
}
}
package Solution领扣网;
import java.util.HashSet;
public class Solution804 {
//唯一的摩尔斯密码
public static void main(String[] args) {
String[] words={"gin", "zen", "gig", "msg"};
System.out.println(uniqueMorseRepresentations(words));
}
public static int uniqueMorseRepresentations(String[] words) {
String[] s={".-","-...","-.-.","-..",".","..-.",
"--.","....","..",".---","-.-",".-..",
"--","-.","---",".--.","--.-",".-.","...",
"-","..-","...-",".--","-..-","-.--","--.."};
HashSet<String> string=new HashSet<String>();
String s1="";
for(int i=0;i<words.length;i++){
for(int j=0;j<words[i].length();j++){
s1+=s[words[i].charAt(j)-'a'];
}
string.add(s1);
s1="";
}
return string.size();
}
}
package Solution领扣网;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
public class Solution350 {
//两个数组的交集||
public static void main(String[] args) {
int[] nums1={1,2,2,1};
int[] nums2={2,2};
// int[] nums1={4,9,5};
// int[] nums2={9,4,9,8,4};
System.out.println(Arrays.toString(intersect(nums1,nums2)));
}
public static int[] intersect(int[] nums1, int[] nums2) {
//创建一个链表用于存储交集元素的个数
LinkedList<Integer> linkedList=new LinkedList<Integer>();
//创建两个哈希map,存放元素与元素的个数
HashMap<Integer,Integer> hashMap1=new HashMap<Integer,Integer>();
HashMap<Integer,Integer> hashMap2=new HashMap<Integer,Integer>();
//记录单个元素的个数
int count=0;
//遍历数组当中的元素
for (int i : nums1) {
for (int j = 0; j < nums1.length; j++) {
if(i==nums1[j]){
count++;
}
}
//元素与元素的个数
hashMap1.put(i, count);
count=0;
}
count=0;
for (int i : nums2) {
for(int j=0;j<nums2.length;j++){
if(i==nums2[j]){
count++;
}
}
hashMap2.put(i, count);
count=0;
}
Set<Integer> set=hashMap1.keySet();
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
int e=it.next();
if(hashMap2.containsKey(e)){
for(int i=0;i<(hashMap1.get(e)>=hashMap2.get(e)?hashMap2.get(e):hashMap1.get(e));i++){
linkedList.add(e);
}
}
}
int left=0;
int[] arrnew=new int[linkedList.size()];
while(!linkedList.isEmpty()){
int e=linkedList.pop();
arrnew[left++]=e;
}
return arrnew;
}
/*
//遍历键的集合
Set<String> set = map2.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "=" + map2.get(key));
}
//遍历值的集合
Collection<String> list = map2.values();
Iterator<String> it2 = list.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
*/
}
package Solution领扣网;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
public class Solution347 {
//前k个高频元素
public static void main(String[] args) {
//1 1
//1,2,2,2,3,3,4,4,4,5 2
//1,2,2,2,3,3,3,3
// int[] nums={1,1,1,2,2,2,3};
// int[] nums={1};
// int[] nums={1,2,2,2,3,3,4,4,4,5};
int[] nums={1,2,2,2,3,3,3,3};
System.out.println(Arrays.toString(topKFrequent(nums,2)));
}
public static int[] topKFrequent(int[] nums, int k) {
//创建一个链表
LinkedList<Integer> linkedList=new LinkedList<Integer>();
//创建一个k大小的数组,用来存放高频的数值
int[] arrnew=new int[k];
//创建一个哈希map用来存数字与次数
HashMap<Integer,Integer> hashMap=new HashMap<Integer,Integer>();
//记录单个元素的个数
int count=0;
//遍历数组当中的元素
for (int i : nums) {
for (int j = 0; j < nums.length; j++) {
if(i==nums[j]){
count++;
}
}
//元素与元素的个数
hashMap.put(i, count);
count=0;
}
//创建比较方法
ArrayList<HashMap.Entry<Integer,Integer>> list=new ArrayList<HashMap.Entry<Integer,Integer>>(hashMap.entrySet());
Collections.sort(list,new Comparator<HashMap.Entry<Integer,Integer>>(){
@Override
public int compare(HashMap.Entry<Integer, Integer> o1,
HashMap.Entry<Integer, Integer> o2) {
return o2.getValue()-(o1.getValue());
}
});
//合适的数字放入数组当中
for (HashMap.Entry<Integer, Integer> entry : list) {
int e=entry.getKey();
linkedList.offer(e);
}
for (int i = 0; i < k; i++) {
arrnew[i]=linkedList.get(i);
}
return arrnew;
}
}
package Solution领扣网;
import java.util.ArrayList;
import java.util.List;
public class Solution54 {
//螺旋矩阵,二维数组的回型输出
public static void main(String[] args) {
int[][] matrix={{1},
{2},
{3}};//3 1
System.out.println(spiralOrder(matrix));
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list=new ArrayList<Integer>();
if(matrix.length==0){
return list;
}
//边界值的初始化
int left=0,right=matrix[0].length-1;
int bootom=0,top=matrix.length-1;
//满足的条件
while(left<=right&&bootom<=top){
//从列开始
for(int j=left;j<=right;j++){
list.add(matrix[bootom][j]);
}
//行加一
bootom++;
//从行开始
for(int i=bootom;i<=top;i++){
list.add(matrix[i][right]);
}
//列减一
right--;
for(int j=right;j>=left&&top>=bootom;j--){
list.add(matrix[top][j]);
}
//行减一
top--;
for(int i=top;i>=bootom&&right>=left;i--){
list.add(matrix[i][left]);
}
//列加一
left++;
}
return list;
}
}
48题把握住题中的创建数的问题,读懂题,这里也是涉及到行列交换
思路:用外层循环控制数组中的每一个数,从此数到数组的结尾分为一个层,找每层的最大数在比较
public class task4 {
//打印九九乘法表
//回形树输出的打印 矩阵和方阵
public static void main(String[] args) {
//测试数组为空
int[][] arr=new int[0][0];
//测试数组为方阵
int[][] arr1=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
//测试数组为矩阵
int[][] arr2=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};
// numberCount();
// huiXingShu(arr);
// huiXingShu(arr1);
huiXingShu(arr2);
}
public static void numberCount(){
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
System.out.printf("%-3d",i*j);
System.out.print(" ");
}
System.out.println();
}
}
/**
* 0 1 2 3
*
* 1 2 3 4 0
* 5 6 7 8 1
* 9 10 11 12 2
* 13 14 15 16 3
*
* i不变,j递增
* i递增,j不变
* i不变,j递减
* i递减,j不变
* */
//从回形树的有序输出,更好的明白边界对代码的作用
public static void huiXingShu(int[][] arr){
if(arr.length==0){
System.out.print("数组为空!");
return;
}
//矩阵
//左边界、右边界、上边界、下边界
int left=0,right=arr[left].length-1,top=0,bottom=arr.length-1;
//用while循环,循环条件left<=right&&top<=bottom
while(left<=right&&top<=bottom){
//i不变,j递增
for(int j=left;j<=right;j++){
System.out.print(arr[top][j]+" ");
}
top++;
//i递增,j不变
for(int i=top;i<=bottom;i++){
System.out.print(arr[i][right]+" ");
}
right--;
//i不变,j递减
for(int j=right;j>=left;j--){
System.out.print(arr[bottom][j]+" ");
}
bottom--;
for(int i=bottom;i>=top;i--){
System.out.print(arr[i][left]+" ");
}
left++;
}
}
}
package Solution领扣网;
import java.util.LinkedList;
public class Solution20 {
//括号匹配原则
public static void main(String[] args) {
String s="([)]";//([)],{[]},()[]{}
System.out.println(isValid(s));
}
public static boolean isValid(String s){
//创建一个栈,用栈的属性先进后出
LinkedList<Character> stack=new LinkedList<Character>();
//循环遍历字符串当中的每一个元素
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
//判断栈是否为空,为空的话直接进栈
if(stack.isEmpty()){
stack.push(c);
}else{
//先拿出栈顶的元素与当前的字符c进行比较
char top=stack.peek();
//这里解释一下为什么是-1和-2
//字符在相减的过程中,用的是ASCII当中的码值(数字)进行相减,
//所以会是数字,并且括号之间的差值只有-1和-2
if(top-c==-1||top-c==-2){
//如果栈顶元素匹配,栈顶元素弹栈
stack.pop();
}else{
//如果栈顶元素不匹配,栈顶元素入栈
stack.push(c);
}
}
}
//进行栈的判空操作,若果不为空的,则说明括号不匹配
if(!stack.isEmpty()){
return false;
}
return true;
}
}
package Solution领扣网;
import java.util.LinkedList;
public class Solution151 {
//翻转字符串当中的单词
public static void main(String[] args) {
// String s="";
// String s="the sky is blue";
// String s=" hello world! ";
String s="a good example";
System.out.println(reverseWords(s));
}
public static String reverseWords(String s) {
//""空字符串
if(s.length()==0){
return "";
}
//" "空格字符串
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==' '){
count++;
}
}
if(count==s.length()){
return "";
}
//用栈的先进后出
LinkedList<String> stackString=new LinkedList<String>();
//遍历字符串当中元素,让单词进栈,进栈时删掉多余的空格
String s1="";
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
s1+=s.charAt(i);
}else{
if(s1!=""){
stackString.push(s1);
s1="";
}
}
}
stackString.push(s1);
s1="";
//单词弹栈,用空字符串拼接
while(!stackString.isEmpty()){
s1+=stackString.pop()+" ";
}
//处理两边的空格
int left=0;
int right=s1.length()-1;
while(true){
//当两边都不是空格时退出循环
if(s1.charAt(left)!=' '&&s1.charAt(right)!=' '){
break;
}
//如果左边有空格,left++
if(s1.charAt(left)==' '){
left++;
}
//如果右边有空格,right--
if(s1.charAt(right)==' '){
right--;
}
}
//截取相应的长度
s1=s1.substring(left,right+1);
return s1;
}
}
package Solution领扣网;
public class Solution59 {
public static void main(String[] args) {
generateMatrix(3);
}
public static int[][] generateMatrix(int n) {
//创建一个大小为n的二维数组
int count=1;
int[][] arr=new int[n][n];
int left=0;
int right=arr.length-1;
int bootom=0;
int top=arr.length-1;
while(left<=right&&bootom<=top){
for(int j=left;j<=right;j++){
arr[bootom][j]=count++;
}
bootom++;
for(int i=bootom;i<=top;i++){
arr[i][right]=count++;
}
right--;
for(int j=right;j>=left;j--){
arr[top][j]=count++;
}
top--;
for(int i=top;i>=bootom;i--){
arr[i][left]=count++;
}
left++;
}
return arr;
}
}
package Solution领扣网;
public class Solution67 {
//二进制字符串的加减
public static void main(String[] args) {
String a="11011";
String b="1001111";
//2212101 翻转后 1012122
//0101011 翻转后 1101010
String c="0001111";
String d="1101110";
//1222011 翻转后 1102221
//1011111 翻转后 1111101
String e="111";
String f="101";
System.out.println(addBinary(e,f));
}
public static String addBinary(String a, String b) {
//三种情况:00 01 10 11
//用while循环
int righta=a.length()-1;
int rightb=b.length()-1;
String s="";
while(true){
if(a.charAt(righta)=='0'&&b.charAt(rightb)=='0'){
s+="0";
}else if(a.charAt(righta)=='1'&&b.charAt(rightb)=='1'){
s+="2";
}else{
s+="1";
}
if(righta==0||rightb==0){
break;
}
righta--;
rightb--;
}
if(righta==0&&rightb>0){
for(int i=rightb-1;i>=0;i--){
s+=b.charAt(i);
}
}
if(righta>0&&rightb==0){
for(int i=righta-1;i>=0;i--){
s+=a.charAt(i);
}
}
String string="";
string=resver(s);
//根据进制把字符串规正化
int left=string.length()-1;
String rightstring="";
String addstring="";
while(left>=0){
if(string.charAt(left)=='2'){
if(rightstring!=""){
addstring+="1";
}else{
addstring+='0';
rightstring="1";
}
}
if(string.charAt(left)=='1'){
if(rightstring!=""){
addstring+="0";
rightstring="1";
}else{
addstring+='1';
}
}
if(string.charAt(left)=='0'){
if(rightstring!=""){
addstring+='1';
rightstring="";
}else{
addstring+='0';
}
}
left--;
}
if(rightstring!=""){
addstring+=1;
}
addstring=resver(addstring);
return addstring;
}
public static String resver(String s){
//翻转字符串
String string="";
for(int i=s.length()-1;i>=0;i--){
string+=s.charAt(i);
}
return string;
}
}