class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
while(left <= right){
int mid = left - ((right-left)>>1);
if(nums[mid] < target){
left = mid + 1;
}
else if(nums[mid] > target){
right = mid - 1;
}else{
return mid;
}
}
return -1;
}
}
题目: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。
代码:
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right){
int mid = left + ((right-left)>>1);
if(nums[mid] > target){
right = mid - 1;
}else if(nums[mid] < target){
left = mid + 1;
}else{
return mid;
}
}
return left;
}
}
题目: 给你一个按照非递减顺序 排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
代码:
class Solution {
public int[] searchRange(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
while(left <= right){
int mid = left + ((right-left)>>1);
if(nums[mid] > target){
right = mid - 1;
}else if(nums[mid] < target){
left = mid + 1;
}else{
left = mid;
right = mid;
while(left>=0 && nums[left] == target){
left--;
}
while(right<nums.length && nums[right] == target){
right++;
}
return new int[]{++left,--right};
}
}
return new int[]{-1,-1};
}
}
题目: 给你一个非负整数 x ,计算并返回 x 的 算术平方根 。由于返回类型是整数,结果只保留 整数部分 ,小数部分将被舍去 。
注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
代码:
class Solution {
public int mySqrt(int x) {
if(x==0 || x==1){
return x;
}
int left = 1; //注意这里需要由1开始
int right = x>>1 ;
while (left <= right){
int sqrt= left + ((right-left)>>1);
if(sqrt > x/sqrt){
right = sqrt- 1;
}else if(sqrt < x/sqrt){
left = sqrt + 1;
}else{
return sqrt;
}
}
return right;
}
}
class Solution {
public boolean isPerfectSquare(int num) {
if(num==0 || num==1){
return true;
}
int left = 1; //注意这里需要由1开始
int right = num>>1 ;
while (left <= right){
int sqrt= left + ((right-left)>>1);
if(sqrt > num/sqrt){
right = sqrt- 1;
}else if(sqrt < num/sqrt){
left = sqrt + 1;
}else{
right = sqrt;
break;
}
}
if(right * right == num){
return true;
}else{
return false;
}
}
题目: 给你一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,并返回移除后数组的新长度。不要使用额外的数组空间,你必须仅使用 O(1)额外空间并原地修改输入数组。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
代码:
class Solution {
public int removeElement(int[] nums, int val) {
int len = 0;
for(int j=0;j<nums.length;j++){
if(nums[j] != val){
nums[len++] = nums[j];
}
}
return len;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int k = 1;
for(int i=1;i<nums.length;i++){
if(nums[i] != nums[k-1]){
nums[k++] = nums[i];
}
}
return k;
}
}
class Solution {
public void moveZeroes(int[] nums) {
int len = 0;
for(int i=0;i<nums.length;i++){
if(nums[i] != 0){
nums[len++] = nums[i];
}
}
for(len;len<nums.lenght;len++){
nums[len] = 0;
}
}
}
class Solution {
public int[] sortedSquares(int[] nums) {
for(int i=0;i<nums.length;i++){
nums[i] = nums[i] * nums[i];
}
int[] newNums = new int [nums.length];
int left = 0;
int right = nums.length-1;
int i = nums.length-1;
while(left <= right){
if(nums[left] <= nums[right]){
newNums[i--] = nums[right--];
}else{
newNums[i--] = nums[left++]
}
}
return newNums;
}
}
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int len = nums.length + 1;
int sum = 0;
int j = 0;
for(int i=0;i<nums.length;i++){
sum += nums[i];
while(sum >= target){
len = i - j + 1 < len ? (i - j + 1):len;
sum -= sum[j];
j++;
}
}
if(len == nums.length + 1){
return 0;
}else{
return len;
}
}
}
class Solution {
public int totalFruit(int[] fruits) {
int left = 0;
int maxNum = 0;
Map<Integer,Integer> types = new HashMap<Integer,Integer>();
for (int right = 0; right < fruits.length; right++) {
int count = types.getOrDefault(fruits[right],0) + 1;
types.put(fruits[right],count);
while (types.size()>2){
types.put(fruits[left],types.get(fruits[left])-1);
if(types.get(fruits[left]) == 0){
types.remove(fruits[left]);
}
left++;
}
maxNum = maxNum > right-left+1 ? maxNum:right-left+1;
}
return maxNum;
}
}
class Solution {
Map<Character,Integer> sMap = new HashMap<Character,Integer>();
Map<Character,Integer> tMap = new HashMap<Character,Integer>();
public String minWindow(String s, String t) {
if(s.equals(t)){
return s;
}
String minSubStr = "";
int left = 0;
int subLeft = -1;
int subRight = -1;
int len = s.length();
for (int i = 0; i < t.length(); i++) {
int count = tMap.getOrDefault(t.charAt(i),0) + 1;
tMap.put(t.charAt(i),count);
}
for (int i = 0; i < s.length(); i++) {
char sChar = s.charAt(i);
if(tMap.containsKey(sChar)){
int count = sMap.getOrDefault(sChar,0) + 1;
sMap.put(sChar,count);
}
while (hasSubStr() && left<=i){
if(len >= i-left+1){
subLeft = left;
subRight = i;
len = i-left+1;
}
if(sMap.containsKey(s.charAt(left))){
int count = sMap.get(s.charAt(left)) - 1;
sMap.put(s.charAt(left),count);
}
left++;
}
}
if(subLeft == subRight && subLeft==-1){
return "";
}
return s.substring(subLeft,subRight+1);
}
public boolean hasSubStr(){
Set tkeys = tMap.keySet();
Iterator iterator = tkeys.iterator();
while (iterator.hasNext()){
Object tChar = iterator.next();
if(sMap.containsKey(tChar)){
if(sMap.get(tChar) < tMap.get(tChar)){
return false;
}
}else {
return false;
}
}
return true;
}
}
class Solution {
public int[][] generateMatrix(int n) {
int[][] arr = new int [n][n];
int num = 1;
for (int i = 0; i < n/2; i++) {
for (int j = i; j < n-i; j++) {
arr[i][j] = num++;
}
for (int j = i+1; j < n-i; j++) {
arr[j][n-i-1] = num++;
}
for (int j = n-i-2; j >= i ; j--) {
arr[n-i-1][j] = num++;
}
for (int j = n-i-2; j >i ; j--) {
arr[j][i] = num++;
}
}
if(n%2 != 0){
int i = n/2;
arr[i][i] = num;
}
return arr;
}
}
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
List<Integer> l = new ArrayList<>(n*m);
for (int i = 0; i <= m/2; i++) {
if(l.size() != n*m){
for (int j = i; j < m-i; j++) {
l.add(matrix[i][j]);
}
}
if(l.size() != n*m) {
for (int j = i + 1; j < n - i; j++) {
l.add(matrix[j][m - i - 1]);
}
}
if(l.size() != n*m) {
for (int j = m - i - 2; j >= i; j--) {
l.add(matrix[n - i - 1][j]);
}
}
if(l.size() != n*m){
for (int j = n-i-2; j >i ; j--) {
l.add(matrix[j][i]);
}
}
}
return l;
}
}
题目: 给定一个二维数组 array,请返回「螺旋遍历」该数组的结果。
螺旋遍历:从左上角开始,按照向右、向下、向左、向上的顺序依次提取元素,然后再进入内部一层重复相同的步骤,直到提取完所有元素。
代码:
class Solution {
public int[] spiralArray(int[][] array) {
if (array.length == 0)
return new int[]{};
int n = array.length;
int m = array[0].length;
int[] arr = new int[n*m];
int index = 0;
for (int i = 0; i <= m/2; i++) {
if(index != n*m){
for (int j = i; j < m-i; j++) {
arr[index++] = array[i][j];
}
}
if(index != n*m) {
for (int j = i + 1; j < n - i; j++) {
arr[index++] = array[j][m - i - 1];
}
}
if(index != n*m) {
for (int j = m - i - 2; j >= i; j--) {
arr[index++] = array[n - i - 1][j];
}
}
if(index != n*m){
for (int j = n-i-2; j >i ; j--) {
arr[index++] = array[j][i];
}
}
}
return arr;
}
}