leetcode (1-bit and 2-bit Characters)
阿新 • • 發佈:2018-12-06
Title:1-bit and 2-bit Characters 717
Difficulty:Easy
原題leetcode地址:https://leetcode.com/problems/1-bit-and-2-bit-characters/
1. 遍歷陣列,如果是1,則加2,如果是0,則加1;
時間複雜度:O(n),一次一層for迴圈,需要遍歷整個陣列。
空間複雜度:O(1),沒有申請額外的空間。
/** * 遍歷陣列,如果是1,則加2,如果是0,則加1;(注意是1的時候,必須加2) * @param bits * @return */ public static boolean isOneBitCharacter(int[] bits) { int i = 0; while (i < bits.length - 1) { if (bits[i] == 1) { i += 2; } else { i++; } } return i == bits.length - 1; }