问题 Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
publicclassSolution { // you need to treat n as an unsigned value publicinthammingWeight(int n) { intcount=0; while(n != 0){ n = (n & (n-1)); count++; } return count; } }
问题 Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
问题 You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
问题 Given a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
问题 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
classSolution { List<List<Integer>> ans; public List<List<Integer>> combine(int n, int k) { ans = newArrayList<>(); backTrack(newLinkedList(),1,n,k); return ans; } privatevoidbackTrack(LinkedList<Integer> list, int start, int n, int k){ if (k == 0){ ans.add(newArrayList(list)); return; }
for (inti= start; i <= n-k+1; i++){ list.add(i); backTrack(list, i+1, n, k-1); list.removeLast(); } } }