leetcode记录
Posted 保护眼睛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode记录相关的知识,希望对你有一定的参考价值。
leetcode记录
796.旋转字符串
class Solution796 {
public boolean rotateString1(String s, String goal) {
return (s.length() == goal.length())&&(s+s).contains(goal);
}
public boolean rotateString(String s, String goal) {
if (s.equals("") && goal.equals("")) {
return true;
}
int len = s.length();
for (int i = 0; i < len; i++) {
String s1 = s.substring(0, 1);
String s2 = s.substring(1, len);
s = s2 + s2;
if (s.equals(goal)) {
return true;
}
}
return false;
}
}
700.二叉搜索树中的搜索
class Solution700 {
//非递归
public TreeNode searchBST(TreeNode root, int val) {
while (root != null){
if(root.val == val){
return root;
}else if(val > root.val){
root = root.right;
}else{
root = root.left;
}
}
return root;
}
//递归
public TreeNode searchBST1(TreeNode root, int val) {
if (root == null || root.val == val) return root;
else if (root.val > val)
root = searchBST1(root.left, val);
else
root = searchBST1(root.right, val);
return root;
}
}
589、590.N叉树的前、后序遍历
class Solution590 {
List<Integer> res = new ArrayList<>();
public List<Integer> postorder(Node root) {
if (root == null) return res;
for (Node cur : root.children) {
postorder(cur);
}
res.add(root.val);
return res;
}
}
class Solution589 {
List<Integer> res = new ArrayList<>();
public List<Integer> preorder(Node root) {
if (root == null) return res;
res.add(root.val);
for (Node cur : root.children) {
preorder(cur);
}
return res;
}
}
637.二叉树的层平均值
class Solution637 {
public List<Double> averageOfLevels(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<Double> res = new ArrayList<>();
if( root == null)return res;
queue.offer(root);
res.add(root.val*1.0);
while(!queue.isEmpty()){
int size = queue.size();
double sum = 0;
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
sum+=cur.val;
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!=null){
queue.offer(cur.right);
}
}
res.add(sum/size);
}
return res;
}
}
35.搜索位置插入
public static int searchInsert(int[] nums, int target) {
int l = 0;
int r = nums.length - 1;
while (l <= r) {
int mid = l + ((r - l)>>1);
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return l;
}
504.将给定十进制数转为七进制数
class Solution504 {
public String convertToBase7(int num) {
if(num == 0)return"0";
StringBuilder sb = new StringBuilder();
int flag = num >0 ? 1 : -1;
num = Math.abs(num);
while(num != 0){
sb.append(num % 7);
num = num / 7;
}
if(flag < 0){
sb.append("-");
}
return sb.reverse().toString();
}
}
501.二叉搜索树中的众数
class Solution501 {
int count = 0;
int maxFre = 0;
TreeNode pre = null;
ArrayList<Integer> list = new ArrayList<>();
public int[] findMode(TreeNode root) {
inOrder(root);
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
public void inOrder(TreeNode root) {
if (root == null) return;
inOrder(root.left);
if (pre == null || pre.val == root.val) {
count++;
} else {
count = 1;
}
if (count == maxFre) {
list.add(root.val);
} else if (count > maxFre) {
list.clear();
maxFre = count;
list.add(root.val);
}
pre = root;
inOrder(root.right);
}
}
530.二叉搜索树的最小绝对值差
//因为二叉搜索树的中序遍历时升序的,所以最小绝对值差就在相邻两个节点的差值之间
class Solution {
int res = Integer.MAX_VALUE;
TreeNode pre = null;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return res;
}
public void inOrder(TreeNode root){
if(root == null)return;
inOrder(root.left);
if( pre != null){
res = Math.min(root.val - pre.val,res);
}
pre = root;
inOrder(root.right);
}
}
448.找到所有数组中消失的数字
class Solution {
public static List<Integer> findDisappearedNumbers(int[] nums) {
int len = nums.length;
int index =0;
while (index<len) {
if (nums[index] == index + 1) {
index++;
}
else {
int targetIndex = nums[index] - 1;
if(nums[index] == nums[targetIndex]) {
index++;
continue;
}
int tmp = nums[targetIndex];
nums[targetIndex] = nums[index];
nums[index] = tmp;
}
}
List<Integer> res = new ArrayList<>();
for (int i = 0; i < len; i++) {
if(nums[i] != i+1){
res.add(i+1);
}
}
return res;
}
}
453.最小操作次数使数组元素相等
public static int minMoves(int[] nums) {
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
if(nums[i]<min){
min = nums[i];
}
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
res+=(nums[i] - min);
}
return res;
}
//正常思路会超时,将前n-1元素加1,就相当于将最后一个元素减1。
public static int minMoves1(int[] nums) {
if (nums.length == 1) return 0;
int count = 0;
while (!isEqu1(nums)) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
nums[i] = nums[i] + 1;
}
count++;
}
return count;
}
public static boolean isEqu1(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == array[i + 1]) {
continue;
} else {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] array = {1, 1000000000};
long start = System.currentTimeMillis();
System.out.println(minMoves1(array));
long end = System.currentTimeMillis();
System.out.println(end - start);
int[] array2 = {1, 1000000000};
long start2 = System.currentTimeMillis();
System.out.println(minMoves(array2));
long end2 = System.currentTimeMillis();
System.out.println(end2 - start2);
}
//运行结果
999999999
7681
999999999
0
461.汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
//将两个数字异或之后,统计1的个数,每次右移一位,结果记录到res中
public static int hammingDistance(int x, int y) {
int res = 0;
int z = x^y;
while(z!=0){
res+= z&1;
z >>=1;
}
return res;
}
以上是关于leetcode记录的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段
错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段