力扣---2020.9.4
257. 二叉樹的所有路徑
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
dfs(root,"",res);
return res;
}
public void dfs(TreeNode root,String str,List<String> res){
if(root==null){
return;
}
str += root.val;
if(root.left ==null && root.right == null){
res.add(str);
}else{
dfs(root.left,str+"->",res);
dfs(root.right,str+"->",res);
}
}
}
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> paths = new ArrayList<String>();
if (root == null) {
return paths;
}
Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
Queue<String> pathQueue = new LinkedList<String>();
nodeQueue.offer(root);
pathQueue.offer(Integer.toString(root.val));
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.poll();
String path = pathQueue.poll();
if (node.left == null && node.right == null) {
paths.add(path);
} else {
if (node.left != null) {
nodeQueue.offer(node.left);
pathQueue.offer(new StringBuffer(path).append("->").append(node.left.val).toString());
}
if (node.right != null) {
nodeQueue.offer(node.right);
pathQueue.offer(new StringBuffer(path).append("->").append(node.right.val).toString());
}
}
}
return paths;
}
}
78. 子集
class Solution {
List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
List<Integer> track = new ArrayList<>();
backtrack(nums,track,0);
return list;
}
public void backtrack(int[] nums,List<Integer> track,int start){
list.add(new ArrayList(track));
for (int i = start; i < nums.length; i++) {
track.add(nums[i]);
backtrack(nums,track,i+1);
track.remove(track.size()-1);
}
}
}
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
for(int i = 0;i < nums.length;i++){
int all = res.size();
for(int j = 0;j < all;j++){
List<Integer> tmp = new ArrayList<>(res.get(j));
tmp.add(nums[i]);
res.add(tmp);
}
}
return res;
}
}
461. 漢明距離
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
class Solution {
public int hammingDistance(int x, int y) {
int res = 0;
int n = x ^ y;
while(n>0){
res += n&1;
n >>= 1;
}
return res;
}
}
class Solution {
public int hammingDistance(int x, int y) {
int count=0;
if(x==y)
return 0;
while(x!=0||y!=0){
if(x%2!=y%2)
count++;
x=x/2;
y=y/2;
}
return count;
}
}
class Solution {
public int hammingDistance(int x, int y) {
int count=0;
int n = x^y;
while(n!=0){
count++;
n = n&(n-1);
}
return count;
}
}
你知道的越多,你不知道的越多。
相關文章
- 力扣---2020.7.30力扣
- 力扣---2020.9.27力扣
- 力扣---2020.9.29力扣
- 力扣---2020.9.28力扣
- 力扣---2020.9.3力扣
- 力扣2713 2024.6.19力扣
- 力扣2589 5.16力扣
- 力扣1542 2024.5.22力扣
- 力扣題解力扣
- 力扣(LeetCode)543力扣LeetCode
- 力扣(LeetCode)934力扣LeetCode
- 力扣(LeetCode)103力扣LeetCode
- 力扣(LeetCode)513力扣LeetCode
- 力扣(LeetCode)389力扣LeetCode
- 力扣(LeetCode)796力扣LeetCode
- 力扣(LeetCode)863力扣LeetCode
- 力扣(LeetCode)310力扣LeetCode
- 力扣(LeetCode)130力扣LeetCode
- 力扣(LeetCode)965力扣LeetCode
- 力扣社群開通力扣
- 力扣-9.23-680力扣
- 力扣之按身高排序力扣排序
- 力扣之移動零力扣
- 力扣之兩數之和力扣
- 教你如何玩轉力扣力扣
- 力扣oj-字串相乘力扣字串
- 力扣最長公共字首力扣
- 力扣-48 旋轉影像力扣
- 力扣27. 移除元素力扣
- 力扣-376. 擺動序列力扣
- 力扣 22. 括號生成力扣
- 力扣-231. 2 的冪力扣
- 力扣-283. 移動零力扣
- 力扣-54. 螺旋矩陣力扣矩陣
- leetcode力扣 213. 打家劫舍 IILeetCode力扣
- 力扣之有效的迴文力扣
- 力扣之存在重複元素力扣
- 力扣#43 字串相乘(C++)力扣字串C++