394. 字符串解码*

  1. Stack<Integer>numStack=new Stack<>();
  2. Stack<StringBuilder> opeStack=new Stack<>();
  3. StringBuilder builder=new StringBuilder();
  4. int count=0;
  5. for (char c : s.toCharArray()) {
  6. if(c>='0'&&c<='9'){
  7. count=10*count+c-'0';
  8. }else if(c=='['){
  9. numStack.push(count);
  10. opeStack.push(builder);
  11. builder=new StringBuilder();
  12. count=0;
  13. }else if(c==']'){
  14. int num=numStack.pop();
  15. StringBuilder temp = opeStack.pop();
  16. for(int i=0;i<num;i++){
  17. temp.append(builder);
  18. }
  19. builder=temp;
  20. }else{//如果是字符
  21. builder.append(c);
  22. }
  23. }
  24. return builder.toString();

438. 找到字符串中所有字母异位词*

  1. //使用int数组+滑动窗口来判断
  2. List<Integer>res=new ArrayList<>();
  3. if(s.length()<p.length()){
  4. return res;
  5. }
  6. int[]sMap=new int[26];
  7. int[]pMap=new int[26];
  8. for(int i=0;i<p.length();i++){
  9. sMap[s.charAt(i)-'a']++;
  10. pMap[p.charAt(i)-'a']++;
  11. }
  12. if(Arrays.equals(sMap,pMap)){
  13. res.add(0);
  14. }
  15. for(int i=0;i<s.length()-p.length();i++){
  16. sMap[s.charAt(i)-'a']--;
  17. sMap[s.charAt(i+p.length())-'a']++;
  18. if(Arrays.equals(sMap,pMap)){
  19. res.add(i+1);
  20. }
  21. }
  22. return res;

448. 找到所有数组中消失的数字*

  1. class Solution {
  2. public List<Integer> findDisappearedNumbers(int[] nums) {
  3. int left=0;
  4. int right=nums.length;
  5. while (left<right){
  6. if(nums[left]!=left+1&&nums[nums[left]-1]!=nums[left]){
  7. swap(nums,left,nums[left]-1);
  8. }else if(nums[left]!=left+1&&nums[nums[left]-1]==nums[left]){
  9. left++;
  10. }else if(nums[left]==left+1){
  11. left++;
  12. }
  13. }
  14. List<Integer> res = new ArrayList<>();
  15. for(int i=0;i<nums.length;i++){
  16. if(nums[i]!=i+1){
  17. res.add(i+1);
  18. }
  19. }
  20. return res;
  21. }
  22. private void swap(int[]nums,int left,int right){
  23. int temp=nums[right];
  24. nums[right]=nums[left];
  25. nums[left]=temp;
  26. return;
  27. }
  28. }

461. 汉明距离

  1. int num=x^y;
  2. int res=0;
  3. while (num!=0){
  4. num&=num-1;
  5. res++;
  6. }
  7. return res;

543. 二叉树的直径

  1. if(root==null){
  2. return 0;
  3. }
  4. int left = dfs(root.left);
  5. int right = dfs(root.right);
  6. res=Math.max(res,left+right);
  7. return Math.max(left,right)+1;

560. 和为 K 的子数组

  1. if(nums.length==1&&k!=nums[0]){
  2. return 0;
  3. }
  4. Map<Integer,Integer> map=new HashMap<>();
  5. map.put(0,1);
  6. int sum=0;
  7. int res=0;
  8. for(int i=0;i<nums.length;i++){
  9. sum+=nums[i];
  10. map.put(sum,map.getOrDefault(sum,0)+1);
  11. if(map.containsKey(sum-k)){
  12. res+=map.get(sum-k);
  13. }
  14. }
  15. return res;

581. 最短无序连续子数组

  1. int right=-1;
  2. int rightNums=nums[0];
  3. for(int i=1;i<nums.length;i++){
  4. if(nums[i]<rightNums){
  5. right=i;
  6. }else{
  7. rightNums=nums[i];
  8. }
  9. }
  10. int left=0;
  11. int leftNums=nums[nums.length-1];
  12. for(int i=nums.length-2;i>=0;i--){
  13. if(nums[i]>leftNums){
  14. left=i;
  15. }else{
  16. leftNums=nums[i];
  17. }
  18. }
  19. return right-left+1;

621. 任务调度器

image.png

  1. if(n==0){
  2. return tasks.length;
  3. }
  4. int[]map=new int[26];
  5. for(int i=0;i<tasks.length;i++){
  6. map[tasks[i]-'A']++;
  7. }
  8. Arrays.sort(map);
  9. int maxTask=map[25];
  10. int maxCount=1;
  11. for(int i=25;i>=1;i--){
  12. if(map[i]==map[i-1]){
  13. maxCount++;
  14. }else{
  15. break;
  16. }
  17. }
  18. int res=maxTask*(n+1)+maxCount;
  19. return Math.max(tasks.length,res);

1044. 最长重复子串*

  1. class Solution {
  2. public String longestDupSubstring(String s) {
  3. String res="";
  4. int left=0;
  5. int right=s.length()-1;
  6. while (left<=right){
  7. int mid=(left+right+1)/2;
  8. String result = check(s, mid);
  9. if(!result.equals("")){
  10. res=result;
  11. left=mid+1;
  12. }else{
  13. right=mid-1;
  14. }
  15. }
  16. return res;
  17. }
  18. private String check(String s,int len){
  19. String res="";
  20. Set<Integer>set=new HashSet<>();
  21. for(int i=0;i<=s.length()-len;i++){
  22. String substring = s.substring(i, i + len);
  23. int hashCode=substring.hashCode();
  24. if(set.contains(hashCode)&&s.indexOf(substring)!=i){
  25. res=s.substring(i,i+len);
  26. return res;
  27. }
  28. set.add(hashCode);
  29. }
  30. return res;
  31. }
  32. }