提交 3f1945dd 编写于 作者: 两万多's avatar 两万多

2021/7/14

上级 6c6de1ed
f2.png

13.6 KB

// 70. 爬楼梯
// 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
// 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
// 注意:给定 n 是一个正整数。
//https://leetcode-cn.com/problems/climbing-stairs/submissions/
//Rust实现
impl Solution {
pub fn climb_stairs(n: i32) -> i32 {
if n <= 1 {
return n;
}
let mut dp = vec![0;(n+1) as usize];
dp[1] = 1;
dp[2] = 2;
for i in 3..(n+1) {
dp[i as usize] = dp[(i - 1) as usize] + dp[(i - 2) as usize];
}
return dp[n as usize];
}
}
//C实现
// int climbStairs(int n){
// if(n==1)
// return 1;
// int a = 1;
// int b = 2;
// int c;
// for(int i=3;i<=n ;++i){
// c = a + b;
// a = b;
// b = c;
// }
// return b;
// }
\ No newline at end of file
// 子集 II
// 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
// 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
// https://leetcode-cn.com/problems/subsets-ii/
//Rust实现
pub fn dfs(ret: &mut Vec, nums: &Vec, cur: &mut Vec, start: usize) {
ret.push(cur.clone());
for i in start..nums.len() {
if i > start && nums[i] == nums[i - 1] {
continue;
}
cur.push(nums[i]);
dfs(ret, nums, cur, i + 1);
cur.pop();
}
}
impl Solution {
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
nums.sort_unstable();
let mut cur = vec![];
let mut ret = vec![];
dfs(&mut ret, &nums, &mut cur, 0);
ret
}
}
// C++实现
// class Solution {
// public:
// vector<vector<int>> subsetsWithDup(vector<int>& nums) {
// ret.push_back(cur);
// for (int i = start; i < nums.size(); i++) {
// if (i > start && nums[i] == nums[i - 1]) continue;
// cur.push_back(nums[i]);
// dfs(cur, nums, i + 1);
// cur.pop_back();
// }
// }
// vector> subsetsWithDup(vector& nums) {
// sort(nums.begin(), nums.end());
// vectorcur;
// dfs(cur, nums, 0);
// return ret;
// }
// };
\ No newline at end of file
// 35.搜索插入位置
// 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
// 你可以假设数组中无重复元素。
// https://leetcode-cn.com/problems/search-insert-position
// Rust实现
impl Solution {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
if nums[(nums.len()-1)] < target{
nums.len()
}
if nums.len() == 0{
0
}
for i in 0..nums.len() {
if(nums[i] > target)
i
}
}
}
// C实现
// int searchInsert(int* nums, int numsSize, int target){
// if (nums[numsSize-1] < target)
// return numsSize;
// if (numsSize==1)
// return 0;
// int i = 0;
// while(nums[i] < target){
// i++;
// }
// return i;
// }
\ No newline at end of file
// 1. 两数之和
// 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
// 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
// 你可以按任意顺序返回答案。
// https://leetcode-cn.com/problems/two-sum/
// Rust实现
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut result : Vec<i32>=Vec::new();
for _i in 0..nums.len()-1{
for j in _i+1..nums.len(){
if nums[_i] +nums[j] == target{
result.push(_i as i32);
result.push(j as i32);
}
}
}
result
}
}
// C++实现
// class Solution {
// public:
// std::vector<int> twoSum(std::vector<int>& nums, int target)
// {
// int n = nums.size();
// std::vector<int> result;
// for (int i = 0; i < n - 1; i++)
// {
// for (int j = i + 1; j < n; j++)
// {
// if (nums[i] + nums[j] == target)
// {
// result.push_back(i);
// result.push_back(j);
// break;
// }
// }
// }
// return result;
// }
// };
\ No newline at end of file
// 5.最长回文子串
// 给你一个字符串 s,找到 s 中最长的回文子串。
// https://leetcode-cn.com/problems/longest-palindromic-substring
// Rust实现
impl Solution {
pub fn longest_palindrome(s: String) -> String {
let mut ns = Vec::with_capacity(s.len() << 1 + 1);
ns.push('#');
s.chars().for_each(|x| {
ns.push(x);
ns.push('#');
});
let (mut start, mut end) = (0, 0);
let mut arm_len = vec![0usize; ns.len()];
let (mut right, mut mid) = (0, 0);
for i in (0..ns.len()) {
let min_arm_len;
if right > i {
let index = mid * 2 - i;
min_arm_len = arm_len[index].min(right - i);
} else {
min_arm_len = 0;
}
let cur_arm_len = Solution::expand(&ns, i - min_arm_len, i + min_arm_len);
arm_len[i] = cur_arm_len;
if (i + cur_arm_len > right) {
mid = i;
right = i + cur_arm_len;
}
if (cur_arm_len * 2 + 1 > end - start) {
start = i - cur_arm_len;
end = i + cur_arm_len;
}
if (i + (end - start) / 2 >= ns.len()) {
break;
}
}
return String::from(&s[start / 2..end / 2]);
}
fn expand(s: &Vec<char>, left: usize, right: usize) -> usize {
let mut left = left as i32;
let mut right = right;
while left >= 0 && right < s.len() && s[left as usize] == s[right] {
left -= 1;
right += 1;
}
return (right as i32 - left - 2) as usize / 2;
}
}
//参考https://blog.csdn.net/leyi520/article/details/117026577
// Java实现
// class Solution {
// public String longestPalindrome(String s) {
// int start = 0, end = 0;
// for (int i = 0; i < s.length(); i++) {
// int len1 = expandAroundCenter(s, i, i);
// int len2 = expandAroundCenter(s, i, i + 1);
// int len = Math.max(len1, len2);
// if (len > end - start) {
// start = i - (len - 1) / 2;
// end = i + len / 2;
// }
// }
// return s.substring(start, end + 1);
// }
// private int expandAroundCenter(String s, int left, int right) {
// int L = left, R = right;
// while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
// L--;
// R++;
// }
// return R - L - 1;
// }
// }
// 15. 三数之和
// 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
// 注意:答案中不可以包含重复的三元组。
// https://leetcode-cn.com/problems/3sum/
// Rust实现
impl Solution {
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut ret: std::collections::HashSet<Vec<i32>> = std::collections::HashSet::new();
for (i, a) in nums.iter().enumerate() {
let target = 0 - *a;
let mut temp: std::collections::HashMap<i32, i32> = std::collections::HashMap::new();
for b in &nums[i + 1..] {
match temp.get(b) {
Some(&c) => {
let mut vtemp = vec![*a, *b, c];
vtemp.sort();
ret.insert(vtemp);
}
None => {
temp.insert(target - *b, *b);
}
}
}
}
ret.into_iter().collect()
}
}
// C++实现
// class Solution {
// public:
// vector<vector<int>> threeSum(vector<int>& nums) {
// vector<vector<int>>res;
// sort(nums.begin(), nums.end());
// int n = nums.size();
// for (int i = 0; i < n - 2; i++)
// {
// int L = i + 1;
// int R = n - 1;
// if (nums[i] > 0) continue;
// while (L < R) {
// if (nums[i] + nums[L] + nums[R] == 0) {
// vector<int> resList = { nums[i],nums[L],nums[R] };
// res.push_back(resList);
// }
// if (nums[i] + nums[L] + nums[R] <= 0) {
// while (L <= n - 2 && nums[L] == nums[L + 1])
// L++;
// L++;
// }
// else {
// while (R >= 1 && nums[R] == nums[R - 1])
// R--;
// R--;
// }
// }
// while (i < n - 1 && nums[i] == nums[i + 1])
// i++;
// }
// return res;
// }
// };
\ No newline at end of file
// 16. 最接近的三数之和
// 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
// https://leetcode-cn.com/problems/3sum-closest
//Rust实现
impl Solution {
pub fn three_sum_closest(nums: Vec<i32>, target: i32) -> i32 {
let mut temp = nums.clone();
temp.sort();
let (mut l, mut i, mut r): (i32, i32, i32) = (0, 1, 0);
let mut sum: i32 = 0;
let mut ret: i32 = 99999999;
while (i as usize) < temp.len()-1 {
l = i - 1;
r = i + 1;
while l >= 0 && (r as usize) < temp.len() {
sum = temp[i as usize ] + temp[l as usize ] + temp[r as usize];
if sum == target {
return sum;
}
if sum > target {
l -= 1;
} else {
r += 1;
}
if (sum - target).abs() < (ret - target).abs() {
ret = sum;
}
}
i += 1;
}
ret
}
}
//C++实现
// class Solution {
// public:
// int threeSumClosest(vector<int>& nums, int target) {
// sort(nums.begin(), nums.end());//排序 n*lgn
// int near_res = INT_MAX;
// int result = 0;
// for (int i = 0; i < nums.size()-2; i++)
// {
// int j = i + 1, k = nums.size() - 1;
// while (j < k)//O(n^2),双指针
// {
// if (near_res > abs(nums[i] + nums[j] + nums[k] - target))//找最接近的数
// {
// near_res = abs(nums[i] + nums[j] + nums[k] - target);
// result = nums[i] + nums[j] + nums[k];
// }
// if (nums[i] + nums[j] + nums[k] < target)//小值往后增加
// {
// j++;
// }
// else if (nums[i] + nums[j] + nums[k] == target)//大值往前减少
// return target;
// else
// k--;
// }
// }
// return result;
// }
// };
\ No newline at end of file
// 80. 删除有序数组中的重复项 II
// 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。
// 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
//https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii
//Rust实现
impl Solution {
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
      let n = nums.len();
      let mut i = 2usize;
      while i < nums.len() {
          if nums[i] == nums[i - 1] && nums[i - 1] == nums[i - 2] {
              nums.remove(i);
          } else {
              i += 1;
          }
      }
      nums.len() as i32
}
}
//C++实现
// class Solution {
// public:
//   int removeDuplicates(vector<int>& nums) {
//       for (int i = 2; i < nums.size(); ++i) {
//           if (nums[i] == nums[i - 1] && nums[i - 1] == nums[i - 2])
//               nums.erase(nums.begin() + i--);
//       }
//       return nums.size();
//   }
// };
// }
// };
\ No newline at end of file
// 形成两个异或相等数组的三元组数目
// 给你一个整数数组 arr 。
// 现需要从数组中取三个下标 i、j 和 k ,其中 (0 <= i < j <= k < arr.length) 。
// a 和 b 定义如下:
// a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
// b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
// 注意:^ 表示 按位异或 操作。
// 请返回能够令 a == b 成立的三元组 (i, j , k) 的数目。
//https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/
//Rust实现
use std::collections::HashMap;
impl Solution {
pub fn count_triplets(arr: Vec) -> i32 {
let n = arr.len();
let mut cnt = HashMap::new();
let mut total = HashMap::new();
let mut ans = 0;
let mut s = 0;
for k in 0..n {
let s_nxt = s ^ arr[k];
if let Some(&cnt_val) = cnt.get(&s_nxt) {
ans += cnt_val * k - *total.entry(s_nxt).or_insert(0);
}
*cnt.entry(s).or_insert(0) += 1;
*total.entry(s).or_insert(0) += k;
s = s_nxt;
}
ans as i32
}
}
//C++实现
// class Solution {
// public:
// int countTriplets(vector &arr) {
// int n = arr.size();
// unordered_mapcnt, total;
// int ans = 0, s = 0;
// for (int k = 0; k < n; ++k) {
// int s_nxt = s ^ arr[k];
// if (cnt.find(s_nxt) != cnt.end()) {
// ans += cnt[s_nxt] * k - total[s_nxt];
// }
// ++cnt[s];
// total[s] += k;
// s = s_nxt;
// }
// return ans;
// }
// };
// 12. 整数转罗马数字
// 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
// 字符 数值
// I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
// 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
// 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
// I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
// X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
// C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
// 给你一个整数,将其转为罗马数字。
//Rust实现
impl Solution {
pub fn int_to_roman(mut num: i32) -> String {
let cdr = ['I', 'V', 'X', 'L', 'C', 'D', 'M', '-', '-'];
let mut res = String::new();
for i in 0..4 {
let left = num as usize % 10;
num /= 10;
if left <= 3 {
for j in 0..left {
res.push(cdr[i * 2]);
}
} else if left <= 4 {
res.push(cdr[i * 2 + 1]);
res.push(cdr[i * 2]);
} else if left <= 8 {
for j in 5..left {
res.push(cdr[i * 2]);
}
res.push(cdr[i * 2 + 1]);
} else {
res.push(cdr[i * 2 + 2]);
res.push(cdr[i * 2]);
}
if num == 0 {
break;
}
}
res.chars().rev().collect()
}
}
//C++实现
// class Solution {
// public:
// string intToRoman(int num) {
// const string cdr = "IVXLCDM--";
// string res;
// for (int i = 0; i < 4 && num > 0; ++i) {
// int left = num % 10;
// num /= 10;
// if (left <= 3)
// res += string(left, cdr[i * 2]);
// else if (left <= 4) {
// res += cdr[i * 2 + 1];
// res += cdr[i * 2];
// } else if (left <= 8)
// res += string(left - 5, cdr[i * 2]) + cdr[i * 2 + 1];
// else {
// res += cdr[i * 2 + 2];
// res += cdr[i * 2];
// }
// }
// reverse(res.begin(), res.end());
// return res;
// }
// };
\ No newline at end of file
// 27. 移除元素
// 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
// 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
// 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
//https://leetcode-cn.com/problems/remove-element/submissions
//Rust实现
impl Solution {
   pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
       let mut left = 0;
       while left < nums.len() && nums.len() > 0 {
           if nums[left] == val {
               nums.swap_remove(left);
           } else {
               left += 1;
           }
       }
       nums.len() as i32
   }
}
//C++实现
// class Solution {
// public:
//    int removeElement(vector<int>& nums, int val) {
//        int i = 0;
//        for (int j = 0; j < nums.size(); j++) {
//                if (nums[j] != val) {
//                nums[i] = nums[j];
//                i++;
//            }
//        }
//        return i;
//    }
// };
\ No newline at end of file
// 28. 实现 strStr()
// 实现 strStr() 函数。
// 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
// 说明:
// 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
// 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。
//https://leetcode-cn.com/problems/implement-strstr
//Rust实现
impl Solution {
   pub fn str_str(haystack: String, needle: String) -> i32 {
       if needle.len() == 0 {
           return 0;
       }
       let mut j: i32 = -1;
       let next: Vec<i32> = Solution::get_next(needle.as_bytes());
       let (haystack, needle) = (haystack.as_bytes(), needle.as_bytes());
       for i in 0..haystack.len() {
           while j >= 0 && haystack[i] != needle[(j + 1) as usize] {
               j = next[j as usize];
           }
           if haystack[i] == needle[(j + 1) as usize] {
               j += 1;
           }
           if j == (needle.len() - 1) as i32 {
               return (i - needle.len() + 1) as i32;
           }
       }
       -1
   }
   pub fn get_next(s: &[u8]) -> Vec<i32> {
       let mut j: i32 = -1;
       let mut next = vec![0; s.len()];
       next[0] = j;
       for i in 1..s.len() {
           while j >= 0 && s[i] != s[(j + 1) as usize] {
               j = next[j as usize];
           }
           if s[i] == s[(j + 1) as usize] {
               j += 1;
           }
           next[i] = j;
       }
       next
   }
}
//C++实现
// class Solution {
// public:
// int strStr(string haystack, string needle) {
// int m = haystack.size(), n = needle.size();
// if(!n)
// return 0;
// int indx = 0;
// while(indx <= m - n){
// int i = 0, j = n - 1;
// int l = indx, r = indx + n - 1;
// while(haystack[l++] == needle[i++] && haystack[r--] == needle[j--]){
// if(l > r && i > j)
// return indx;
// }
// indx++;
// }
// return -1;
// }
// };
// 91. 解码方法
// 一条包含字母 A-Z 的消息通过以下映射进行了 编码 :
// 'A' -> 1
// 'B' -> 2
// ...
// 'Z' -> 26
// 要 解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:
// "AAJF" ,将消息分组为 (1 1 10 6)
// "KJF" ,将消息分组为 (11 10 6)
// 注意,消息不能分组为 (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6" 和 "06" 在映射中并不等价。
// 给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数 。
// 题目数据保证答案肯定是一个 32 位 的整数。
//https://leetcode-cn.com/problems/decode-ways
//Rust实现
impl Solution {
pub fn num_decodings(s: String) -> i32 {
       let mut res = vec![1, 1];
       let mut reslen = 2;
       let mut pre = '0';
       for ch in s.chars() {
           let mut tmp = 0;
           if (pre == '1' || (pre == '2' && ch <= '6')) {
               tmp += res[reslen - 2];
           }
           if (ch != '0') {
               tmp += res[reslen - 1];
           }
           res.push(tmp);
           reslen += 1;
           pre = ch;
       }
       res[reslen - 1]
}
}
//C++实现
// class Solution {
// public:
//    int numDecodings(string s) {
//        vector<int> res = {1, 1};
//        int reslen = 2;
//        char pre = '0';
//        for (auto & ch : s) {
//            int tmp = 0;
//            if (pre == '1' || (pre == '2' && ch <= '6')) tmp += res[reslen - 2];
//            if (ch != '0') tmp += res[reslen - 1];
//            res.push_back(tmp);
//            reslen++;
//            pre = ch;
//        }
//        return res.back();
//    }
// };
\ No newline at end of file
// 扰乱字符串
// 使用下面描述的算法可以扰乱字符串 s 得到字符串 t :
// 如果字符串的长度为 1 ,算法停止
// 如果字符串的长度 > 1 ,执行下述步骤:
// 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 x 和 y ,且满足 s = x + y 。
// 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 s = x + y 或者 s = y + x 。
// 在 x 和 y 这两个子字符串上继续从步骤 1 开始递归执行此算法。
// 给你两个 长度相等 的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 true ;否则,返回 false 。
// https://leetcode-cn.com/problems/scramble-string
//Rust实现
impl Solution {
pub fn is_scramble(s1: String, s2: String) -> bool {
   if (s1.len() != s2.len()) {
           return false;
       }
       let n = s1.len();
       let mut dp = vec![vec![vec![false; n + 1]; n]; n];
       for i in 0..n {
           for j in 0..n {
               dp[i][j][1] = (s1.chars().nth(i) == s2.chars().nth(j));
           }
       }
       for k in 2..=n {
           for i in 0..=(n - k) {
               for j in 0..=(n - k) {
                   for ck in 1..k {
                       dp[i][j][k] |= (
                           (dp[i][j][ck] && dp[i + ck][j + ck][k - ck]) ||
                           (dp[i][j + k - ck][ck] && dp[i + ck][j][k - ck])
                       );
                   }
               }
           }
       }
       dp[0][0][n]
   }
}
//C++实现
// class Solution {
// public:
//    bool isScramble(string s, string t) {
//        if (s.length() != t.length()) return false;
//        int n = s.length();
//        vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(n + 1, 0)));
//        for (int i = 0; i < n; ++ i)
//            for (int j = 0; j < n; ++ j)
//                dp[i][j][1] = s[i] == t[j];
//        for (int k = 2; k <= n; ++ k){
//            for (int i = 0; i + k <= n; ++ i){
//                for (int j = 0; j + k <= n; ++ j){
//                    for (int ck = 1; ck < k; ++ ck){
//                        dp[i][j][k] |= (
//                            (dp[i][j][ck] && dp[i + ck][j + ck][k - ck]) ||
//                            (dp[i][j + k - ck][ck] && dp[i + ck][j][k - ck])
//                        );
//                    }
//                }
//            }
//        }
//        return dp[0][0][n];
//    }
// };
\ No newline at end of file
// 搜索二维矩阵
// 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
// 每行中的整数从左到右按升序排列。
// 每行的第一个整数大于前一行的最后一个整数。
// https://leetcode-cn.com/problems/search-a-2d-matrix
// Rust实现
impl Solution {
pub fn search_matrix(matrix: Vec>, target: i32) -> bool {
let (m, n) = (matrix.len(), matrix[0].len());
let (mut left, mut right) = (0, (m * n - 1) as i32);
while (left <= right) {
let mid = (left + right) / 2;
if matrix[mid as usize / n][mid as usize % n] == target {
return true;
} else if matrix[mid as usize / n][mid as usize % n] > target {
right = mid - 1;
} else {
left = mid + 1;
}
}
false
}
}
// C++实现
// class Solution {
// public:
// bool searchMatrix(vector<vector<int>>& matrix, int target) {
// int m = matrix.size(), n = matrix[0].size(), left = 0, right = m * n - 1;
// while (left <= right) {
// int mid = (left + right) / 2;
// if (matrix[mid / n][mid % n] == target) return true;
// else if (matrix[mid / n][mid % n] > target) right = mid - 1;
// else left = mid + 1;
// }
// return false;
// }
// };
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册